AppleScript
Idle Wish List
Dear iTunes Santa,
It would be great if clearing Up Next could be done via AppleScript.
Also, I have wanted to
tell application "iTunes"
make new playlist window with properties {view:playlist "Mom's Favorites"}
end tell
for a long time.
I have been a good boy.
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"
try
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
return
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.
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, "600x600") 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…)
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
try
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.
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"
try
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
beep
return
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)
try
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.
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"
try
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
try
display dialog "Rename selected playlist to:" & return & return & newName
on error
return
end try
tell thisPlaylist to set name to newName
else
error
end if
on error m number n
try
display dialog "Can't change the selected playlist's name." buttons {"OK"}
end try
return
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.
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:

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.
"10 things that iTunes does right"
Kirk lists ten things iTunes does right at Macworld.
I'm very partial to #10.
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.

