dougscripts.com

Commonly Used iTunes Commands 1 2 3 

Here are some of the commands available to use with iTunes. This is just a quick primer to get you started.

Commands from the Required Suite

close
Close a window:

tell application "iTunes"

close EQ window 1

end tell


tell application "iTunes"

close every playlist window

end tell

 

count
How many of whatever object is specified, most helpful for counting tracks and playlists. Example uses:

tell application "iTunes"

get count of playlists

end tell


tell application "iTunes"

get count of tracks of (get view of front window)

end tell


(* the view property returns a reference to the selected playlist *)


tell application "iTunes"

count (tracks of (view of front window) whose album is "Greatest Hits")

end tell


(* the snippet below will count only those tracks whose album name contains the word "hits": *)


tell application "iTunes"

count (tracks of (view of front window) whose album contains "hits")

end tell


tell application "iTunes"

repeat with x from 1 to count tracks of (view of front window)

(* do something with track x of the selected playlist *)

end repeat

end tell

 

delete
Removes whatever object is specified, usually a track or playlist, without confirmation; this command does not delete files from your computer. CAUTION: when you delete a track from the main library (library playlist 1), the track will also be removed from any playlists which contain it. Example uses:

-- delete the selected playlist...CAREFUL!


tell application "iTunes"

delete (view of front window)

end tell


-- delete a track...CAREFUL!


tell application "iTunes"

delete track 7 of (view of front window)

end tell


-- delete a track from iTunes entirely...CAREFUL!


tell application "iTunes"

delete track 7 of library playlist 1

end tell

 

duplicate
This is the command used to copy a track or tracks from one playlist (or source) to another:

tell application "iTunes"

duplicate track 5 of library playlist 1 to playlist "Mom's Faves"

end tell


tell application "iTunes"

repeat with aTrack in selection

duplicate aTrack to playlist "Mom's Faves"

end repeat

end tell


The direct object of duplicate must be a reference to a track or tracks. And even though the selection object works, a list of track references does not.

-- this works

tell application "iTunes"

duplicate selection to playlist "Mom's Faves"

end tell


-- this does not work

tell application "iTunes"

set my_Nirvana_tracks to (every track of library playlist 1 whose album is "Nevermind")

duplicate my_Nirvana_tracks to playlist "Nirvana - Nevermind"

end tell


-- work around

tell application "iTunes"

duplicate (every track of library playlist 1 whose album is "Nevermind") to playlist "Nirvana - Nevermind"

end tell


-- or

tell application "iTunes"

set my_Nirvana_tracks to (every track of library playlist 1 whose album is "Nevermind")

repeat with a_track in my_Nirvana_tracks

duplicate a_track to playlist "Nirvana - Nevermind"

end repeat

end tell

 

make
Creates a new object, in iTunes's case a new user playlist. That's all. It's the scriptable version of the "New Playlist" option in the "File" menu. It also returns a reference to the new playlist. Example use (the "new" is optional):

tell application "iTunes"

make new user playlist

end tell


tell application "iTunes"

set myNewPlaylist to (make new user playlist)

end tell


-- note how some or all playlist properties can be set in a record:


tell application "iTunes"

make new user playlist with properties {name:"Mom's Favorites", shuffle:false, song repeat:one}

end tell

 

open
Use this command to add a file to a Master Library ("Music", "Audiobooks", and so on, depending on the kind of track) and start play. Requires the alias of the file to open, which suggests you might want to use the AppleScript "choose file" command.

tell application "iTunes"

open (choose file)

end tell

 

Commands from the iTunes Suite

add
Add a new track or tracks to the iTunes library. Audio CD tracks will be imported to your computer.
The add command requires the alias of the file(s) in a list (lists are enclosed by brackets and the values in a list are separated by commas). You can also designate an extant playlist to copy the track to:

tell application "iTunes"

-- 'choose file' returns an alias

set newFile to (choose file with prompt "Select a song to add...")

add newFile to playlist "Check These Out!"

end tell

The add command returns a reference to the track added, so:

tell application "iTunes"

set newFile to (choose file with prompt "Select a song to add...")

set new_track to (add newFile to playlist "Check These Out!")

set comment of new_track to "Some comment for this example"

end tell

 

convert
Converts a list of tracks using the currently set encoder, and returns a reference to the converted track.

tell application "iTunes"

convert selection

end tell


tell application "iTunes"

repeat with aTrack in (every track of (get view of front window))

convert aTrack

end repeat

end tell

 

play
Will play the current track (start the player) or play the object specified. However, in the latter regard, the object can be a track or playlist identified by index number or name, or an audio file's filepath. Here are several examples:

-- by index number...


tell application "iTunes"

play track 16 of playlist 1

end tell


-- by name...


tell application "iTunes"

play track "Running Two" of playlist "Lola"

end tell


-- playlist by name...


tell application "iTunes"

play playlist "Lola"

end tell


tell application "iTunes"

play the playlist named "Lola"

end tell


-- by filename...


tell application "iTunes"

play "Main:Sound:MP3s:Lola:Running Two.mp3"

end tell

 

pause, playpause, and stop
I think you can guess what these do. Like other player-oriented commands, they appear alone on a command line and do not refer to any objects.

tell application "iTunes"

stop

end tell


-- or


tell application "iTunes"

pause

end tell


-- or toggle from one to the other state...


tell application "iTunes"

playpause

end tell

 

next track and previous track
These two commands allow you to skip to the next or previous track in the current playlist. The "base track"—if you will, the track from which to advance or regress from—needn't be playing, but it must be the current track in the player. No result is returned, so you don't know what "next track" is unless you tap current track. The syntax is as simple as it gets (either command would appear alone on a command line and does not refer to any objects):

tell application "iTunes"

next track

end tell


-- perhaps you would use it like this:


tell application "iTunes"

if (get comment of current track) is "Don't Play This" then

next track

end if

end tell

 

open location

Connects to an internet streaming audio source. Just supply the URL of the site:

tell application "iTunes"

open location "http://shiva.6o4.net:8605"

end tell

 

reveal

Display a track and select it in iTunes.

tell application "iTunes"

if player state is not stopped then

reveal current track

end if

end tell

search

Search a given playlist for tracks meeting the supplied criteria. You can search by album, artist, composer, or song name; or the displayed songs, or everywhere.

tell application "iTunes"
	-- search the Recently Played playlist for tracks whose artist name is "The Clash"
	set my_search_results to (search playlist "Recently Played" for "The Clash" only artists)
end tell

tell application "iTunes"
	-- search everywhere for songs containing "jam"
	set my_search_results to (search library playlist 1 for "jam")
end tell

update

Have a specified device sync immediately. This will typically be an iPhone or iPod and will only work if the device is set to automatically sync and not "manually manage". Here is a simple example which updates the single mounted device (additional spefific devices can be referenced by name, for example):

tell application "iTunes"

update (some source whose kind is iPod)

end tell


< prev | 1 2 3 
Site contents © 2001 - 2024 (that's right: 2001) Doug Adams and weblished by Doug Adams. Contact support AT dougscripts DOT com. About.
All rights reserved. Privacy.
AppleScript, iTunes, iPod, iPad, and iPhone are registered trademarks of Apple Inc. This site has no direct affiliation with Apple, Inc.
The one who says "it cannot be done" should not be interrupting the one who is doing it.