Showing posts with label Applescript. Show all posts
Showing posts with label Applescript. Show all posts

2011-01-03

A Mac OS X app to wake a Mac and then open screen sharing

I have a Mac Mini hooked up to our TV and stereo as a HTPC which has a hard-wired Ethernet connection. To save a bit of energy I have it set to go to sleep when it isn't used. My wife wanted to be able to play internet radio stations over the stereo, but she didn't want to have to turn on the TV, get a keyboard, etc. just to start one playing. I showed her how to use one program to wake the Mac Mini, and then how to launch screen sharing through Finder, but it was a lot of clicking. So I started googling for a way to set up a one-click icon that would (1) wake the Mac Mini, and (2) then connect to it by screen sharing.

As to waking the Mac Mini, I found this page that gives an Automator workflow to send the wake on lan magic packet (which will only work if the target computer has a hard wired Ethernet connection) and also open screen sharing:

Wake sleeping Mac with AppleScript and Automator

The guts of this is a PHP script written by Mark Muir to send the magic packet that wakes the sleeping computer. The version of this PHP script at the link has comments explaining how it works. Since I have zero experience with Automator I decided to adapt this to be an AppleScript. Here is what I ended up with:

on run
set command to "/usr/bin/php -r " & quoted form of ("$mac = \"a4:6a:19:d8:d2:24\";
$ip = \"10.10.10.255\";
$mac_bytes = explode(\":\", $mac);
$mac_addr = \"\";
for ($i=0; $i<6; $i++)
$mac_addr .= chr(hexdec($mac_bytes[$i]));
$packet = \"\";
for ($i=0; $i<6; $i++) /*6x 0xFF*/
$packet .= chr(255);
for ($i=0; $i<16; $i++) /*16x MAC address*/
$packet .= $mac_addr;

$port = 9;
$sock = socket_create(AF_INET, SOCK_DGRAM, SOL_UDP);
socket_set_option($sock, SOL_SOCKET, SO_BROADCAST, TRUE);
socket_sendto($sock, $packet, strlen($packet), 0, $ip, $port);
socket_close($sock);
")
do shell script command
delay 3
tell application "Screen Sharing"
open location "vnc://MyComputersBonjourName.local"
end tell
end run


To use this yourself do the following:

  • Make sure screen sharing is configured on the target computer and verify it works when you manually connect from the source computer.
  • Go to the target computer and open Network Utility and look up the Hardware Address of the target computers Ethernet network interface. Change the $mac value in the Applescript to be this value.
  • Look up the Bonjour name of the target computer (System Preferences -> Sharing : Computer Name) and substitute it for MyComputersBonjourName in the Applescript.
  • Change the $ip value in the Applescript to be the IP address of your router with 255 substituted for the last digits. For many people this would be 192.168.1.255. Also, 255.255.255.255 should work.
  • Open AppleScript Editor on the source computer (the one you want to connect from) and paste the script in it and then click Compile and then click Run to test it. It should wake the target computer and open screen sharing for it.
  • Save the AppleScript as an app by selecting File -> Save As and then picking application as the file type in the dialog box.
  • Double click on the app to verify it works.
  • Drag the app to the Dock or wherever you want it to live.

2009-06-12

Applescript to give a primative command line interface to MS Word 2008

One of the things I hate about MS Word is having to use the mouse to click through menus to do simple things. Of course you can create keyboard shortcuts for stuff, but I find it hard to remember really complicated keyboard combos. What I prefer is the old XyWrite approach, which is to hit a command key, then enter a two or three letter code for an action, and then hit enter to have it execute.

Back when I was using Word 2000 for Windows I found a simple solution: Create a keyboard shortcut for the Run Macro command (Ctrl-M), and then write macros with 2-3 character names. For example: I wrote a macro to accept all revisions in a document, then named it 'aa' (for accept all). Then to accept all revisions all I had to do was hit Ctrl-M, type aa, hit enter, and boom, all the revisions were accepted.

When I switched to Mac OS X and Word 2008 for Mac I couldn't find any obvious way to recreate this functionality, so I suffered through clicking through menus for a lot of commands. However, one day I just reached my limit with click-click-click-click and decided to find a solution using Applescript. Here is what I ended up with:

-- Open a dialog box for user input
set var_input to display dialog "Word command: " default answer ""
-- For some reason you need to do this to get the user input into a string variable
set var_input to (text returned of var_input) as string
-- Applescript apparently doesn't have SELECT CASE statements so use If Else to take different
-- actions based on user input
if var_input = "st" then
    -- Convert selected text to strikethrough font
    tell application "Microsoft Word"
        set strike through of font object of selection to true
    end tell
else if var_input = "aa" then
    -- Accept all changes in the document
    tell application "Microsoft Word"
        accept all revisions active document
    end tell
else if var_input = "kt" then
    -- Toggle keep text together (so text doesn't break over page break)
    tell application "Microsoft Word"
        tell selection
            if ((keep with next of paragraph 1 as boolean) is false) then
                set keep with next of every paragraph to true
            else
                set keep with next of every paragraph to false
            end if
        end tell
    end tell
else if var_input = "uc" then
    -- Convert the selected text to all UPPERCASE
    tell application "Microsoft Word"
        set case of characters of selection to upper case
    end tell
end if
What this does is open up a dialog box where you can type in a string of characters, and then based on what you entered it tells MS Word to do some command or other.

Then I saved the Applescript in the folder:
Users/myusername/Documents/Microsoft User Data/Word Script Menu Items

With the name:
Command Line\cM.scpt

Although its not at all obvious, the \cM.scpt at the end creates a Ctrl-M as a keyboard shortcut for the Applescript. For more on naming Applescripts in this folder so they have keyboard shortcuts see:

http://www.entourage.mvps.org/script/add_shortcuts.html

Its for Entourage, but the script naming conventions appear to be the same.