Improvements: Quit an application and wait until it is closed.

Hi,

I’m currently writing a script that allows me to switch between my two different iTunes Libraries.
One uses my external harddrive and the other one has only a few mp3s on my local drive in it.

Before the script gets really active it has to check if iTunes is running and if so, wait until it is closed.

I came up with following solution:


tell application "System Events" to set iTunes_running to (name of processes) contains "iTunes"
if iTunes_running then
	display dialog "iTunes is running. Shut down iTunes Now?" buttons {"Cancel", "Quit iTunes now"} default button 2 with icon stop
	if button returned of result = "Quit iTunes now" then
		tell application "iTunes" to quit
		-- check every 3 seconds is iTunes is still running,
		-- since it needs some time until it is quit.
		repeat while iTunes_running
			tell application "System Events" to set iTunes_running to (name of processes) contains "iTunes"
			delay 3
		end repeat
		display dialog "iTunes is not running anymore!" buttons {"Quit"} default button 1
	else
		-- quit script and don't quit iTunes
		quit
	end if
end if

Questions: Is there a better way or are there any improvements. It seems that either the script or the act of quitting iTunes take some system ressources.
Any suggestions or improvements would be nice.

Model: iBook G3, 800Mhz, 640 RAM
AppleScript: 1.10
Browser: Safari 412.5
Operating System: Mac OS X (10.4)

Hi, xphilx.

I think you’re basically doing the right thing. It may be slightly more efficient to say ‘(process “iTunes” exists)’ rather than ‘(name of processes) contains “iTunes”’. It may also make more sense to put the delay in the repeat above the process check. Otherwise you’re setting the ‘iTunes_running’ variable and then waiting three seconds before looking to see what you set it to. I personally would write the repeat something like this:

tell application "System Events"
	repeat while (application process "iTunes" exists)
		delay 0.5
	end repeat
end tell

‘quit’ is a command aimed at applications. The line in your script that just says ‘quit’ will quit the application running the script, which is only the same as just quitting the script if the script’s saved as an application. Also, the application will usually only obey the ‘quit’ command after it’s finished running the script. If you want the script to stop dead at a certain point, you can use ‘error number -128’.

Hi,

Try this and see if it helps…or give you some ideas.

The script below quits iTunes almost instantaneously and, I believe, should not use up much resources. Is this what you want?

tell application "System Events" to set allOpenApps to name of every process
if "iTunes" is in allOpenApps then
	display dialog "iTunes is running. Shut down iTunes Now?" buttons {"Cancel", "Quit Now"} default button 2 with icon stop
	if button returned of the result is "Quit Now" then tell application "iTunes" to quit
	beep
	display dialog "iTunes has quit now." buttons "Bye.." giving up after 2
else
	--do nothing
end if

Good luck!

archseed :slight_smile:

Hi Nigel Garvey,

thanks for the nice hints.
This is what the script looks like now:


tell application "System Events"
	if (application process "iTunes" exists) then
		display dialog "iTunes is running. Shut down iTunes Now?" buttons {"Cancel", "Quit Now"} default button 2 with icon stop giving up after 10
		if button returned of the result is "Quit Now" then
			tell application "iTunes" to quit
			repeat while (application process "iTunes" exists)
				delay 0.5
			end repeat
			beep
			display dialog "iTunes has quit now." buttons "Continue" giving up after 2
		else
			-- Do not shutdown iTunes or waited longer than 10 seconds
			-- abort Script:
			error number -128
		end if
	else
		-- iTunes is not running
	end if
end tell

It’s working pretty well. Unfortunately there’s only one thing bothering me:
After I start the script and if iTunes is active the upcoming dialog will not the active window. So I need to click twice. One time to activate and then a 2nd time to hit the Quit iTunes button.

I guess it is because I use the tell “System Events” command.

Any ideas who I can get this fixed?

Model: iBook G3, 800 Mhz, 640 MB RAM
AppleScript: 1.10
Browser: Safari 412.5
Operating System: Mac OS X (10.4)

hello archseed,

I don’t see the difference to my first script, since you use the same command to shutdown iTunes.
I need to wait before the script will continue until iTunes is closed.

I got the feeling that your script will continue even when iTunes is in the process of shutting down.

Or am I wrong?

Model: iBook G3, 800 Mhz, 640 MB RAM
AppleScript: 1.10
Browser: Safari 412.5
Operating System: Mac OS X (10.4)

Hi, xphilx.

Yes. Since the ‘display dialog’ command is in the ‘tell application “System Events”’ block, it’s System Events that’s displaying the dialog. You need to bring System Events to the front (using ‘activate’) in order for the dialog to appear on top of everything else.

tell application "System Events"
	if (application process "iTunes" exists) then
		activate -- Bring System Events to the front.
		display dialog "iTunes is running. Shut down iTunes Now?" buttons {"Cancel", "Quit Now"} default button 2 with icon stop giving up after 10

		-- etc.
	end if
end tell

Another way is to tell some other application to display the dialog, but activating System Events seems to be the easiest thing for your script.

Thanks for your support.

I’ve released the SourceCode of my ‘Phils-iTunes-Library-Switcher’-AppleScript:

http://bbs.applescript.net/viewtopic.php?pid=47721#p47721

The script will easily switch between two different iTunes libraries. Nice for everyone who uses a notebook and has most if his (or hers) music on an external device.

Most recent version will be available here:
http://snapup.net/blog/2005/10/29/phils-itunes-library-switcher/

regards from berlin / germany

Model: iBook G3, 800 Mhz, 640 MB RAM
AppleScript: 1.10
Browser: Safari 412.5
Operating System: Mac OS X (10.4)