Can't detect iTunes error

I previously posted my script to burn cd with itunes from a filemaker database. It works great, but in testing a portion of the script to try to make it fail I uncovered a problem that I don’t understand. When I am running parallels software I can’t burn a CD with iTunes. iTunes gives the error “burner busy”. I click “OK” and my script hangs instead detecting the error.

does anyone have a suggestion. below is the subroutine to burn a cd

on Burn_the_CD(pList)

activate me
display dialog "Do not use this computer while iTunes is Burning the CD." & return & return & " Follow the instructions in the iTunes window to insert a Blank CD. If iTunes gives an error message then quit iTunes to reset." buttons {"I understand"} default button 1 with icon 1 giving up after 10
tell application "iTunes"
	activate
	try
		set view of front browser window to user playlist pList
		tell application "System Events"
			tell process "iTunes"
				click the menu item "Burn Playlist to Disc" of the menu "File" of menu bar 1
			end tell
		end tell
		-- set m to be a counter to give up after 10 minutes
		set m to 0
		repeat until m = 600
			--is itunes running or did user quit it
			if my test_itunes() then
				if (kind of container of view of front browser window is audio CD) then
					delay 2
					tell application "System Events"
						tell process "iTunes"
							click the menu item "Eject Disc" of the menu "Controls" of menu bar 1
						end tell
					end tell
					set addenda to "Disc burn successful. I will now delete the track from iTunes and return to your database. The voice note in your database has not been touched."
					set user_cancel to "CD Burn successful"
					return {addenda, user_cancel}
					exit repeat
				else
					delay 1
					set m to (m + 1)
				end if
			else
				set addenda to "iTunes has quit. I will now delete the track from iTunes and return to your database. The voice note in your database has not been touched."
				set user_cancel to "CD Burn canceled by user"
				return {addenda, user_cancel}
				exit repeat
			end if
		end repeat
		if m = 600 then
			set addenda to "Your CD was not burned because iTunes stopped responding. I will now delete the track from iTunes and return your database. The voice note in your database has not been touched."
			set user_cancel to "iTunes timed out"
			return {addenda, user_cancel}
		end if
	on error
		set addenda to "Your CD was not burned because of an unexpected error. I will now delete the track from iTunes and return to your database. The voice note in your database has not been touched."
		set user_cancel to "CD Burn error"
		return {addenda, user_cancel}
	end try
end tell

end Burn_the_CD

on test_itunes()
–this tests if the process “iTunes” is running
if (do shell script “ps ax”) contains “iTunes” then
set iTunesRunning to true
return iTunesRunning
else
set iTunesRunning to false
return iTunesRunning
end if
end test_itunes

Model: MacBook
Browser: Safari 419.3
Operating System: Mac OS X (10.4)

One suggestion - your try block is HUGE.

try
            set view of front browser window to user playlist pList
            tell application "System Events"
                tell process "iTunes"
                    click the menu item "Burn Playlist to Disc" of the menu "File" of menu bar 1
                end tell
            end tell
            -- set m to be a counter to give up after 10 minutes
            set m to 0
            repeat until m = 600
                --is itunes running or did user quit it
                if my test_itunes() then
                    if (kind of container of view of front browser window is audio CD) then
                        delay 2
                        tell application "System Events"
                            tell process "iTunes"
                                click the menu item "Eject Disc" of the menu "Controls" of menu bar 1
                            end tell
                        end tell
                        set addenda to "Disc burn successful. I will now delete the track from iTunes and return to your database. The voice note in your database has not been touched."
                        set user_cancel to "CD Burn successful"
                        return {addenda, user_cancel}
                        exit repeat
                    else
                        delay 1
                        set m to (m + 1)
                    end if
                else
                    set addenda to "iTunes has quit. I will now delete the track from iTunes and return to your database. The voice note in your database has not been touched."
                    set user_cancel to "CD Burn canceled by user"
                    return {addenda, user_cancel}
                    exit repeat
                end if
            end repeat
            if m = 600 then
                set addenda to "Your CD was not burned because iTunes stopped responding. I will now delete the track from iTunes and return your database. The voice note in your database has not been touched."
                set user_cancel to "iTunes timed out"
                return {addenda, user_cancel}
            end if
        on error
            set addenda to "Your CD was not burned because of an unexpected error. I will now delete the track from iTunes and return to your database. The voice note in your database has not been touched."
            set user_cancel to "CD Burn error"
            return {addenda, user_cancel}
        end try

You should limit the usage of try to a few lines of code, and certainly don’t use it with nested tells. The problem being, if something fails, you have no way of knowing what it was, and sometimes when you have nested tell blocks, the error won’t find its way up to the top level. Instead, often the script will just die a quiet death (or in your case, hang).

Also, you might want to think of just testing to see if Parallels is running and throw a dialog that the user should re-run the script after quitting Parallels.

thanks,

I see your point. So perhaps I should just put try block around commands that I think might fail.

Model: MacBook
Browser: Safari 419.3
Operating System: Mac OS X (10.4)

Exactly. If you use the “Error Handlers” item in the context menu in Script Editor, you can get a full tryon error block that will display the error number and message when the command in the try section fails. This is very useful to me for stuff like this.

So I rewrote the script. What do you think

on Burn_the_CD(pList, gwarn)

activate me
if gwarn = 1 then
	display dialog "Do not use this computer while iTunes is Burning the CD." & return & return & " Follow the instructions in the iTunes window to insert a Blank CD. If iTunes gives an error message then quit iTunes to reset." buttons {"I understand"} default button 1 with icon 1 giving up after 5
else
	display dialog "Do not use this computer while iTunes is Burning the CD." & return & return & " Follow the instructions in the iTunes window to insert a Blank CD. If iTunes gives an error message then quit iTunes to reset." buttons {"I understand"} default button 1 with icon 1
end if
tell application "iTunes"
	activate
	set view of front browser window to user playlist pList
	tell application "System Events"
		try
			tell process "iTunes"
				click the menu item "Burn Playlist to Disc" of the menu "File" of menu bar 1
			end tell
		on error
			set addenda to "Error with your Superdrive. Your CD has not been burned. I will now delete the track from iTunes and return to your database. The voice note in your database has not been touched."
			set user_cancel to "Super Drive Error"
			return {addenda, user_cancel}
		end try
	end tell
	-- set m to be a counter to give up after 10 minutes
	set m to 0
	repeat until m = 600
		--is itunes running or did user quit it
		if (my test_process("iTunes")) then
			try
				set Is_CD_Burn_Complete to (kind of container of view of front browser window is audio CD)
			on error
				set addenda to "iTunes reported an error with the CD. Your CD may be corrupted. I will now delete the track from iTunes and return to your database. The voice note in your database has not been touched."
				set user_cancel to "iTunes error discard CD"
				return {addenda, user_cancel}
				exit repeat
			end try
			
			if (Is_CD_Burn_Complete) then
				delay 2
				try
					tell application "System Events"
						tell process "iTunes"
							click the menu item "Eject Disc" of the menu "Controls" of menu bar 1
						end tell
					end tell
				on error
					set addenda to "Your CD was not burned because of an unexpected error. I will now delete the track from iTunes and return to your database. The voice note in your database has not been touched."
					set user_cancel to "CD Burn error"
					return {addenda, user_cancel}
				end try
				set addenda to "Disc burn successful. I will now delete the track from iTunes and return to your database. The voice note in your database has not been touched."
				set user_cancel to "CD Burn successful"
				return {addenda, user_cancel}
				exit repeat
			else
				delay 1
				set m to (m + 1)
			end if
		else
			set addenda to "iTunes has quit. I will now delete the track from iTunes and return to your database. The voice note in your database has not been touched."
			set user_cancel to "CD Burn canceled by user"
			return {addenda, user_cancel}
			exit repeat
		end if
	end repeat
	if m = 600 then
		set addenda to "Your CD was not burned because iTunes stopped responding. I will now delete the track from iTunes and return your database. The voice note in your database has not been touched."
		set user_cancel to "iTunes timed out"
		return {addenda, user_cancel}
	end if
	
end tell

end Burn_the_CD

on test_process(which_app)
–this tests if the process “iTunes” is running
if (do shell script “ps ax”) contains which_app then
set processRunning to true
return processRunning
else
set processRunning to false
return processRunning
end if
end test_process

Model: MacBook
Browser: Safari 419.3
Operating System: Mac OS X (10.4)