AppleScript and Parallels?

I don’t see a Dictionary, but I’ve read that there is some ability to script Parallels…

I’m looking to write a script that when run will bring a specific Virtual Machine (by name) to the front.

I run 5 virtual machines for website testing and want to call-forward the correct on using my Xkeys pad (which can run AppleScripts via ContollerMate).

Is this possible?

Assuming that each Virtual Machine has a separate window, you just have to know what name of the process is.

This script should work:

property ProcessName : "Virtual Desktop"

tell application "System Events" to tell process ProcessName
	set AllWindows to (name of every window)
	set ChosenWindow to (choose from list AllWindows)
	activate window (ChosenWindow as string)
end tell

Hope it works,
ief2

Parallels doesn’t seem to respond to “activate” or “open” window, either via tell or System Events.

I have noticed Parallels Desktop seems to spawn 5 identical subprocesses (prl_vm_app) but I can’t figure out how to tell which is which or address them directly.

Does Parallels use some sort of trickery to keep itself from being scripted, even via UI?

I found a fix for this stuff after banging my head on it for a few hours. I stumbled onto this line when I was trying to send mouse clicks and it was showing the last result in applescript:

scroll area 1 of window “Windows - Parallels Desktop” of application process “prl_client_app” of application “System Events”

If you ever have problems with “Parallels Desktop”, try “prl_client_app” or one of the other processes in Activity Monitor. Also, here is the code to show a specific window:


tell application “Parallels Desktop” to activate

on GetParallelsWindows()
tell application “System Events”
tell process “Parallels Desktop”
return name of every window
end tell
end tell
end GetParallelsWindows

on ActivateParallelsWindow(theName)
set theName to theName as string – work around an Applescript bug where vars can fail comparisons if not strings
if theName is “Parallels Virtual Machines” or theName is “Virtual Machines List” then
set theName to “Virtual Machines List” – for some reason the VM list’s window and menu name are different
else
if theName ends with " - Parallels Desktop" then
set theName to (text items 1 through ((length of theName) - (length of " - Parallels Desktop")) of theName) as string
end if
end if
tell application “System Events”
tell process “Parallels Desktop”
click menu item theName of menu of menu bar item “Window” of menu bar 1
end tell
end tell
end ActivateParallelsWindow

set theNames to GetParallelsWindows()

repeat with theName in theNames
ActivateParallelsWindow(theName)
delay 1
end repeat


And here is some more code to get/set the position of the window, in case someone needs it:


tell application “System Events”
set thePos to position of window 1 of process “prl_client_app”
–set thePos to position of window 1 of process “Parallels Desktop” – works too
–set theName to name of front window of process “Parallels Desktop”
end tell

return thePos

tell application “System Events”
set the position of window 1 of process “Parallels Desktop” to {100, 100}
end tell

return


Parallels does accept key presses but not mouse clicks:


tell application “Parallels Desktop” to activate

tell application “System Events”
tell process “Parallels Desktop”
–key code 123 – left
–key code 124 – right
–key code 126 – up
–key code 125 – down
–key code 86 – number pad 4, for StickyMouse
–key code 88 – number pad 6, for StickyMouse
–key code 91 – number pad 8, for StickyMouse
–key code 84 – number pad 2, for StickyMouse
key code 87 – number pad 5, for StickyMouse
–keystroke return
–click at {100, 100} – fails
end tell
end tell


You can try a free tool like MouseTools to control the cursor from the console:

tell application “Finder”
set myPath to POSIX path of ((container of (path to me)) as alias)
end tell

on ClickMouse(x, y)
global myPath

set theCommand to myPath & "MouseTools" & " -x " & x & " -y " & y & " -leftClick"

do shell script theCommand

end ClickMouse

ClickMouse(20, 10) – click Apple menu


Unfortunately there’s a bug in mouse tools that keeps the mouse pressed. Also it doesn’t seem to work with Parallels, so I ended up using StickyMouse in XP and sending the number pad 5 key to simulate a click. With these tools, you can activate a window at a known location, move the mouse in reference to that, send a click to bring up a menu, and then navigate with the arrow keys and return. It’s not ideal, but does enough for me to finish my work.

Hope this helps someone.

Model: Mac Mini
AppleScript: 2.2.1
Browser: Safari 534.51.22
Operating System: Mac OS X (10.7)