Archive for the
‘AppleScript’ Category

March 13 '17 - 3:26 pm
Preserve a Genius Shuffle Playlist

One of my favorite features of iTunes is Genius Shuffle. By pressing the Option key and Space Bar simultaneously, iTunes will construct a 25-track Genius-type playlist in Up Next around a “seed” track it chooses at random from your library. Typically, I’ll slap Option-Space Bar repeatedly until I get the type of songs I’m in the mood for. Sometimes though—because I have an itchy DJ finger—I’ll abandon the current set of tracks after a few songs and create a new different Genius Shuffle arrangement. But still I’d like to have been able to save the playlist I had abandoned because, at least for a while there, I really liked it. Well, you can do this with AppleScript.

When iTunes is playing Up Next like this the track references are available in current playlist. It is a simple matter to copy them to a new playlist. The script I’ve listed below asks for a name for the new playlist; I’ve set the default answer in the display dialog to “Genius Shuffle” and when I run the script I’ll keep that in the name, for example: “Rockin’ Blues – Genius Shuffle”, “70’s Funk – Genius Shuffle”, and so on.

tell application “iTunes”


if not (exists current playlist) then error

set opt to (display dialog “Enter a name for the new playlist:” default answer “Genius Shuffle”)

set newP to (make new user playlist with properties {name:text returned of opt})

duplicate every track of current playlist to newP

reveal newP

on error


end try

end tell

Click on the little AppleScript icon above to open the script in Script Editor at your house—don’t copy the text in the browser.

Save the script as a Compiled “.scpt” with Script Editor named whatever you like in your [home]/Library/iTunes/Scripts/ folder.

One issue is that if any of the tracks in Up Next are “dead” tracks then the script will fail (a repeat loop could duplicate each track individually to filter dead tracks, but I wanted to keep this simple). On the other hand, I think iTunes will ignore dead tracks when it creates a Genius Shuffle playlist.

Also I would only run this after creating a Genius Shuffle playlist; it doesn’t make much sense to run it when anything else is playing.

February 26 '17 - 8:37 am
How to Blankety-Blank Blank

As you may have noticed, the last few versions of iTunes have been gradually “contextualizing” the interface. For example, the Info window will only display the kind-pertinent tags for a selected track. (Remember: it used to be that the Info window displayed the same configuration for any track; so that Music tracks had access to TV-kind tags, like Season and Episode ID, and so on.)

Despite this, when the iTunes browser window is in Songs view, non-contextual tags can still be displayed and tracks can still be sorted using them. This is because every track entry in the database has the same columns whether they are used for that kind of track or not. Thus, for example, Music tracks have “season” and “episode ID” track tags even though they are not used for Music tracks.

AppleScript can access these tags and a script of mine, Sort by Artwork Size, can store the dimensions of a track’s artwork (eg, “600×600”) to the Category or Episode ID tag. Tracks can then be sorted by their “artwork size” or those tags can be used when constructing Smart playlist criteria.

However, since these tags can’t be accessed very easily it is a chore to clear them if they are no longer required. So here’s a script that will blank a particular tag of all the selected tracks. In this case, it blanks the episode ID tag. For people who already know how to script such a thing, this will be easy as pie. But, if your skill level as a scripter is below Dabbler, you may want to use this as a template for your own blanking scripts: (more…)

August 20 '16 - 6:15 am
Copy Grouping to Work

As you probably know, the latest beta version of iTunes 12.5 includes Work and Movement track tags which Classical music listeners will appreciate. In many cases, you might want to use the text in the Grouping tag for the Work tag. While it might seem easy to just do a Multi-Edit on the tracks and copy-and-paste using the Get Info fields, you’d only be able to do this for individual batches of a single work at a time.

Here’s an AppleScript that will simply copy the Grouping tag to the Work tag for any number of selected tracks:

tell application “iTunes”

set sel to selection of front browser window

if sel is {} then return

repeat with aTrack in sel


tell aTrack to set work to (get grouping)

end try

end repeat

end tell

Save this named whatever you like to your [home]/Library/iTunes/Scripts/ folder so that it will appear in the iTunes Script menu. Select some tracks and launch the script by selecting it from the Script menu. The text from the Grouping tag, even if it’s blank, will be copied to the Work tag of each selected track.

UPDATE: This script can be downloaded as part of the Work and Movement Scripts.

August 15 '16 - 12:02 pm
Hassle-Free Playlist Description

One of the neat things that Apple added to iTunes not so long ago is the user-editable description that is available for regular Playlists (Smart, Folder, Genius and Master library playlists do not have this option) and is visible when the Playlist is in Playlist View. You can edit this description by clicking the Playlist’s “Edit Playlist” button. But when you do this, the iTunes interface changes: a column appears at the right edge of iTunes listing the current tracks in the playlist to which you can drag tracks. It also will change the (now center column) browser window to display the full Music library, which totally discombobulates me.

I do not always care for this when I just want to edit the Playlist’s description. I’d prefer to do so without shaking up the interface. This script will do it:

tell application “iTunes”


set thisPlaylist to (get view of front browser window)

tell thisPlaylist

if special kind is not none or smart or genius or shared then error

end tell

on error



end try

set defaultAnswer to (get thisPlaylist’s description)

if defaultAnswer is missing value then set defaultAnswer to “”

set newDescription to text returned of ¬

(display dialog “Enter the description text for the playlist” & return ¬

& thisPlaylist’s name default answer defaultAnswer)


set thisPlaylist’s description to newDescription

end try

end tell

Save this named whatever you like to your [home]/Library/iTunes/Scripts/ folder so that it will appear in the iTunes Script menu. Select a playlist and launch the script by selecting it from the Script menu. It will quit if the selected playlist is the wrong kind. It will display the current description for the playlist if it exists, otherwise the text field will be blank. Enter up to 255 characters, which is the most that the description can accept, and then click “OK”.

Give this a keyboard shortcut to maximize your quality of life.

August 5 '16 - 6:38 am
The Next Track, Episode 12

In Episode 12 of The Next Track podcast, Kirk and I talk about iTunes and AppleScript. During the episode, we mentioned the following scripts:

Artist – Name Corrector
Remove n Characters from Front or Back
This Tag, That Tag
Search Replace Tag Text
Proper English Title Capitalization
List MIAs
Super Remove Dead Tracks
Music Folder Files Not Added
Move Playlists to Folder
Playlist Manager
Find Album Artwork With Google
Save Album Art as folder.jpg
Embed Artwork
Re-Embed Artwork
PDF Adder
View Cached Music
Copy Tag Info Tracks to Tracks
A Space Between

In case you don’t know, The Next Track is a weekly 30-minute podcast in which Kirk McElhearn and I discuss how people listen to music today; it was inevitable we’d talk about iTunes and AppleScript at some point, right?

January 11 '16 - 6:15 am
Playlist Time to Playlist Name

Here’s a simple script that was inspired by a question Kirk got asked in his Ask the Tunes Guy column in Macworld last week. A user lamented that the playlist length (that is, its total time) is not available in the Music app on iOS. Kirk recommended adding this information to the playlist name in iTunes and then re-syncing. A good solution but kind of a chore. This script will do the renaming part for you.

Just select a playlist in iTunes and launch the script. It will display a confirmation dialog and then rename the playlist by appending the time length to its current name:

tell application “iTunes”


set thisPlaylist to view of front browser window

if special kind of thisPlaylist is none then

set newName to (my fixName(thisPlaylist’s name) & ” | ” & thisPlaylist’s time) as text

— confirm


display dialog “Rename selected playlist to:” & return & return & newName

on error


end try

tell thisPlaylist to set name to newName



end if

on error m number n


display dialog “Can’t change the selected playlist’s name.” buttons {“OK”}

end try


end try

end tell

to fixName(n)

if n does not contain “|” then return n

return text 1 thru ((offset of “|” in n) – 2) of n

end fixName

Couple of things to note: only user-created scripts (plain and Smart) can be affected. Also the script uses the “pipe” character to separate the actual name with the appended data. When you run the script again on a playlist that has previously been affected by the script, it uses the “|” as a marker to erase the appended text; thus, the playlist name can be updated if the contents changes at a later date. So, if you happen to already use “|” in your playlist names you may have a problem. You can edit the script in Script Editor to change each of the occurrences of “|” to some other text character that you aren’t likely to use.

November 24 '15 - 10:22 am
NEW: Doug’s Check For Update

While some applets and all apps from this site have a menu command to “Check for Update…”, many AppleScripts do not. To make it easier to check if a script you downloaded at one time or another has a newer version available, use Doug’s Check For Update. Just drop an AppleScript file on it (or launch it and use an Open Panel to navigate to a specific script) and it will check the website for the latest version of that script:

Doug's Check For Update

Sure, it’d be great if I could include a Sparkle-like mechanism to automatically check and install updates—as my apps do. But the code required to do so would just add too much overhead to a—very likely—simple AppleScript.

More information and download is here.

August 26 '15 - 1:19 pm
“10 things that iTunes does right”

Kirk lists ten things iTunes does right at Macworld.

I’m very partial to #10.

July 25 '15 - 2:34 pm
Get Asked This A Lot

“Do you have any scripts that work with iCloud Music Library/iTunes Match tracks? Like, to delete the cloud uploads and re-upload tracks?”

No. AppleScript cannot access anything in the cloud. Here’s one of the reasons why: While it’s certainly possible for me to write a script that arbitrarily deleted local tracks and files on your computer (with or without your knowledge), that’s OK because presumably you knew stuff was going to get deleted by running the script. It’s your responsibility, and Apple doesn’t care what you do on your machine. You could set in on fire if that’s what you wanted to do. Or install Flash. However, stuff in the cloud is Apple’s responsibility. And they have pretty much guaranteed you that your goods up there are safe. So they’re not going to let an AppleScript (of all things!) have arbitrary access to your goods in the cloud where it could potentially be a bad actor.

AppleScript can manipulate cloud tracks locally; for instance, copy such tracks to a new playlist or edit their tags. But anything to do directly with the cloud is strictly out-of-bounds.

July 17 '15 - 1:50 pm
iTunes 12.2 and AppleScript Issues, Volume 2

I have already noted the new loved properties for tracks and playlists. I must admit that an update to the iTunes sdef was heartening (pun not much intended). However, there are some lingering issues and new issues with the latest incarnation of iTunes. Perhaps they have yet to be got to. Yeah, that’s it. They’ll fix it, fingers crossed.

Pretty much anything to do with the Apple Music tabs is off limits to AppleScript. So, for example, you can’t select a track or playlist in “For You” and get info about it or manipulate it. I don’t mind this so much. Once tracks are added to “My Music”, they are accessible as shared tracks. It’d been the same with iTunes Match tracks. You can get and set the conventional track properties, you just can’t access file metadata because, by definition, there are no local files for tracks in the cloud. Still, access to tracks and playlists in “For You” and “New” might be convenient, if even for accessibility purposes.

I am disappointed that current track does not work with playing Apple Music tracks (if playing from within the “For You” tab). I’m not sure if there’s a security issue here, or it’s just not hooked up yet. Definetly screws up anything watching for current track to change. Notifications is aware of playing Apple Music tracks, so why not AppleScript? Even as read-only. I’d be cool with that.

The new Playlist view doesn’t seem quite hooked-up yet either. This is the new default view for local playlists. Anyone who’s ever worked with broadcast automation will note that the abridged track information in Playlist view is reminiscent of a music log. (Note to Apple: please let us change this default view. As much as I really like the Playlist view I prefer Songs view as default to work with track tagging and only switch to Playlist view on formal occasions.) AppleScript-wise, it’s all OK. But the reason I don’t think it’s hooked-up properly yet is by observing the pasteboard data from a playlist in Playlist view. Now, pasteboard data for dragged tracks and playlists typically contain metadata about the playlist and tracks being dragged. Except when the drag originates from a local playlist in Playlist view. In such cases, only the file paths are available in the pasteboard. And if cloud tracks are included in the drag, well, they don’t have local files, so nothing about them at all appears in the pasteboard. However, when the very same playlist is in Songs view, the metadata is available from the dragged tracks’ pasteboard as expected. So the data is already permissibly available, just not (yet?) in Playlist view. I suspect that, because it seems to be the same view used in “For You” when you play a playlist there, it has no provision for includiing metadata. Someone should get on that.

Also, there’s no AppleScript way to access the artwork(s) or description that is a part of the new playlist header. It would be convenient to be able to script a playlist’s description at least.

Many of my scripts and apps access the iTunes Library.xml file to get information about tracks and playlists. That’s what it’s there for. iTunes 12.2 has, uhm, mucked around with the format of this file. As a result, there’s been a big scramble by all of us who develop iTunes apps to re-jigger our stuff. The XML now contains information about mobile apps, Ringtones, iTunes LP, iTunes Extras, and books. The “Master” playlist has a name of “####!####” where it used to be “Library”. “Smart Info” and “Smart Criteria” keys are now being used with playlists other than the Smart variety. Additionally, there is now no way to tell the difference between Genius and Smart playlists; they look identical in the XML. Previously, a Genius playlist would key the persistent ID of its seed track and this would be a differentiating factor. But the “Genius Track ID” key is gone.

The new “Share iTunes Library XML with other applications” Preference setting is still a mystery to me, but I suppose there could be situations whereby you’d want this off. Sometimes I’m just plain dumb about things like that. But because some users think it looks like the perfect thing to uncheck I’ve needed to include a check for access to the XML at the start of many apps. Because if it’s off, the script or app won’t work.

On the bright-side though, despite the Apple Music considerations, iTunes still has pretty robust AppleScript support! So thanks for that, Apple.

Site contents © 2001 - 2017 (that's right: 2001) Doug Adams and weblished by Doug Adams. Contact support AT dougscripts DOT com. About.
All rights reserved. Legal.
AppleScript, iTunes, iPod, iPad, and iPhone are registered trademarks of Apple Inc. This site has no direct affiliation with Apple, Inc.
One who says "it cannot be done" should not interrupt one doing it.