Submit Hint Search The Forums LinksStatsPollsHeadlinesRSS
14,000 hints and counting!

Run AppleScripts and shell scripts in a Windows VM Apps
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.]
    •    
  • Currently 3.20 / 5
  You rated: 4 / 5 (15 votes cast)
 
[8,721 views]  

Run AppleScripts and shell scripts in a Windows VM | 5 comments | Create New Account
Click here to return to the 'Run AppleScripts and shell scripts in a Windows VM' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
Run AppleScripts and shell scripts in a Windows VM
Authored by: tom_b on May 17, '10 01:13:46PM
To avoid having to put your password in plaintext in the script, consider creating a public-private key pair as described in the PuTTy documentation.

[ Reply to This | # ]
Run AppleScripts and shell scripts in a Windows VM
Authored by: syao on May 17, '10 03:15:26PM

I used a very similar way back when I needed to generate Growl notifications from Windows Live Messenger running into a Parallels Desktop VM.

A hint I feel like adding, in case people don't want to store their password in a plain text file (being paranoid is never a bad thing, is it), you can use puttygen.exe (download from the same page as plink.exe) to generate a key pair to be used with plink.

  1. In the VM, run puttygen and generate a new 1024-bit SSH2-DSA key
  2. Hit the Save private key button and store it to a convenient location (example: C:\Windows\privkey.ppk)
  3. Copy the text in the "Public key for pasting into OpenSSH authorized_keys file"
  4. Back to the Mac desktop, run the command touch ~/.ssh/authorized_keys && open -t ~/.ssh/authorized_keys from a Terminal
  5. Paste in the text editor that pops up the key you previously copied, save and close it

You can now use the following command to run the above mentioned script, and you won't need to edit it if you change your Mac password (it doesn't use passwords anymore to login):
plink.exe -ssh -i C:\Windows\privkey.ppk -noagent -m disable.txt Dave@DavesMac



[ Reply to This | # ]
Run AppleScripts and shell scripts in a Windows VM
Authored by: davechevell on May 18, '10 12:51:15AM

That's excellent - thanks! A very necessary addition.


I'm curious about getting Windows apps to display growl notifications - not just MSN, but anything... any chance you could point me in the right direction?



[ Reply to This | # ]
Run AppleScripts and shell scripts in a Windows VM
Authored by: dgerrity on May 18, '10 06:23:34PM
What a fantastic hint! I've been frustrated by this problem as well and am a little chagrined that I didn't think of this solution -- especially since I have the whole cygwin implementation on my Windows VM.

The best part of this hint (for me) is the realization that I can forward specific ports through the ssh connection. My company network blocks IMAP, SMTP, POP3 and many other ports, which is painful. I've tried to connect the VM to the Mac via a virtual router; it's just out of my league in terms of making it work.

There is a separate outside wireless network available that doesn't block any ports. I put the Mac Airport on the public net, bridge the Windows VM to the ethernet adapter, and forward the ports I need for IMAP from the Windows environment to the Mac -- where it can escape the tyranny of the corporate net.

Thank you -- you're brilliant. Among the best hints of all time for Mac users forced into a windows environment.

[ Reply to This | # ]
Run AppleScripts and shell scripts in a Windows VM
Authored by: Anonymous on May 18, '10 07:31:09PM

in your shell command to open fusion, you need the .app or else it will fail

open "/Applications/VMWare Fusion.app/"

or

open /Applications/VMware\ Fusion.app/



[ Reply to This | # ]