getting track info when there is none

so in my program there is a section of code that when you click a button it displays the current track title,artist and album in a dialog box.
It all works fine except when there is no song playing…i don’t mean paused or stopped, i mean right when you open iTunes there is no song playing and I want to have the dialog box say No Song playing, but when you run the program I just get the error that iTunes can’t get current track name.
Heres my code right now fo this:

if the name of theObject is equal to "get_info" then
		tell application "iTunes"
			set thetitle to (name of current track)
			set theartist to (artist of current track)
			set thealbum to (album of current track)
			if thetitle is "" then
				set thetitle to "No song playing"
				set theartist to ""
				set thealbum to ""
			else if theartist is equal to "" then
				set thetitle to (name of current track)
				set theartist to "Unknown Artist"
				set thealbum to (album of current track)
			else if thealbum is equal to "" then
				set thetitle to (name of current track)
				set theartist to (artist of current track)
				set thealbum to "Unknown Album"
			end if
		end tell
		output(thetitle & return & theartist & return & thealbum)
	end if
end clicked
---this prompts the track info dialog----
on output(str)
	display dialog str
end output

i also need help so that when you quit iTunes it quits my program as well. Thanks!

maybe you could simply test for a current track in a try/on error:


tell application "iTunes"
	try
		get current track
		-- ... get name, title etc ...
	on error
		display dialog "no song playing"
	end try
end tell

Expanding’s on Dominik’s suggestion:

on clicked theObject
	if the name of theObject is equal to "get_info" then
		try
			tell application "iTunes"
				tell current track
					set theTitle to name
					set theArtist to artist
					set theAlbum to album
				end tell
				
				if theArtist is "" then set theArtist to "Unknown Artist"
				if theAlbum is "" then set theAlbum to "Unknown Album"
			end tell
			
			output(theTitle & return & theArtist & return & theAlbum)
		on error
			display dialog "No song playing." buttons {"OK"} default button 1
		end try
	end if
end clicked

---this prompt's the track info dialog----
on output(str)
	display dialog str
end output

Hi,

Another thing you can do is use the ‘player state’ property. Something like this:


tell application "iTunes"
	if player state is stopped then
		set the_prompt to "No track playing."
	else
		set {name:the_name, artist:the_artist, album:the_album} to current track
		set the_prompt to the_name & return & the_artist & return & the_album
	end if
end tell
display dialog the_prompt

gl,

Ok, so I used Bruces method and it worked fine. Thnaks for that. Now I was wondering if there is a way that when iTunes is quit it quits my application as well.