Mac OS X

Programmatically suspending, quitting, and resuming Parallels Desktop for scheduled backup

I wanted to do automatic scheduled backups with the awesomely amazing SuperDuper!. The computer to be backed up was a Mac Mini running Mac OS X 10.5 Leopard. But the problem was that Parallels Desktop was running all the time. And everyone knows that when you try to back up open files that may be changing, Bad Things Happen.

"No problem," I thought. Since SuperDuper! allows you to run a script before and after a scheduled backup (have I mentioned how brilliant SuperDuper! is?) I'll use a script to suspend the virtual machine before the backup, and resume it afterward.

SuperDuper! with shell scripts assigned

So I fired up Script Editor and went to open up Parallels Desktop's AppleScript Dictionary.

Uh.

Apparently Parallels Desktop does not implement AppleScript.

I was stymied until I remembered something that the indefatigable Matt Neuburg had written in AppleScript: The Definitive Guide, Second Edition in a chapter titled Unscriptable Applications. The approach is to cheat by using the Accessibility API built into Mac OS X. From there you can direct mouse clicks, radio button selection...in short, all sorts of mayhem. "Aha!" I thought. "I'll just direct Parallels from the Accessibility API! Sort of like the puppeteer in Being John Malkovich."

First I made sure that accessibility was enabled by going to the Universal Access preference pane in System Preferences.

Enable assistive devices

Then I made a shell script for SuperDuper! to run before backup. Here's the script:

#!/bin/sh

osascript /Users/john/suspendparallels.txt
sleep 35
osascript /Users/john/quitparallels.txt
sleep 10

The shell script first executes the suspendparallels.txt AppleScript to suspend the virtual machine:

-- Suspend a Parallels virtual machine.
-- Assumptions:
--   - only one virtual machine is running
--   - the "Enable access for assistive devices" checkbox is checked in the Universal Access control panel
-- Tested on Mac OS X 10.5.7 with Parallels Desktop 4.0.
-- John VanDyk 7/22/2009

tell application "Parallels Desktop" to activate
tell application "System Events"
  tell application process "Parallels Desktop"
    tell menu "Virtual Machine" of menu bar 1
      click menu item "Suspend"
    end tell
  end tell
end tell

Then the shell script waits for 35 seconds (enough time for the virtual machine to be written to disk), and executes the quitparallels.txt AppleScript:

-- Quit Parallels.
-- Assumptions:
--   - only one virtual machine is running
--   - the "Enable access for assistive devices" checkbox is checked in the Universal Access control panel
-- Tested on Mac OS X 10.5.7 with Parallels Desktop 4.0.
-- John VanDyk 7/22/2009

tell application "Parallels Desktop" to activate
tell application "System Events"
  tell application process "Parallels Desktop"
    tell menu "Parallels Desktop" of menu bar 1
      click menu item "Quit Parallels Desktop"
    end tell
  end tell
end tell

"But John," I can hear you asking, "why not put these together into one simple AppleScript and just use 'delay 35' to create the pause?" I'll tell you why. Because when you do it that way it doesn't work. Plus I got a scary error about "class released with no pool in place - just leaking". Seriously. So I figured, since AppleScript is always, well, flaky for me, I'd just put as much as I could in the shell script and call out to AppleScript only when necessary.

All right. So now SuperDuper! can clone the drive since the Parallels Desktop Windows XP virtual machine has been suspended and the program has been closed. But what about afterwards, when we want to bring Parallels Desktop up just like it was? Turns out, that's even easier. Here's the shell script that SuperDuper! runs after it completes the copy:

#!/bin/sh

osascript -e 'tell app "Parallels Desktop" to activate'
sleep 10
osascript /Users/john/resumeparallels.txt

That first osascript call is actually launching Parallels Desktop. We give it 10 seconds to launch, which is plenty.

Fortunately it launches with the suspended virtual machine. All we have to do is tell it to resume. Here's resumeparallels.txt:

-- Resume a Parallels virtual machine.
-- Assumptions:
--   - only one virtual machine is running
--   - the "Enable access for assistive devices" checkbox is checked in the Universal Access control panel
-- Tested on Mac OS X 10.5.7 with Parallels Desktop 4.0.
-- John VanDyk 7/22/2009

tell application "Parallels Desktop" to activate
tell application "System Events"
  tell application process "Parallels Desktop"
    tell menu "Virtual Machine" of menu bar 1
      click menu item "Resume"
    end tell
  end tell
end tell

Presto! It works.

Topic: 

Negative deletion

I'm confused...does this mean Leopard is actually creating files?

Topic: 

Example: How to install a package for R on Mac OS X 10.5 Leopard

Assuming that you already have the R statistics program installed on your Mac, the following can be used to install a package from the command line.

First, get the .tgz file (we'll use plotrix as the package we want to install):

$ curl http://cran.r-project.org/bin/macosx/universal/contrib/r-release/plotrix_2.6-1.tgz > plotrix_2.6-1.tgz

Check that R is properly installed:

$ which R
/usr/bin/R

Install the package:

$ sudo R CMD INSTALL plotrix_2.6-1.tgz
Password:
* Installing to library '/Library/Frameworks/R.framework/Resources/library'
* Installing *binary* package 'plotrix' ...
* DONE (plotrix)

Check that everything is happy:

$ ls -l /Library/Frameworks/R.framework/Resources/library/
total 8
drwxrwxr-x  16 root  admin  544 Jun 26 09:59 KernSmooth
drwxrwxr-x  20 root  admin  680 Jun 26 09:59 MASS
drwxrwxr-x  22 root  admin  748 Jun 26 09:59 Matrix
-rw-rw-r--   1 root  admin  866 Jun 26 10:00 R.css
drwxrwxr-x  15 root  admin  510 Jun 26 09:59 base
drwxrwxr-x  16 root  admin  544 Jun 26 09:59 boot
drwxrwxr-x  18 root  admin  612 Jun 26 09:59 class
drwxrwxr-x  16 root  admin  544 Jun 26 09:59 cluster
drwxrwxr-x  13 root  admin  442 Jun 26 09:59 codetools
drwxrwxr-x  13 root  admin  442 Jun 26 09:59 datasets
drwxrwxr-x  18 root  admin  612 Jun 26 09:59 foreign
drwxrwxr-x  17 root  admin  578 Jun 26 09:59 grDevices
drwxrwxr-x  15 root  admin  510 Jun 26 09:59 graphics
drwxrwxr-x  16 root  admin  544 Jun 26 09:59 grid
drwxrwxr-x  19 root  admin  646 Jun 26 09:59 lattice
drwxrwxr-x  15 root  admin  510 Jun 26 10:00 methods
drwxrwxr-x  15 root  admin  510 Jun 26 09:59 mgcv
drwxrwxr-x  21 root  admin  714 Jun 26 09:59 nlme
drwxrwxr-x  18 root  admin  612 Jun 26 09:59 nnet
drwxr-xr-x  15 root  admin  510 Jul  7 14:30 plotrix
drwxrwxr-x  17 root  admin  578 Jun 26 09:59 rpart
drwxrwxr-x  20 root  admin  680 Jun 26 09:59 spatial
drwxrwxr-x  15 root  admin  510 Jun 26 09:59 splines
drwxrwxr-x  18 root  admin  612 Jun 26 09:59 stats
drwxrwxr-x  14 root  admin  476 Jun 26 09:59 stats4
drwxrwxr-x  16 root  admin  544 Jun 26 09:59 survival
drwxrwxr-x  17 root  admin  578 Jun 26 09:59 tcltk
drwxrwxr-x  15 root  admin  510 Jun 26 09:59 tools
drwxrwxr-x  17 root  admin  578 Jun 26 09:59 utils

Sure enough, plotrix is there nestled among the other packages.

Topic: 

Blast2GO BLAST results not opening in Mac web browser

If you use Blast2GO on Mac OS X 10.5 Leopard (and haven't been derailed completely by Apple moving Java Web Start around), you might have run into the issue of not being able to view BLAST results in a web browser. Blast2GO even helpfully opens a window for you to choose which browser you want to use, but no matter what you select, nothing happens. Except the following gets written to the Application Messages pane:

Error with choosen Browser! /Applications/Firefox.app: cannot execute

The solution is to tell Blast2GO which browser you want to use. You do that by specifying a path in the blast2go.properties file. You can find the file at /Users/yourusername/blast2go/blast2go.properties.

When you open the file in your text editor* you'll see a property entry under User Pathes (sic) called BlastBrowser.Explorer. You may be tempted to put /Applications/Firefox.app here or even, if you're clever, /Applications/Firefox.app/Contents/MacOS/firefox. But don't do that. Firefox will just complain at you about more than one instance running at a time. Rather, use /usr/bin/open and it will automatically launch the appropriate browser.

// User Pathes
MainGui.Workspace=/Volumes/Data/blast2go_datafile.dat
MainGui.Favorites=/Volumes/Data/blast2go_datafile.dat,
BlastBrowser.Explorer=/usr/bin/open

I ensured that Firefox was the default application to open .fcgi files by creating a text file called foo.fcgi and then selecting Get Info. Probably unnecessary because /usr/bin/open opens URLs in the default browser anyway.

*I used TextWrangler -- if you're still opening files in TextEdit do yourself a favor and download TextWrangler, then make it the default text editor.

Pages

Subscribe to RSS - Mac OS X