Apple Script- Some one better answer this

hypertron

Registered
Im trying to get iTunes to set a variable as its current track.
Code:
tell application "iTunes"
	activate
	set CTRACK to the current track
end tell
whats wrong with this?
-hypertron
 
Works fine for me, as long as there is an active track.However, if there's no song playing, then you'll get an error. To get around this, you should either put it within a TRY statement in your script, or check that iTunes isn't stopped. For example:

Code:
tell application "iTunes"
	try
		set CTRACK to the current track
	on error
		display dialog "Error: No active track!"
	end try
end tell

or:

Code:
tell application "iTunes"
	if the player state is not stopped then
		set CTRACK to the current track
	end if
end tell

Hope this helps.
 
Aww, I figured out what the problem is, i think, tho i have no idea how to fix it. I'm trying to get System Events t o type the song, but it doesnt want to do that.

Code:
tell application "System Events"
	keystroke return
	keystroke "iTunes is playing "
	keystroke CTRACK
	keystroke return
end tell
It still gives me the error. rawr...
 
The problem is that "the current track" is not a text value; it is of type 'track'. If you say "return the current track", you'll get something like this as a result: file track id 9165 of user playlist id 9157 of source id 45 of application "iTunes". Not text.

Instead of saying "set CTRACK to the current track", say: "set CTRACK to the name of the current track".

So, a completed script, with proper error handling, might look something like this:
Code:
tell application "iTunes"
	try
		set CTRACK to the name of the current track
		
		tell application "TextEdit" to activate
		
		tell application "System Events"
			keystroke return
			keystroke "iTunes is playing "
			keystroke CTRACK
			keystroke return
		end tell
	on error
		display dialog "An error occurred!"
	end try
end tell
That'll type the name of the current track into a document that's open in TextEdit.
 
Your thread title sounds like you're threatening us. Was that done on purpose?
 
alright, that worked, thanks, but how do i get the Artists name? Is that even possible?
Yeah, it's easy. Instead of saying "the name of the current track", just say, "the artist of the current track".

You should take a look at iTunes' scripting dictionary. In Script Editor, go to File > Open Dictionary. Alternatively, you can drag the iTunes application onto Script Editor in the Finder. Once the dictionary opens, look for the entry for "track" under "iTunes Suite". All the properties of tracks are listed there.
 
Back
Top