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.

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.

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.