DIY
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…)
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.
Reset Plays MIA
[UPDATE: iTunes 12.4.1, released June 2, 2016, restores the Reset Plays feature. The "Reset" button is located in the track's Get Info window's "Details" tab adjacent to the play count entry.
Of course, the script below is still serviceable.]
Looks like the "Reset Plays" command is MIA in iTunes 12.4. Here's a script that will reset the played count of each selected track to 0 and remove its played date.
tell application "iTunes"
set sel to selection
if sel is {} then return -- nothing selected
repeat with thisTrack in sel
-- use try blocks to fail gracefully if problem
try
set thisTrack's played count to 0
end try
try
set thisTrack's played date to missing value
end try
end repeat
end tell
Click on the script icon to open this in Script Editor on your machine. From Script Editor's File menu, click "Save...". In the Save Panel, enter a name for the script, like "Reset Plays", and save it to your [username]/Library/iTunes/Scripts/ folder (if no "Scripts" folder exists, create one). Once saved to this folder, the script will appear in iTunes' Script menu. To use, select the track(s) in iTunes whose plays you want to reset and then choose the "Reset Plays" script in the Script menu.
A more advanced version of this script is available as a download from this page.
Alternatively, you can use Add or Subtract Play Count, New Last Played Date, or New Play Count. Each has some capacity to change a track's Plays.
There still seems to be a bug regarding changing the Plays of Matched/Uploaded tracks in the cloud, whereby the played count reverts to whatever it was before being "artificially" set with AppleScript (or other means). I couldn't say if that issue is related to the disappearance of "Reset Plays".
Delete Other Artwork
iTunes is able to accommodate more than one image for a track entry's artwork. What hasn't always been obvious is that the first artwork in an array of artworks in a track is the "display" artwork. Any additional artwork in the artworks array are spares. Modern versions of the iTunes Get Info Artwork panel now label images as "Album Artwork" and "Other Artwork".
Some people think that these "Other Artwork" have to go.
It is a simple matter to simply remove the other artwork from a track entry with an AppleScript. Here's one now that goes through each track in a selection and repeats through its artworks (if there's more than one artwork), removing the last artwork until there is only the first one left:
tell application "iTunes"
set selectedTracks to selection of front browser window
repeat with i from 1 to (count of selectedTracks)
set thisTrack to item i of selectedTracks
tell thisTrack
try
if (count of its artworks) > 1 then
repeat until ((count of its artworks) is 1)
delete its last artwork
end repeat
end if
end try
end tell
end repeat
end tell
Click the script icon above to download this script to Script Editor at your house and click "New Script" to allow it to open. Save it named whatever you like in your ~/Library/iTunes/Scripts/ folder (or wherever you put them). Select some tracks in iTunes and launch the script.
There's always the kid in the front who wants to know why you don't make a script like this that just works on the entire library. Well: iTunes can be kind of touchy when AppleScripting a large amount of artwork stuff. It may "go modal" as it updates its database and under such circumstances may block an AppleScript from continuing its operation. So I purposely used the selection object so that the script can be used piecemeal on a small batch of tracks at a time. Additionally, you might want to eyeball the "Other Artworks" of a track before you start blindly mowing them all down; in which case having to select the tracks manually compels you to be in control.
The routines are in a try block to skip over errors because of what I mentioned above about modality and also in case there's some image gunk in there that causes trouble. Because images downloaded from the internet that you use as album artwork are always pristine, right?
Play Selected Track and Cue Next
A Correspondent inquired about a script that would play a selected track in iTunes through to the end and then stop and then select the next track but not play it. In such a way, a playlist containing sequential musical cues required for a theatrical performance could be fired one track at a time, via the script, without a lot of stopping and mouse-clicking and swearing backstage ("Up yer scrim!", "Purple behind!").
This is such a script:
-- Play Selected Track and Cue Next
tell application "iTunes"
-- get the single selected track and play it
set theSelection to selection
if length of theSelection is 1 then
set theTrack to item 1 of theSelection
try
play theTrack with once
on error
## beep
return
end try
-- the selected track is playing, now do some other stuff...
-- get the playlist
set thePlaylist to (get view of front window)
-- stash current fixed indexing value
set curfi to fixed indexing
-- we want free indexing not fixed indexing
set fixed indexing to false
-- compute next track's index
set idx to (get index of theTrack)
if (idx = (count of tracks of thePlaylist)) then
set idx to 1
else
set idx to (idx + 1)
end if
-- select the next track in the playlist
reveal track idx of thePlaylist
-- restore fixed indexing to whatever it was before
set fixed indexing to curfi
end if
end tell
What you'd want to do is save this with Script Editor, named whatever you like, with a File Format of "Script" and put it in your [Home]/Library/iTunes/Scripts/ folder so it appears in iTunes' Script menu.
Prepare a playlist and select its first track. When it's time to actually play the track, don't use any iTunes play controls; instead, fire the script. The script will play the selected track, figure out which track is next in sequence, select it, and then quit. iTunes will stop when the current track has finished by virtue of that with once parameter on the play command. When it's time to play the next track, which is now the selected track, fire the script to play it and select the next track. And so on.
While assigning this script a keyboard shortcut will be convenient if you can keep a hand near the keyboard, something that could fire this via a physical remote control would be super boss. Under such circumstances, you may prefer—or it may be necessary—to save the script with a File Format of "Application".
Spotify Artwork
Remember that script I posted a while back that exports the currently playing iTunes track's artwork?
Well, this one works with Spotify's desktop app:
tell application "Spotify"
try
if player state is not stopped then
set alb to (get album of current track)
set rawData to (get artwork of current track)
else
return
end if
on error
display dialog "Problem getting track info." buttons {"OK"}
return
end try
end tell
try
set baseLoc to choose folder
on error
return
end try
set newPath to ((baseLoc as text) & (my replaceChars(alb, ":", "_")) & ".tiff") as text
try
set fileRef to (open for access newPath with write permission)
write rawData to fileRef starting at 0
tell me to close access fileRef
on error m number n
log n
log m
try
tell me to close access fileRef
end try
end try
on replaceChars(txt, srch, repl)
set text item delimiters to srch
set item_list to every text item of txt
set text item delimiters to repl
set txt to item_list as string
set text item delimiters to ""
return txt
end replaceChars
Spotify has a pretty basic sdef but you can get some info from the current track, including artwork.
Blanking the Genre Tag in iTunes 12
Kirk has found a bug in iTunes 12 whereby selecting a batch of tracks and deleting their Genre tag via the Get Info panel inserts 8 spaces instead of empty text into each track's Genre tag. This creates a Blank Genre that is actually displayed and selectable in Genre lists.
This AppleScript will correctly delete the Genre tag of each track in a selection:
tell application "iTunes"
set sel to selection
repeat with thisTrack in sel
tell thisTrack to set its genre to ""
end repeat
end tell
Also, any number of other scripts that can edit the Genre tag, like Multi-Item Edit, will get the job done.
Save Current Track's Artwork
I don't know why you'd want to do this on any kind of regular basis, but I thought it was kinda fun for a Friday afternoon. I wanted to see if I could save artwork from iTunes Radio tracks, but it works with whatever the current track is:
tell application "iTunes"
try
if player state is not stopped then
set alb to (get album of current track)
tell artwork 1 of current track
if format is JPEG picture then
set imgFormat to ".jpg"
else
set imgFormat to ".png"
end if
end tell
set rawData to (get raw data of artwork 1 of current track)
else
return
end if
on error
display dialog "Problem getting track info." buttons {"OK"}
return
end try
end tell
try
set baseLoc to choose folder
on error
return
end try
set newPath to ((baseLoc as text) & alb & imgFormat) as text
try
tell me to set fileRef to (open for access newPath with write permission)
write rawData to fileRef starting at 0
tell me to close access fileRef
on error m number n
log n
log m
try
tell me to close access fileRef
end try
end try
Open that in Script Editor and "Save..." it as "Save Current Track's Artwork" (or whatever) in your ~/Library/iTunes/Scripts/ folder. Whenever a track is playing, you can launch it to choose a folder in which to save the current track's artwork. Great way to fill up your desktop with effluvia. (I'm kidding. But it would be good for that.)
More iTunes Power Search at Your Fingertips
[UPDATE: In iTunes 12.2 and later, selecting "Music" in this script will go to the "New" section of Apple Music. The other kind selections work as expected.]
Kirk has noticed that Apple has spruced up the iTunes Power Search page. Inspired, I improved this simple AppleScript to launch a Power Search by kind (click the AppleScript Editor icon below the code to display the code in AppleScript Editor on your machine):
set chooseOptions to {"All Results", "Apps", "Music", "Movies", "TV Shows", ¬
"Podcasts", "Books", "iTunesU"}
set optList to {"mt=3", "mt=8", "mt=1", "mt=6", "mt=4", "mt=2", ¬
"media=books#powerSearch&restrict=false&submit=media", ¬
"institutionTerm=&media=iTunesU"}
set opt to (choose from list chooseOptions with prompt "iTunes Power Search...")
if opt is false then return
repeat with i from 1 to length of chooseOptions
if opt as text is (item i of chooseOptions) then
set opt to (item i of optList)
end if
end repeat
set link to ("itms://phobos.apple.com/WebObjects/MZSearch.woa/wa/advancedSearch?" & opt) as text
tell application "iTunes" to open location link
Save that as something like "Power Search" in your ~/Library/iTunes/Scripts/ folder and assign it a shortcut. When you launch it, you can select the area you want to search: