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.

(This space intentionally left blank because of the long table of contents over there on the right.)

 

Back to top

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"
Back to top

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
Back to top

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
Back to top

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
Back to top

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
Back to top

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
Back to top

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")
Back to top

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.

Back to top

Comments

Permalink

Hey there. I'm using some applescript in a shell script, and osascript -e lets you write the applescript right in the shell. But, it needs to be on one line. Do you know how i can use "try ... end try" in one line? I can't have errors appearing, because I'm logging something. or, do you know if there are ways, like the semicolon, to symbolize a new line in applescript but keep it on one line?

Have you tried putting a "\n" in your scripts where you want a newline? I do something like that in a Java program that runs scripts using osascript, and I can dig that up if need be. I'm pretty sure I just put "\n" wherever there would normally be a newline.