First time scripting iTunes

I searched and couldn’t find an answer to this.

I have never scripted iTunes before but wanted what I thought would be a simple script. I want to get all song names in my main library where the song name is the same as the artist. Like “Bad Company” by Bad Company, “Black Sabbath” by Black Sabbath, etc. I thought this would be a one liner but I can’t get it to work.

tell application "iTunes"
 	get tracks of library playlist 1 whose album = name
end tell

Checking the iTunes scripting dictionary, there doesn’t seem to be a name attribute under track. Any ideas?

Hi Matt-Boy,

If you happen to own a large music collection I would not recommend to opt for a pure AppleScript solution, as you will have to iterate over all file tracks available in your library playlist. And this will take quite a while…

Therefor I suggest an approach that includes parsing the iTunes XML library file with the help of PyObjC.

Just download this tiny Python script to your desktop and execute the following AppleScript code in the Script Editor:


set pyscriptpath to POSIX path of (((path to desktop) as Unicode text) & "albumartist.py")
set persistentids to paragraphs of (do shell script "/usr/bin/python/ " & quoted form of pyscriptpath)

tell application "iTunes"
	repeat with persistentid in persistentids
		set foundtrack to (every track in library playlist 1 whose persistent ID is persistentid)
		-- further process foundtrack
	end repeat
end tell

Of course, this can be further enhanced and modified, but shows the general idea. BUT it requires Mac OS X 10.5. Or you need to install PyObjC in case you are running 10.4…

Maybe this will help you? I hope so!

Tested on Mac OS X 10.5.4 & iTunes 7.7.1

Interesting. I hadn’t thought of reading the XML file directly. I have scripts that read XML using system events. Maybe I can experiment with that. I don’t have 10.5 yet so I it sounds like I would need to download that python script.

For the sake of argument, what if I did want to just use iTunes scripting dictionary. I am still curious how to do it even if the script took a while to run.


tell application "iTunes"
	set filetracks to every file track in playlist 1
	repeat with filetrack in filetracks
		if (name of filetrack) is equal to (album of filetrack) then
			-- further process the track
		end if
	end repeat
end tell

When specifying objects, other properties can be inherited from the parent class.

If you’re using Leopard, then I would encourage you to check out the “Show inherited items in dictionary viewer” preference in Script Editor.

Tracks inherit from the item class, which has a name property.

Thanks for all the replies. I am still in Tiger but moving to Leopard soon. The inherited items feature sounds great.

-Matt

Model: PowerPC G5 Tower OS 10.4.8
AppleScript: XCode 2.5
Browser: Safari 419.3
Operating System: Mac OS X (10.4)

Um, I think this would be the right place to post this. sweatdrop Um, I’m writing a script suite for my own uses, and I have a “Launcher”, if you will, that I built for iTunes. Now, my problem. I need to get AppleScript to tell iTunes to get the first checked track in the current playlist. I’ve gone through the Scripting dictionary, to no avail. Can anyone help me with this?

Model: eMac
AppleScript: 1.9.1
Browser: Firefox 2.0.0.16
Operating System: Mac OS X (10.2.x)

Hi,

checked means enabled in the dictionary


tell application "iTunes"
	get 1st track of current playlist whose enabled is true
end tell

Mille grazi, Stefan. I was wondering how I’d get around it.

Um, about that scriptlet. I keep getting this when it runs:

“-------------Execution Error-------------------
iTunes got an error: Can’t get track 1 of
current playlist whose enabled = true.”

Now, it worked ONCE before.

On my Mac StefanK’s code only runs successful when iTunes is currently playing a song. If iTunes is not playing (e.g. right after it opened), I also get this error, because then «current playlist» is an «unknown object type».

Nevertheless, if you specify the playlist, then the code always works:


tell application "iTunes"
	get 1st track of playlist 1 whose enabled is true
end tell

So, in other words, I can’t have iTunes launch and play right off the bat?

EDIT: I got it to successfully launch and start playing with that script once…maybe it was a bug or something.

Well, if iTunes is already running and playback of a current track is paused, then the script will run successfully on my Mac. But if iTunes is not running and opened by the script, then it fails (because there is no «current playlist» after startup).

But of course you can launch iTunes and tell your script to start playing tracks in a certain playlist:


tell application "iTunes"
	set firsttrack to 1st track of playlist "They Might Be Giants" whose enabled is true
	play firsttrack
end tell

After launching iTunes, no track is targeted, so there is no current track.
The description of current playlist in the dictionary is
the playlist containing the currently targeted track,
this is not the currently selected playlist

Wham, Bam, Thank you , Man! Worked like a charm.

EDIT: Also, is there a way to make it recognize the name of the last played playlist?

EDIT: Found out how to do it:
and this makes it launch and play the first enabled track. Hope it helps someone :slight_smile:

tell application "System Events"
	set checkRun to (name of processes) contains "iTunes"
	if (checkRun is false) then
		tell application "Finder"
			launch application "iTunes"
			set visible of every process whose name is "iTunes" to false
			tell application "iTunes"
				tell source "Library"
					tell playlist "Library"
						set theTrack to the 1st track whose enabled is true
						play theTrack
					end tell
				end tell
			end tell
		end tell
	end if
end tell