iTunes AppleScript examples (command examples)

iTunes AppleScript FAQ: Can you share some iTunes AppleScript examples (example scripts)?

As I've been working a lot with Mac speech recognition software lately, AppleScript has once again become important in my life. I've been writing a lot of small AppleScript scripts, including AppleScript iTunes scripts, and I thought I'd share snippets of code from those iTunes scripts here for anyone else they might benefit.

2021 Update: Wherever it says “iTunes” below, substitute the word “Music”, as Apple has renamed the iTunes application to Music.

AppleScript iTunes example - Play a playlist

To get started, here's an AppleScript iTunes script to play an iTunes playlist:

-- play the playlist named "My Top Rated"
tell application "iTunes"
  play the playlist named "My Top Rated"
end tell

If you haven't used AppleScript much before, note that you can abbreviate that script like this:

tell application "iTunes"
  play playlist named "My Top Rated"
end tell

or this:

tell application "iTunes"
  play playlist "My Top Rated"
end tell

or even this:

tell application "iTunes" to play playlist "My Top Rated"

More on AppleScript iTunes playlists

While I'm writing about AppleScript iTunes playlists, here is a way to get a list of the iTunes playlist names:

tell application "iTunes"
  get name of playlists
end tell

To get a count of the number of iTunes playlists:

tell application "iTunes"
  get count of playlists
end tell

Play an iTunes song by name with AppleScript

Here's one way to play a particular song in an iTunes playlist with AppleScript:

tell application "iTunes"
   play track "Not Ready to Make Nice" of playlist "Dixie Chicks"
end tell

iTunes tracks: Next, previous, play, pause

Here is a collection of one-line AppleScript iTunes commands that can be used:

tell app "iTunes" to stop
tell app "iTunes" to pause
tell app "iTunes" to play
tell app "iTunes" to playpause
tell app "iTunes" to play next track
tell app "iTunes" to play previous track

Some one-liners to get AppleScript iTunes track properties:

set current_track to the current track
set track_album to the album of the  current track
set track_artist to the artist of the current track
set track_comments to the comment of the current track
set track_name to the name of the current track

I found this on another website (sorry I don't remember its name, or I would link to it, but this AppleScript script gets a count of tracks in the current playlist because "the view property returns a reference to the selected playlist":

tell application "iTunes"
  get count of tracks of (get view of front window)
end tell

AppleScript to get the number of tracks in an album:

tell application "iTunes"
  count (tracks of (view of front window) whose album is "Greatest Hits")
end tell

How to play a track by number (assuming you know the number of the track and playlist you want to play):

tell application "iTunes"
  play track 10 of playlist 1
end tell

AppleScript iTunes volume control

Here is a short collection of AppleScript iTunes volume commands. First, to get the current iTunes volume:

tell application "iTunes"
  set currentVolume to sound volume
end tell

This command can be used to decrease the Mac system volume (not the iTunes volume, but this is more or less related to iTunes AppleScript scripts):

-- takes an argument like 10 or 20 to decrease the volume
on run argv
  set decrease to item 1 of argv
  set current_volume to output volume of (get volume settings)

  if current_volume is greater than 0 then
  	set current_volume to current_volume - decrease
  end if
  set volume output volume current_volume

  say "Decreased volume"
end run

and this opposite script can be used to increase Mac system volume from AppleScript:

-- takes an argument like 10 or 20 to increase the volume

on run argv
  set increase to item 1 of argv
  set current_volume to output volume of (get volume settings)

  if current_volume is less than 100 then
  	set current_volume to current_volume + increase
  end if
  set volume output volume current_volume

  say "Increased volume"
end run

iTunes searching with AppleScript: Search and play

Here's an AppleScript iTunes search script that searches an iTunes playlist for songs by the artist named Bob Seger:

tell application "iTunes"
  set search_results to (search playlist "My Favorites" for "Bob Seger" only artists)
end tell

You can then extend this iTunes search example with a "search and play" script like this:

tell application "iTunes"
  set search_results to (search playlist "Music" for "Bob Seger" only artists)
  repeat with t in search_results
    play t
  end repeat
end tell

Or if you prefer your playlists by number, this script will work:

tell application "iTunes"
  set search_results to (search library playlist 1 for "Bob Seger" only artists)
  repeat with t in search_results
    play t
  end repeat
end tell

Here's a similar AppleScript iTunes search and play example:

tell application "iTunes"
  activate
  set results to (every file track of playlist "Library" whose name contains "Young at Heart" and artist contains "Bennett")
  repeat with t in results
    play t
  end repeat
end tell

If you prefer working with iTunes playlists by number (which might be easier in a long, complicated script), you can search this way instead:

tell application "iTunes"
  set search_results to (search library playlist 1 for "Bob Seger" only artists)
end tell

AppleScript “radio” script: Play a radio station

I couldn't find a great way to play radio stations out of the Radio Stations playlist that ships with iTunes, but I did find a workaround. The workaround is:

  • Create a new playlist named something like "Radio"
  • Drag the radio stations you want to be able to control from the existing iTunes Radio Stations list to your new "Radio" playlist.
  • Use a script like this to play them:
-- AppleScript iTunes radio station example
tell application "iTunes" to play (first track of playlist "Radio" whose name is "ESPN 1300")

or this:

-- AppleScript iTunes radio station example
tell application "iTunes" to play (first track of playlist "Radio" whose name contains "The Score")

AppleScript/iTunes summary

I hope this list of iTunes AppleScript examples has been helpful. If you have any iTunes AppleScript examples you'd like to share, please add them to the Comments section below.