Quit iTunes when current track ends

The subject tells you what I want my script to do.
I’m really new to scripting, I started learning about scripts with this website, so be patient! I tried to write my own script. Here it is:


try
	tell application "iTunes" to set Canzone to the name of current track
	
on error
	display dialog "iTunes was not running!" giving up after 5
	tell me to quit
end try

on idle
	tell application "iTunes"
		if (name of current track) is not Canzone then
			display dialog "Chiudo iTunes?" buttons {"No", "Si"} default button "Si" giving up after 10
			if button returned of result = "Si" then
				quit
				tell me to quit
				
			end if
			
		end if
	end tell
	return 15
end idle


I saved it as an Application, with the option “Do not quit”. But when I run it, I get an error:“The variable Canzone is not defined”

I thought that this is because I defined it outside the “on idle” block, so I tried with a repeat statement instead of “on idle”, like this:

try
	tell application "iTunes" to set Canzone to the name of current track
	
on error
	display dialog "iTunes was not running!" giving up after 5
	tell me to quit
end try



repeat
	tell application "iTunes"
		
		if (name of current track) is not equal to Canzone then
			display dialog "Chiudo iTunes?" buttons {"No", "Si"} default button "Si" giving up after 10
			if button returned of result = "Si" then
				tell application "iTunes" to quit
				exit repeat
			end if
		else
			delay 15
			
		end if
	end tell
end repeat

Like this, it works, but only if I click on the “Si” button. Despite the "default button “Si” " if I wait 10 seconds the dialog appears again, and iTuns doesn’t quit.
Where is my mistake? I’m sorry, I know this is a stupid question but keep in mind I’m at the very beginning…:stuck_out_tongue:

Hi. Default button highlights a button and toggles its functionality with the return key. It doesn’t actually return a value, without your interaction; this remains the case even if the dialog dismisses itself after a set time.
Here is an alternate version:

tell application "iTunes" to if player state is playing then
	playpause
	play current track with once
-------------------------------------------------------------------------------------------
--do you just want to stop after playing (above) or actually quit?
	repeat while player state is playing
		delay 30
	end repeat
	quit
-------------------------------------------------------------------------------------------

end if

Thanks a lot!
I have another question (if you don’t mind):slight_smile:

I found another script, it checks if a specific application is running. If it is not, it displays a dialog with two buttons “Stop” and “Ok”
Stop quits the script and leaves the Mac running, Ok shuts it down.
But it has the :
default button “Ok” giving up after 30
option, and it actually works! After 30 seconds, if I don’t click a button the computer shuts down.
Why doesn’t it work in the script that I posted?

Edit:
Sorry again, problem solved :smiley:
Thanks for your help!