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?
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…
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
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)
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
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
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
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