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 nametells 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
| Element | What it is |
|---|---|
tracks | Every song, video and audiobook in the open library. |
playlists | Every playlist and smart playlist in the open library. |
iPods | The iPods connected now. |
music players | The 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
| Property | Type | Description |
|---|---|---|
selection | list of tracks, read-only | The tracks selected in the window: the songs in the selected albums, or the selected songs. |
current track | track, read-only | The track playing or paused now, if it's in the library. |
player state | player state, read-only | stopped, playing or paused. |
player position | real | Seconds into the current track. Set it to jump to that point. |
sound volume | integer | The player's volume, 0 to 100. |
audiobook speed | real | Playback speed for audiobooks, from 0.75 to 2 (1 is normal speed). The pitch stays the same. |
library name | text, read-only | The name of the open library. |
scrobbling | boolean | Whether 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 account | text, read-only | The ListenBrainz user plays are sent to, or empty text when no account is set up. |
plays waiting | integer, read-only | How many plays are waiting to be sent to ListenBrainz. |
automatically checks for updates | boolean | Whether ClassicTunes checks for a newer version once a day. Always false in a copy that can't update. |
licensed | boolean, read-only | Whether ClassicTunes is licensed (also true in a copy that doesn't need a license). |
license email | text, read-only | The email address the license belongs to, or empty text when not licensed. |
trial days left | integer, read-only | Days 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.
| Property | Type | Description |
|---|---|---|
id | text, read-only | The track's unique id. It doesn't change. |
name | text | The title. |
artist | text | |
album | text | |
album artist | text | |
genre | text | |
composer | text | |
comment | text | |
year | integer | 0 when there's none. |
track number | integer | |
track count | integer | How many tracks the album (or disc) has. |
disc number | integer | |
disc count | integer | |
rating | integer | 0 to 100, in steps of 20: one star is 20, five stars is 100. Other values are rounded to the nearest star. |
played count | integer, read-only | How many times it's been played, including plays on iPods. |
played date | date, read-only | When it was last played (missing value if never). |
date added | date, read-only | When it was added to the library. |
duration | real, read-only | Length in seconds. |
media kind | media kind | song, 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 kind | video kind | For a video: movie, TV show, music video or home video (where it goes in the iPod's Videos menu). Only videos have one. |
show | text | For a TV show: the show it's an episode of. |
season number | integer | For a TV show: its season. |
episode number | integer | For a TV show: its episode. |
explicit | boolean | Whether 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. |
sync | boolean | True 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. |
bookmark | real | Where playback carries on from, in seconds, for audiobooks and tracks that remember their place. Set it to 0 to start from the beginning. |
location | file, read-only | The track's file. |
iPod compatible | boolean, read-only | Whether this library's iPods can play the file as it is. See iPod › Find Songs iPods Can't Play…. |
iPod conversion | iPod conversion, read-only | How iPods get the track: none (the default), at every sync or kept copy. Change it with convert for iPods. |
artwork size | integer, read-only | The 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.
| Property | Type | Description |
|---|---|---|
id | text, read-only | The playlist's unique id. |
name | text | The playlist's name. It can't be set to empty text. |
smart | boolean, read-only | Whether it's a smart playlist. |
sync | boolean | Whether the playlist goes to every iPod and music player that syncs the entire library. Setting it changes all of them. |
| Element | What it is |
|---|---|
tracks | The 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.
| Property | Type | Description |
|---|---|---|
id | text, read-only | The iPod's id. |
name | text, read-only | The iPod's name. |
model | text, read-only | The model, such as "iPod classic 80 GB". |
capacity | real, read-only | The size of its disk, in bytes. |
free space | real, read-only | Free space, in bytes. |
syncing | boolean, read-only | Whether it's syncing now. |
syncs entire library | boolean | Whether the whole library is synced. When false, the chosen artists, albums, genres and playlists are synced instead. |
chosen artists | list of text | Artists chosen to sync, by name. |
chosen albums | list of text | Albums chosen to sync, each written as "Artist — Album". "Artist - Album" with a hyphen works too. |
chosen genres | list of text | Genres chosen to sync. |
chosen playlists | list of text | Playlists chosen to sync, by name. |
fill free space | fill choice | What fills the rest of the space after what's chosen (like iTunes' Autofill). Only used when syncs entire library is false. |
syncs all photos | boolean | Whether every photo in the Photos app goes to the iPod, as well as the chosen albums and periods. |
chosen photo albums | list of text | Photos app albums (Favorites too) and ClassicTunes photo albums chosen for the iPod, by name. |
chosen photo periods | list of text | Periods by when photos were taken, written as "2019" (a year) or "2019-07" (a month). |
photo space limit | real | The most space photos may use on the iPod, in bytes. 0 means no limit. |
photo videos | boolean | Whether the chosen albums' and periods' videos go to the iPod's Videos menu too, converted for the iPod. |
photo video space limit | real | The most space those videos may use, in bytes (0 for no limit). The newest go first. |
photos only on this Mac | boolean | Leave out photos whose originals are only in iCloud. |
last backup | date, read-only | When the newest snapshot of the iPod was taken (missing value when there's none). |
snapshot count | integer, read-only | How many snapshots of the iPod there are. |
games | list of text, read-only | The names of the click wheel games on the iPod (Apple's games, as iTunes put them there). |
rockbox | boolean, read-only | Whether 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 games | list of text, read-only | The 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).
| Property | Type | Description |
|---|---|---|
id | text, read-only | The player's id. |
name | text, read-only | The player's name. |
kind | text, read-only | The kind of player it was set up as (Shanling Q2, Tangara, Rockbox…). |
capacity | real, read-only | The size of its storage, in bytes. |
free space | real, read-only | Free space, in bytes. |
syncing | boolean, read-only | Whether it's syncing now. |
syncs entire library | boolean | Whether the whole library is synced. When false, the chosen artists, albums, genres and playlists are synced instead. |
chosen artists | list of text | Artists chosen to sync, by name. |
chosen albums | list of text | Albums chosen to sync, each written as "Artist — Album". |
chosen genres | list of text | Genres chosen to sync. |
chosen playlists | list of text | Playlists chosen to sync, by name. |
fill free space | fill choice | What fills the rest of the space after what's chosen. Only used when syncs entire library is false. |
rockbox games | list of text, read-only | The 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]
| Parameter | Type | Description |
|---|---|---|
| (direct) | file or list of files | The files or folders to add. |
filling in missing details | boolean, optional | Afterward, 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
| Parameter | Type | Description |
|---|---|---|
| (direct) | playlist | The playlist. |
to | file | Where 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
| Parameter | Type | Description |
|---|---|---|
to | file | Where 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]
| Parameter | Type | Description |
|---|---|---|
downloading | boolean, optional | Whether 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]
| Parameter | Type | Description |
|---|---|---|
| (direct) | track or list of tracks, optional | Tracks 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]
| Parameter | Type | Description |
|---|---|---|
| (direct) | track or list of tracks, optional | Tracks 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]
| Parameter | Type | Description |
|---|---|---|
| (direct) | track or list of tracks, optional | Tracks whose albums to look up. When left out, every album missing a year, genre, track numbers or artist. |
adding covers | boolean, optional | Also 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]
| Parameter | Type | Description |
|---|---|---|
| (direct) | track or list of tracks | The tracks. |
how | iPod conversion, optional | replace 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 AAC | boolean, optional | Convert 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
| Value | Meaning |
|---|---|
song | Music. |
video | A video. |
audiobook | An audiobook. |
video kind
| Value | Meaning |
|---|---|
movie | Listed under Movies on the iPod. |
TV show | Listed under TV Shows. |
music video | Listed under Music Videos. |
home video | A home video. |
fill choice
| Value | Meaning |
|---|---|
no fill | Filling free space is off. |
unplayed songs | Songs you haven't played. |
highest rated songs | Your highest rated songs. |
most played songs | Your most played songs. |
recently added songs | Songs added most recently. |
random albums | Random whole albums. |
iPod conversion
| Value | Meaning |
|---|---|
none | The 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 sync | Converted each time the song is copied to an iPod. The original stays as it is. |
kept copy | A converted copy is kept just for iPods. The original stays for the Mac. |
replace file | Converted once, replacing the file. Only used with convert for iPods. |
player state
| Value | Meaning |
|---|---|
stopped | Nothing is playing or paused. |
playing | Something is playing. |
paused | Something is paused. |
Errors scripts can get
| Message | What 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. |