Sometimes I want to run AppleScripts, bash scripts, etc. directly inside my Windows virtual
machine at work. Even though I'm working in a Windows environment, there are so many useful things
that can be done via AppleScripts or Terminal commands to control network, system and component
settings, that it can be very useful to run them directly from the virtual machine.
After messing about with clunky folder actions that would trigger an Automator action (or launchd
plist) to arbitrarily kick off scripts, it occured to me that a much better way of doing this would
be to find a Terminal client on Windows that could ssh into the OS X host and run commands
directly.
It's important that this solution be scriptable, so we can create double-clickable actions to
perform common tasks. In this example, we will be using Richard Kulesus' excellent, free
AppleScripts to disable and enable Active Screen Corners, directly within our Windows VM. Bear in
mind that this requires at least a middling knowledge of Windows - this is probably most useful to
Mac owners who have to run Windows at work and have some level of familiarity with it. Now, let's
teach Windows to speak a little Unix..
My workmates freak out a little when using my laptop, because every time they hit a corner of the
screen it activates Dashboard, Spaces, or Exposé. Richard Kulesus created a great pair
of AppleScripts to disable/enable Active Screen Corners, but switching it on and off involved
moving away from my virtual machine, running the script, and moving back. Well, none of us would be
here if we didn't believe that anything requiring requiring 10 seconds work couldn't be achieved in
less than 1, would we?
The utility that makes this work is a small Windows-based command line ssh client, called
plink.exe. You can download it (as well as a GUI-based Terminal emulator, 'putty.exe') from
here.
Save plink.exe into
C:\Windows\System32 (this is not strictly necessary, but it makes writing batch scripts a
lot easier, as we will be able to call plink.exe directly - much like when we put an app in one of
OS X's system directories so we can call it directly in Terminal without having to specify its exact
location).
In Windows, create first a .txt file that contains the commands you want to send to OS X's
Terminal. In my case, I created a filed called disable.txt that contains only the following line, to
run my desired AppleScript:
osascript /Users/Dave/Scripts/disableHotCorners.scpt
Then, with plink.exe saved into the System32 directory as mentioned above, I created an MS-DOS
batch file that contains the following command:
plink.exe -ssh -pw mypassword -noagent -m disable.txt Dave@DavesMac
Set 'mypassword' to your OS X password, 'disable.txt' is the name of the .txt file containing
your desired commands, 'Dave' should be your OS X username, and 'DavesMac' should be your Computer
Name (go to System Prefs » Sharing to see what your Mac's Computer Name is set to - or use the IP
address of the host, if you must).
Thus, when I run my batch file in my Windows virtual machine, it runs plink.exe, reads the
desired commands contained in my specified .txt file, makes an ssh connection to the OS X host, and
runs the commands I specified.
Note: If your AppleScript calls any other application (and it usually will) OS X will steal focus
from the VM, and the dock and menu bar will appear until you click back into the virtual machine.
You can combat this by ending your AppleScripts with the line:
tell application "VMWare Fusion" to activate
This will give focus back to your virtual machine - you will probably see the dock and menu bar
flicker in and out of sight for a moment, as your script is run. If you're Using Parallels or
VirtualBox you would obviously use that app's name instead. If you're running apps directly from
Terminal, I presume the command open "/Applications/VMWare Fusion" will suffice - if you know
of a better way to give focus to an app from Terminal, sound off in the comments.
If you can think of any other clever uses for this kind of thing, I would love to hear
about it. The program plist.exe seems to be able to retrieve the results of Terminal commands, so I suppose it's
possible for OS X and Windows to establish a dialogue in this way, passing parameters back and
forth.
[crarko adds: I haven't tested this one.]
Mac OS X Hints
http://hints.macworld.com/article.php?story=20100517020728222