Quit Parallels Desktop 7 via script causes an error

Hello everybody.

I need your help to solve a problem with the closure of Parallels Desktop 7 (in Coherence mode, no icon in the dock but only in the menu bar) via a script to restart or Shutdown the Mac.
Whatever kind of command I try to use (quit, kill -9, killall) the application is closed, but it always interrupts the restart/shutdown giving this error: "The application canceled the restart. To try again, quit Parallels Desktop and choose restart from the Apple menu. "
Also by choosing restart (or shutdown) from the Apple menu, I get the exact same error. The only way to close it without error message is left click the icon in the menu bar and choose “Quit Parallels Desktop”,


display dialog "Make your choiche." buttons {"Cancel", "Restart", "Shutdown"} default button 1 cancel button 1
tell application "System Events"
	if button returned of the result is "Shutdown" then
		quitApp
		shut down
	else
		quitApp
		restart
	end if
end tell

on quitApp()
	set safeAppList to {"Finder", "MYC"} --MYC is the name of the script
	
	tell application "System Events"
		set visible of every process to true
		set visibleApps to (displayed name of every process whose visible is true and name is not safeAppList)
		set invisibleApps to (displayed name of every process whose visible is false and name is not safeAppList)
set allApps to visibleApps & invisibleApps
	end tell
	
if allApps contains "Parallels Desktop" then
try
tell application "Parallels Desktop" to quit
repeat while (application "Parallels Desktop" exists)
delay 0.5
end repeat
end try
end if

	repeat with anApp in theAppsNames
		try
			tell application anApp to quit
		end try
	end repeat
end quitApp

AppleScript: 2.2.1
Browser: Chrome 31.0.1650.63
Operating System: Mac OS X (10.7)

Given that the only way seems to be operate on the icon in the menu bar, I also tried to use


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

but nothing happens.

EDIT
I used UI Browser to find the “coordinates” of the menu.

Hi,

I doubt that the script works at all.
The handler quitApp is never called because the parentheses are missing

try it this way


property safeAppList : {"com.apple.Finder"}

display dialog "Make your choiche." buttons {"Cancel", "Restart", "Shutdown"} default button 1 cancel button 1

if button returned of the result is "Shutdown" then
	quitApp()
	tell application "System Events" to shut down
else
	quitApp()
	tell application "System Events" to restart
end if

on quitApp()
	tell application "System Events"
		set appsToQuit to bundle identifier of every process whose background only is false
	end tell
	
	if application "Parallels Desktop" is running then
		quit application "Parallels Desktop"
	end if
	
	repeat with anApp in appsToQuit
		if anApp is not in safeAppList then
			try
				quit application id anApp
			end try
		end if
	end repeat
end quitApp


Hi Stefan,
thanks for your help.

You’re right, I copied an old version of the script, sorry. But I’ve tried also with parentheses and the problem was the same.

Now with your suggestion no longer appears the error message, but the process is interrupted anyway.
If I run the script directly, in the results window I get :
error "Parallels Desktop has found an error: Operation canceled by user. " number -128

Sorry I can’t help with Parallels Desktop

Hi,

I have the same problem with HexEdit. HexEdit opens with an Open dialog. This messes up the quitting of visible processes. The dialog is waiting for user input.

gl,
kel

After attempts of any kind (except magic) maybe, just maybe, I found the solution (tests made worked perfectly) :


property safeAppList : {"com.apple.Finder"}

display dialog "Make your choiche." buttons {"Cancel", "Restart", "Shutdown"} default button 1 cancel button 1

if button returned of the result is "Shutdown" then
	quitApp()
	tell application "System Events" to shut down
else
	quitApp()
	tell application "System Events" to restart
end if

on quitApp()
	tell application "System Events"
		set appsToQuit to bundle identifier of every process whose background only is false
	end tell
	
	if application "Parallels Desktop" is running then
		ignoring application responses
			quit application "Parallels Desktop"
		end ignoring
	end if
	
	repeat with anApp in appsToQuit
		if anApp is not in safeAppList then
			try
				quit application id anApp
			end try
		end if
	end repeat
end quitApp

@Stefan : thanks for your optimization of my horrible script.

@kel1 : maybe you can use this solution to solve the problem you have with HexEdit.