AppleScript

Script ClassicTunes with AppleScript, with example scripts and a complete reference to its dictionary of classes, properties, commands and enumerations.

ClassicTunes can be scripted with AppleScript. Scripts can see and edit the songs in your library, play music, sync, preview and back up iPods, sync other music players, look up covers and details online, and more. Anything you can script, you can also run from the Script menu, from Automator, or from another app.

To see the dictionary for yourself, open Script Editor and choose File › Open Dictionary…, then ClassicTunes.

Note: On macOS 13 Ventura and later, most of these things are also actions in the Shortcuts app. See Shortcuts.

Before you start

  • The first time a script from another app (such as Script Editor) talks to ClassicTunes, macOS asks whether that app may control ClassicTunes. Click OK. You can change your answer later in System Settings › Privacy & Security › Automation.
  • Scripts work on the library that's open in ClassicTunes now. library name tells you which one that is.
  • Commands that take a while (backing up an iPod, looking up covers for a whole library, importing a full iPod) keep the script waiting until they finish. AppleScript gives up waiting after two minutes unless you say otherwise, so wrap them in with timeout of … seconds, as the examples below do.

Scripts only sync iPods set up with this library

A script can only sync an iPod that has already been synced with the library that's open, from inside ClassicTunes. This keeps a script from replacing the music on an iPod that belongs to another library, or one that's never been set up. If you ask a script to sync an iPod that isn't set up yet, it stops with the error "That iPod isn't set up with this library yet. Sync it once from ClassicTunes first." With no iPod named, sync syncs every connected iPod that belongs to this library, and skips the rest.

A script's sync is still careful in the same ways as a sync you start in the app. Before an iPod's first sync, or a sync that would remove a lot, ClassicTunes offers a snapshot first (if that's turned on in Settings › Backups), and the sync waits until you answer in the app.

Example scripts

Sync your iPod and eject it when it's done

tell application "ClassicTunes"
    if (count of iPods) is 0 then return "No iPod is connected."
    set myIPod to iPod 1
    sync myIPod
    delay 2
    repeat while syncing of myIPod
        delay 5
    end repeat
    eject myIPod
end tell

sync returns as soon as the sync has started, so the script checks syncing until it's finished. If ClassicTunes asks about taking a snapshot first, syncing stays false until you answer, so answer before the script moves on.

To sync every connected iPod that belongs to this library, use sync on its own:

tell application "ClassicTunes" to sync

Play a playlist

tell application "ClassicTunes"
    play playlist "Bedtime"
    set sound volume to 40
end tell

See what a sync would do before you run it

tell application "ClassicTunes"
    set myIPod to iPod 1
    with timeout of 600 seconds
        set report to preview sync myIPod
    end timeout
end tell
display dialog report buttons {"Cancel", "Sync Now"} default button "Sync Now"
tell application "ClassicTunes" to sync myIPod

preview sync works out what syncing would add, convert, remove and change, without changing anything on the iPod, and returns it as a few sentences.

Find tracks

tell application "ClassicTunes"
    -- Names of your highly rated jazz songs
    get name of every track whose genre is "Jazz" and rating ≥ 80

    -- Songs you've never played, added in the last 30 days
    set newSongs to every track whose played count is 0 and date added > ((current date) - 30 * days)

    -- Give every Blink-182 song five stars
    set rating of (every track whose artist is "Blink-182") to 100
end tell

Back up an iPod before changing it

tell application "ClassicTunes"
    with timeout of 7200 seconds
        set takenAt to back up iPod 1
    end timeout
end tell
display notification "Snapshot taken " & (takenAt as text)

The first snapshot of a full iPod can take an hour; later ones only copy what changed.

Add a folder of music and fill in missing details

set newMusic to POSIX file "/Users/Shared/New Music"
tell application "ClassicTunes"
    with timeout of 1800 seconds
        set added to import newMusic with filling in missing details
    end timeout
end tell
display dialog (added as text) & " songs added."

Choose the music an iPod syncs

tell application "ClassicTunes"
    set myIPod to iPod 1
    set syncs entire library of myIPod to false
    set chosen artists of myIPod to {"Radiohead", "Björk"}
    set chosen albums of myIPod to {"Miles Davis — Kind of Blue"}
    set chosen playlists of myIPod to {"Running"}
    set fill free space of myIPod to unplayed songs
end tell

Reference

ClassicTunes' dictionary has the Standard Suite that most Mac apps share (open, close, quit, count, exists, windows and so on), and the ClassicTunes Suite, described here.

Types in the tables: text, integer (a whole number), real (a number that can have decimals), boolean (true or false), date, file, and list of text. Properties marked read-only can be read but not set.

application

ClassicTunes itself. These properties and elements are used inside tell application "ClassicTunes".

Elements

ElementWhat it is
tracksEvery song, video and audiobook in the open library.
playlistsEvery playlist and smart playlist in the open library.
iPodsThe iPods connected now.
music playersThe connected music players that aren't iPods (set up with iPod › Set Up a Music Player…).

You can refer to them by number (iPod 1), by name (playlist "Bedtime"), by id (track id "…"), or with whose (every track whose artist is "Prince"). All four are read-only: scripts can change tracks and playlists, but not add or delete them.

Properties

PropertyTypeDescription
selectionlist of tracks, read-onlyThe tracks selected in the window: the songs in the selected albums, or the selected songs.
current tracktrack, read-onlyThe track playing or paused now, if it's in the library.
player stateplayer state, read-onlystopped, playing or paused.
player positionrealSeconds into the current track. Set it to jump to that point.
sound volumeintegerThe player's volume, 0 to 100.
audiobook speedrealPlayback speed for audiobooks, from 0.75 to 2 (1 is normal speed). The pitch stays the same.
library nametext, read-onlyThe name of the open library.
scrobblingbooleanWhether what's played (on the Mac, the iPod and Rockbox) is sent to ListenBrainz. Plays are only sent once an account is set up in Settings › Scrobbling.
ListenBrainz accounttext, read-onlyThe ListenBrainz user plays are sent to, or empty text when no account is set up.
plays waitinginteger, read-onlyHow many plays are waiting to be sent to ListenBrainz.
automatically checks for updatesbooleanWhether ClassicTunes checks for a newer version once a day. Always false in a copy that can't update.
licensedboolean, read-onlyWhether ClassicTunes is licensed (also true in a copy that doesn't need a license).
license emailtext, read-onlyThe email address the license belongs to, or empty text when not licensed.
trial days leftinteger, read-onlyDays left in the free trial: 0 once it has ended, and -1 when licensed.

The application also responds to play: play on its own carries on with what's paused, or starts the first song in the library if nothing is playing.

track

A song, video or audiobook in the library. Changes you make to a track's details are written into its file's tags, as they are when you edit them in Get Info, and go to iPods at their next sync. Setting a text property to "" clears it.

PropertyTypeDescription
idtext, read-onlyThe track's unique id. It doesn't change.
nametextThe title.
artisttext
albumtext
album artisttext
genretext
composertext
commenttext
yearinteger0 when there's none.
track numberinteger
track countintegerHow many tracks the album (or disc) has.
disc numberinteger
disc countinteger
ratinginteger0 to 100, in steps of 20: one star is 20, five stars is 100. Other values are rounded to the nearest star.
played countinteger, read-onlyHow many times it's been played, including plays on iPods.
played datedate, read-onlyWhen it was last played (missing value if never).
date addeddate, read-onlyWhen it was added to the library.
durationreal, read-onlyLength in seconds.
media kindmedia kindsong, video or audiobook. Setting it moves a track between Music and Audiobooks; videos can't be changed to another kind, and songs and audiobooks can't be made videos.
video kindvideo kindFor a video: movie, TV show, music video or home video (where it goes in the iPod's Videos menu). Only videos have one.
showtextFor a TV show: the show it's an episode of.
season numberintegerFor a TV show: its season.
episode numberintegerFor a TV show: its episode.
explicitbooleanWhether the track has explicit lyrics (shown with an E after its title). Setting it writes the advisory into AAC and Apple Lossless files, and goes to iPods at their next sync. Setting it to false clears the mark.
syncbooleanTrue when every iPod and music player syncs the track. Setting it leaves the track off, or puts it back on, all of them at once. Each iPod's and player's own choices are on its page.
bookmarkrealWhere playback carries on from, in seconds, for audiobooks and tracks that remember their place. Set it to 0 to start from the beginning.
locationfile, read-onlyThe track's file.
iPod compatibleboolean, read-onlyWhether this library's iPods can play the file as it is. See iPod › Find Songs iPods Can't Play….
iPod conversioniPod conversion, read-onlyHow iPods get the track: none (the default), at every sync or kept copy. Change it with convert for iPods.
artwork sizeinteger, read-onlyThe width of the cover in pixels, or 0 when it has none.

Tracks respond to play. play track 3 plays one track; play (every track whose album is "Abbey Road") plays all of them in order, as one queue.

playlist

A playlist or smart playlist.

PropertyTypeDescription
idtext, read-onlyThe playlist's unique id.
nametextThe playlist's name. It can't be set to empty text.
smartboolean, read-onlyWhether it's a smart playlist.
syncbooleanWhether the playlist goes to every iPod and music player that syncs the entire library. Setting it changes all of them.
ElementWhat it is
tracksThe playlist's tracks, in order. For a smart playlist, the tracks that match its rules now. Read-only.

Playlists respond to play (an empty playlist gives the error "The playlist is empty.") and export.

iPod

A connected iPod.

PropertyTypeDescription
idtext, read-onlyThe iPod's id.
nametext, read-onlyThe iPod's name.
modeltext, read-onlyThe model, such as "iPod classic 80 GB".
capacityreal, read-onlyThe size of its disk, in bytes.
free spacereal, read-onlyFree space, in bytes.
syncingboolean, read-onlyWhether it's syncing now.
syncs entire librarybooleanWhether the whole library is synced. When false, the chosen artists, albums, genres and playlists are synced instead.
chosen artistslist of textArtists chosen to sync, by name.
chosen albumslist of textAlbums chosen to sync, each written as "Artist — Album". "Artist - Album" with a hyphen works too.
chosen genreslist of textGenres chosen to sync.
chosen playlistslist of textPlaylists chosen to sync, by name.
fill free spacefill choiceWhat fills the rest of the space after what's chosen (like iTunes' Autofill). Only used when syncs entire library is false.
syncs all photosbooleanWhether every photo in the Photos app goes to the iPod, as well as the chosen albums and periods.
chosen photo albumslist of textPhotos app albums (Favorites too) and ClassicTunes photo albums chosen for the iPod, by name.
chosen photo periodslist of textPeriods by when photos were taken, written as "2019" (a year) or "2019-07" (a month).
photo space limitrealThe most space photos may use on the iPod, in bytes. 0 means no limit.
photo videosbooleanWhether the chosen albums' and periods' videos go to the iPod's Videos menu too, converted for the iPod.
photo video space limitrealThe most space those videos may use, in bytes (0 for no limit). The newest go first.
photos only on this MacbooleanLeave out photos whose originals are only in iCloud.
last backupdate, read-onlyWhen the newest snapshot of the iPod was taken (missing value when there's none).
snapshot countinteger, read-onlyHow many snapshots of the iPod there are.
gameslist of text, read-onlyThe names of the click wheel games on the iPod (Apple's games, as iTunes put them there).
rockboxboolean, read-onlyWhether Rockbox is installed on the iPod. When it is, playlists are also written for Rockbox, and its play log is read back at each sync.
rockbox gameslist of text, read-onlyThe names of the games Rockbox has installed (empty without Rockbox).

The choices you set here are the same ones as on the iPod's page under Sync Options, and they're saved with the open library. Setting any of them when the iPod isn't connected gives the error "That iPod isn't connected."

iPods respond to sync, preview sync, back up, add to library and eject.

music player

A connected music player that isn't an iPod, set up with iPod › Set Up a Music Player… (for example a Shanling Q2, a Tangara, a Rockbox player or an SD card).

PropertyTypeDescription
idtext, read-onlyThe player's id.
nametext, read-onlyThe player's name.
kindtext, read-onlyThe kind of player it was set up as (Shanling Q2, Tangara, Rockbox…).
capacityreal, read-onlyThe size of its storage, in bytes.
free spacereal, read-onlyFree space, in bytes.
syncingboolean, read-onlyWhether it's syncing now.
syncs entire librarybooleanWhether the whole library is synced. When false, the chosen artists, albums, genres and playlists are synced instead.
chosen artistslist of textArtists chosen to sync, by name.
chosen albumslist of textAlbums chosen to sync, each written as "Artist — Album".
chosen genreslist of textGenres chosen to sync.
chosen playlistslist of textPlaylists chosen to sync, by name.
fill free spacefill choiceWhat fills the rest of the space after what's chosen. Only used when syncs entire library is false.
rockbox gameslist of text, read-onlyThe names of the games Rockbox has installed (empty without Rockbox).

Music players respond to sync and eject. Unlike iPods, a music player doesn't have to belong to the open library to be synced by a script.

Commands

play

Plays a track, several tracks, or a playlist, or carries on with what's paused.

play [track, list of tracks or playlist]
  • With a track: plays it.
  • With several tracks (play (every track whose …)): plays them in order, as one queue.
  • With a playlist: plays its tracks in order.
  • On its own: carries on if something is paused, or starts the first song in the library if nothing is playing.

pause

Pauses playback.

playpause

Plays if paused, pauses if playing.

start playing

Starts what Settings › General › Play key › Play chooses for this library (carry on where you left off, shuffle all songs, a playlist, recently added, or your current audiobook), or carries on if something is paused.

stop

Stops playback.

next track

Skips to the next track.

previous track

Goes back to the previous track.

sync

Syncs an iPod (or every connected iPod) that's already set up with this library, or syncs a music player.

sync [iPod or music player]
  • With an iPod: syncs it, if it belongs to the open library.
  • With a music player: syncs it.
  • On its own: syncs every connected iPod that belongs to the open library.

sync returns straight away; check the iPod's syncing property to see when it's finished. A first or large iPod sync asks about a snapshot in the app first. Errors: "No iPod is connected.", "That iPod isn't set up with this library yet. Sync it once from ClassicTunes first.", and "None of the connected iPods are set up with this library yet. Sync them once from ClassicTunes first."

preview sync

Works out what syncing the iPod would do, without changing anything on it.

preview sync iPod

Result: text. A few sentences: what's added (and converted first), what's removed, the playlists that change, and the space left afterward (or how much more is needed). Podcast episodes still to download are counted, not downloaded.

back up

Saves a snapshot of the whole iPod. Only what changed since its last snapshot is copied.

back up iPod

Result: date. When the snapshot was taken. A first snapshot of a full iPod can take an hour, so use a long timeout.

add to library

Copies everything on the iPod into the library: its songs, videos and audiobooks with their covers, play counts, ratings and other details, its playlists (On-The-Go and smart ones too), and its podcasts as subscribed shows. Songs already in the library are left as they are. Nothing on the iPod changes.

add to library iPod

Result: integer. How many items were added (songs, videos, audiobooks and podcast episodes). A full iPod takes a while, so use a long timeout.

eject

Ejects an iPod or music player.

eject iPod or music player

If the iPod is busy (for example, Finder has a window open on it), the script gets an error saying what to do.

import

Adds files or folders to the library.

import file or list of files [with filling in missing details]
ParameterTypeDescription
(direct)file or list of filesThe files or folders to add.
filling in missing detailsboolean, optionalAfterward, look the added songs' albums up on Apple and MusicBrainz and, where an album clearly matches, fill in what its songs are missing (year, genre, track and disc numbers, album name, artist) and a cover if it has none. Details already in the files are never changed. False when left out.
  • An .m3u or .m3u8 playlist file becomes a playlist; its songs that aren't in the library yet are added.
  • An OPML file subscribes to the podcasts it lists.
  • Files are copied into the library or left where they are, as Settings › General › Adding songs › Copy files into the library when adding them says. Scripts add straight away, without the window that shows what will be added.

Result: integer. How many items were added (songs, or podcasts for an OPML file).

export

Saves a playlist as an .m3u8 file, which most music apps and players open.

export playlist to file
ParameterTypeDescription
(direct)playlistThe playlist.
tofileWhere to save it.
set destination to POSIX file "/Users/Shared/Road Trip.m3u8"
tell application "ClassicTunes" to export playlist "Road Trip" to destination

export podcasts

Saves your podcast subscriptions as an OPML file, which other podcast apps import.

export podcasts to file
ParameterTypeDescription
tofileWhere to save it.

Result: integer. How many podcasts were saved.

check for new episodes

Checks every podcast for new episodes and, unless told not to, downloads the episodes each show keeps for iPods.

check for new episodes [downloading boolean]
ParameterTypeDescription
downloadingboolean, optionalWhether to download the episodes each show keeps. When left out, Settings › General › Podcasts › Download new episodes automatically decides. without downloading only checks.

Result: integer. How many new episodes were found. Many podcasts take a while, so use a long timeout.

find missing artwork

Looks online (Apple and MusicBrainz) for covers of albums that have none, and adds a cover only where the title, artist and year clearly match.

find missing artwork [track or list of tracks]
ParameterTypeDescription
(direct)track or list of tracks, optionalTracks whose albums to check. When left out, every album without a cover.

Result: integer. How many albums got a cover.

find better artwork

Looks online for larger copies of album covers, and replaces a cover only where the same artwork is found larger.

find better artwork [track or list of tracks]
ParameterTypeDescription
(direct)track or list of tracks, optionalTracks whose albums to check. When left out, every album with a cover.

Result: integer. How many albums got a better cover. Checking many albums takes a while, so use a long timeout.

look up and replace info

Looks albums up on Apple and MusicBrainz, and replaces their info (title, artist, year, genre, song titles and track numbers, in the files too where their format allows) only where the release clearly matches: most of the album's songs, by the same artist. A backup of the library's info is saved first.

look up and replace info [track or list of tracks] [adding covers boolean]
ParameterTypeDescription
(direct)track or list of tracks, optionalTracks whose albums to look up. When left out, every album missing a year, genre, track numbers or artist.
adding coversboolean, optionalAlso add the release's cover to albums without one. True when left out.

Result: integer. How many albums were changed.

convert for iPods

Sets how iPods get tracks they can't play as they are, or converts them once. Tracks iPods already play are skipped.

convert for iPods track or list of tracks [how iPod conversion] [as AAC boolean]
ParameterTypeDescription
(direct)track or list of tracksThe tracks.
howiPod conversion, optionalreplace file (the default) converts each one once, replacing its file. at every sync converts it each time it's copied to an iPod. kept copy keeps a converted copy just for iPods. none goes back to the default.
as AACboolean, optionalConvert to AAC 256 kbps even for lossless songs (otherwise they become Apple Lossless). False when left out.

Result: integer. How many tracks were changed.

tell application "ClassicTunes"
    convert for iPods (every track whose iPod compatible is false) how kept copy
end tell

send scrobbles

Sends the plays waiting to ListenBrainz now. They're otherwise sent shortly after each play or sync.

Result: text. A sentence saying what happened. With no account set up, it says so.

check for updates

Checks for a newer version of ClassicTunes, showing what's new and offering to install it. In a copy of ClassicTunes that can't update, it gives an error saying so. See Updates.

activate license

Activates a license key on this Mac and returns a sentence saying who it's licensed to. It needs an internet connection. If the key is wrong, canceled, or already in use on two Macs, it gives an error saying so.

activate license "CT-XXXXX-XXXXX-XXXXX-XXXXX"

deactivate license

Deactivates the license on this Mac, freeing its place for another Mac. See Trial and License.

check license

Checks with classictunes.app now that the license is still active on this Mac, and returns a sentence saying whether ClassicTunes is licensed. ClassicTunes otherwise does this by itself about once a month. If this Mac was removed from its license, or the license was refunded, syncing stops; if there's no internet connection, nothing changes.

license macs

Returns the names of the Macs this Mac's license is activated on, this one included. It needs an internet connection.

license macs -- {"Mac Studio", "MacBook Air"}

remove license mac

Removes another Mac from this Mac's license, by its name as license macs lists it, freeing its place for a new Mac: for one that's lost, sold or no longer used. It stops syncing when it next checks its license. To free this Mac's place, use deactivate license instead.

remove license mac "MacBook Air"

After the trial, sync gives an error saying the trial has ended.

Enumerations

media kind

ValueMeaning
songMusic.
videoA video.
audiobookAn audiobook.

video kind

ValueMeaning
movieListed under Movies on the iPod.
TV showListed under TV Shows.
music videoListed under Music Videos.
home videoA home video.

fill choice

ValueMeaning
no fillFilling free space is off.
unplayed songsSongs you haven't played.
highest rated songsYour highest rated songs.
most played songsYour most played songs.
recently added songsSongs added most recently.
random albumsRandom whole albums.

iPod conversion

ValueMeaning
noneThe default. Songs in other formats (FLAC and so on) are converted to AAC at sync; songs an iPod can't play in its own formats are left off it.
at every syncConverted each time the song is copied to an iPod. The original stays as it is.
kept copyA converted copy is kept just for iPods. The original stays for the Mac.
replace fileConverted once, replacing the file. Only used with convert for iPods.

player state

ValueMeaning
stoppedNothing is playing or paused.
playingSomething is playing.
pausedSomething is paused.

Errors scripts can get

MessageWhat to do
No iPod is connected.Connect the iPod and wait for it to appear in ClassicTunes' sidebar.
That iPod isn't connected.The iPod you named was disconnected. Connect it again.
That iPod isn't set up with this library yet. Sync it once from ClassicTunes first.Sync the iPod once from the app, with this library open.
That music player isn't connected.Connect the player again.
The playlist is empty.Add songs to the playlist, or play another one.
Only songs and audiobooks can change kind.media kind can't make a song a video or change a video's kind. Use video kind for videos.
Only videos have a video kind.Set video kind only on videos.
Give the tracks to convert.convert for iPods needs one or more tracks.
Give the files or folders to import.import needs a file, a folder, or a list of them.
Say where to save it (to file …).export and export podcasts need a to file.