dougscripts.com

frontpage : scripts : here

Missing Menu Commands

This is a list (a very subjective list) of scripts that perform tasks you may wish were actual iTunes, Music or TV Menu commands*. Just copy and paste 'em to your AppleScript Editor and save to the appropriate "Scripts" folder...then add a Shortcut.

Optionally, you can click the mini-AppleScript Editor icon link beneath each snippet to open the script in AppleScript Editor on your computer. These links use URL Protocol Messaging to safely and securely send the contents of the script to your AppleScript Editor application.

*In macOS 10.15 and later, you will need to replace "iTunes" in the snippets with either "Music" or "TV". Also, there is no guarantee that these snippets written for iTunes will work with the Music or TV apps.

12 Second Fade/Stop and Next Track - control

Run to initiate a 12 second fade-to-stop of the current playing track and then play the next track.

tell application "iTunes"

if player state is playing then

set stoVO to get sound volume

set diff to (stoVO / 12)

repeat

set sv to (get sound volume)

set sub to (sv - diff)

if (sub is less than or equal to 3) then

copy 0 to sound volume

exit repeat

end if

copy sub to sound volume

delay 1

end repeat

stop

copy stoVO to sound volume

next track

play

end if

end tell

Click to open in Script Editor
Choose Playlist - control

Select, view and play a playlist by entering its name--even just a portion of it.

tell application "iTunes"

set matchFound to false

set hasList to {}

set myPlaylist to the text returned of (display dialog "Enter a Playlist Name" default answer "" default button 2)

set allPlaylists to name of playlists

repeat with thisPlaylist in allPlaylists

if thisPlaylist is myPlaylist then

set matchFound to true

exit repeat

else

if thisPlaylist contains myPlaylist then

set end of hasList to thisPlaylist

end if

end if

end repeat

if not matchFound then

if length of hasList is 0 then

display dialog "No matching playlist was found."

else if length of hasList is 1 then

set thisPlaylist to hasList as string

else

set thisPlaylist to (choose from list hasList with prompt "The following playlists contain " & myPlaylist without empty selection allowed)

if thisPlaylist is false then return

end if

end if

try

set view of browser window 1 to playlist thisPlaylist

play playlist thisPlaylist

end try

end tell

Click to open in Script Editor
De-Shuffle All Playlists - control

Turns "Shuffle" off for every playlist. Note that iTunes 11 no longer recognizes the AppleScript shuffle command.

tell application "iTunes"

repeat with i from 1 to (index of last playlist)

try

set shuffle of playlist i to false

end try

end repeat

end tell

Click to open in Script Editor
Delete Empty Playlists - control

Examines each user-created playlist for at least one track and deletes the ones that have no tracks.

tell application "iTunes"

set y to (get index of last user playlist)

repeat with i from y to 1 by -1

try

set thisPlaylist to user playlist i

if special kind of thisPlaylist is none then

if not (exists track 1 of thisPlaylist) then

delete thisPlaylist

end if

end if

end try

end repeat

end tell

Click to open in Script Editor
Eject iPods - control

Run this script to eject a single mounted iPod or select from a choose box if more than one iPod is mounted.

tell application "iTunes"

try

set ipodSources to (get sources whose kind is iPod)

if ipodSources is {} then error

set ipodNames to (get name of sources whose kind is iPod)

set len to (count of ipodSources)

on error

return

end try

if len is 1 then

my eject_(first item of ipodSources, first item of ipodNames)

else

set rez to (choose from list ipodNames with prompt "Eject:" with multiple selections allowed)

if rez is false then return

repeat with ipodName in rez

repeat with i from 1 to (count of rez)

if ipodName is (item i of ipodNames) then

my eject_(item i of ipodSources, ipodName)

end if

end repeat

end repeat

end if

end tell


to eject_(d, n)

try

tell application "iTunes" to eject d

end try

end eject_

Click to open in Script Editor
Just Played These - control

Increase the play counts of the selected tracks and set their played date to now.

tell application "iTunes"

set sel to selection

if sel is not {} then

repeat with t in sel

tell t

try

set played count to (get played count) + 1

set played date to (get current date)

end try

end tell

end repeat

end if

end tell

Click to open in Script Editor
Name New Playlist from Selection - control

The script does exactly what "New Playlist from Selection" (Command-Shift-N) does except you can enter the name for the playlist before it's created. Assign it the Command-Shift-N keyboard shortcut.

tell application id "com.apple.iTunes"

try

set sel to selection

if sel is {} then error

set opt to (display dialog "Make new playlist from selected tracks named:" default answer "" with title "Name New Playlist From Selection" with icon 1)

set newName to (text returned of opt)

if newName is "" then error

on error

return

end try

set newPlaylist to (make new playlist with properties {name:newName})

repeat with t in sel

try

duplicate t to newPlaylist

end try

end repeat

reveal newPlaylist

end tell

Click to open in Script Editor
Play Random Playlist - control

Note that iTunes 11 no longer recognizes the AppleScript shuffle command.

tell application "iTunes"

set randomPlaylist to user playlist (random number from 1 to ((index of last user playlist) as integer))

set view of front browser window to randomPlaylist

-- un-comment to shuffle the playlist first

(*

tell randomPlaylist

repeat 5 times

set shuffle to false

set shuffle to true

end repeat

end tell

*)

play randomPlaylist

end tell

Click to open in Script Editor
Play Random Track of Random Playlist - control

Note that iTunes 11 no longer recognizes the AppleScript shuffle command.

tell application "iTunes"

set randomPlaylist to user playlist (random number from 1 to ((index of last user playlist) as integer))

set view of front browser window to randomPlaylist

-- un-comment to shuffle the playlist first

(*

tell randomPlaylist

repeat 5 times

set shuffle to false

set shuffle to true

end repeat

end tell

*)

play some track of randomPlaylist

end tell

Click to open in Script Editor
Play Selected Track Once - control

Select a track, run the script, the selected track plays and stops

tell application "iTunes"

set sel to selection

if sel is not {} and length of sel is 1 then play item 1 of selection with once

end tell

Click to open in Script Editor
QuickLook Lyrics - control

Run this while a song is playing or while a track is selected to view its lyrics--if any--in a QuickLook window.

tell application "iTunes"

try

if player state is playing then

tell current track to set {nom, art, lyr} to {name, artist, lyrics}

else

if selection is not {} then

tell item 1 of selection to set {nom, art, lyr} to {name, artist, lyrics}

end if

end if

if lyr is "" then error

on error

return

end try

end tell


set tempfile to (((path to application support from user domain) as text) & art & " " & nom) as text

try

set fileRef to (open for access tempfile with write permission)

write lyr to fileRef

close access fileRef

on error

close access fileRef

end try


do shell script "qlmanage -p " & quoted form of POSIX path of tempfile & ";

rm " & quoted form of POSIX path of tempfile & ";"

Click to open in Script Editor
Selected Tracks to Current Playlist - control

Select some tracks and they will be copied to the currently playing playlist.

tell application "iTunes"

if player state is not stopped then

set curPlaylist to current playlist

if curPlaylist is not view of front browser window and class of curPlaylist is user playlist and not smart of curPlaylist and selection is not {} then

try

duplicate selection to curPlaylist

display dialog "Tracks copied." buttons {"Thanks"} default button 1 giving up after 10

end try

end if

end if

end tell

Click to open in Script Editor
Skip Ahead n Seconds - control

Change the "differential" property to the number of seconds you want the current track to skip ahead to. Useful for skipping over commercials and what not.

property differential : 120 -- seconds


tell application "iTunes"

if current track exists then

if player position is greater than start of current track and player position is less than ((finish of current track) - differential) then

set player position to (player position + differential)

end if

end if

end tell

Click to open in Script Editor
Skip and Pretend We Played This - control

Increment the Play Count of the currently playing track and set its Last Played Date to "now", and, irregardless of acomplishing that, advance to the next track in the current playlist. (I have a Smart Playlist that live updates to tracks not played in the last 8 weeks. If a song from it is playing that I don't want to hear again for another 8 weeks, I run this script while it's playing, it leaves the Smart Playlist, and the next song plays.)

tell application "iTunes"

if player state is not stopped then

try

set theTrack to current track

tell theTrack

set originalSkipCount to (get skipped count)

set played count to (get played count) + 1

set played date to (get current date)

end tell

next track

set theTrack's skipped count to originalSkipCount

end try

end if

end tell

Click to open in Script Editor
Stop After This Song - control

Stops iTunes after current playing songs ends

tell application "iTunes"

if player state is playing then

try

set curTrack to current track

if class of curTrack is URL track then return

set curPos to player position

stop

play curTrack with once

set player position to curPos

end try

end if

end tell

Click to open in Script Editor
Super Shuffle - control

Shuffle no longer accessible as of iTunes 11. Select a playlist and run this script to super-shuffle the tracks. For more super-ness, change the 7 in "repeat 7 times" to as large a number as you can stand.

tell application "iTunes"

tell view of front browser window

if special kind is not Party Shuffle then

repeat 7 times

set shuffle to false

set shuffle to true

end repeat

end if

end tell

end tell

Click to open in Script Editor
Toggle Checkmarks of Selected - control

tell application "iTunes"

set sel to selection

if sel is not {} then

repeat with aTrack in sel

tell aTrack

try

set enabled to not (get enabled)

end try

end tell

end repeat

end if

end tell

Click to open in Script Editor
Toggle Mute - control

Alas, as of iTunes 9.1 the only way to mute the audio in iTunes is via AppleScript.

tell application "iTunes" to set mute to (not mute)

Click to open in Script Editor
View Current Playlist - control

Selects the playlist containing currently playing track. Same as Command-L without current track being selected.

tell application "iTunes"

if player state is not stopped then

set view of front browser window to (get current playlist)

end if

end tell

Click to open in Script Editor
Show Get Info Window of a Track's File - file

Select a track and run this script to display its file's Get Info window in the Finder.

tell application "iTunes"

set sel to selection

if sel is not {} and length of sel is 1 then

my openInfo(get location of item 1 of sel)

end if

end tell


to openInfo(l)

try

tell application "Finder"

open the information window of l

activate

end tell

end try

end openInfo

Click to open in Script Editor
Open iTunes Scripts folder - miscelaneous

Open the iTunes Scripts folder (~/Library/iTunes/Scripts) and make it frontmost.

tell application "Finder"

open folder ((path to home folder as text) & "Library:iTunes:Scripts")

activate

end tell

Click to open in Script Editor
Current Track to (Select Playlist) - playlists

tell application "iTunes"

if player state is not stopped then

set curTrack to current track

with timeout of 300 seconds

set thePlaylist to (choose from list ¬

(get name of every user playlist whose smart is false and special kind is none) ¬

with prompt "Copy \"" & (name of curTrack) & "\" to..." OK button name ¬

"This Playlist" without multiple selections allowed) as text

if thePlaylist is "false" then

return

else

set thePlaylist to playlist thePlaylist

end if

end timeout

try

if not (exists (some track of thePlaylist whose database ID is (get curTrack's database ID))) then ¬

duplicate curTrack to thePlaylist

end try

end if

end tell

Click to open in Script Editor
Delete Selected Playlists - playlists

Displays a list of user playlists from which you select those you want to delete.

tell application "iTunes"

set listOfPlaylists to (get name of user playlists whose special kind is none)

set deleteThese to (choose from list listOfPlaylists as list with prompt "Select Playlists to delete..." with multiple selections allowed)

if deleteThese is false then return -- operation canceled

repeat with thisPlaylist in deleteThese

try

delete playlist thisPlaylist

end try

end repeat

end tell

Click to open in Script Editor
Jump to Playlist - playlists

Enter a name or first few letters of a playlist and the script will try to select it in the main browser window.

tell application "iTunes"

repeat

set searchForThis to text returned of (display dialog "Enter the name or first few letters of the playlist you want to view:" default answer "")

if searchForThis is not "" then exit repeat

end repeat

try

set view of front browser window to (get (first playlist whose name starts with searchForThis))

on error

display dialog "None of your playlists seem to begin with " & searchForThis buttons {"Cancel"} default button 1

end try

end tell

Click to open in Script Editor
Search Results to New Playlist - playlists

Creates a playlist containing the results of a search using the search term as the playlist's name.

tell application "iTunes"

set searchString to text returned of (display dialog "Enter text to search for:" default answer "" default button 2)

set searchResult to search library playlist 1 for searchString

if searchResult is not {} then

set newPlaylist to (make new playlist with properties {name:searchString})

repeat with t in searchResult

try

duplicate t to newPlaylist

end try

end repeat

set view of front browser window to newPlaylist

else

display dialog "No result." buttons {"Cancel"} default button 1

end if

end tell

Click to open in Script Editor
Artist to Album Artist - trackinfo

Copies the text in the Artist tag to the Album Artist tag of each selected track.

tell application "iTunes"

if selection is not {} then

set sel to selection

repeat with t in sel

try

set fixed indexing to true

tell t

set album artist to (get artist)

end tell

end try

end repeat

end if

end tell

Click to open in Script Editor
Erase Bookmark - trackinfo

Resets the bookmark time of the selected tracks to 0.0.

tell application "iTunes"

set sel to selection

if sel is not {} then

repeat with aTrack in sel

try

set bookmark of aTrack to 0.0

end try

end repeat

end if

end tell

Click to open in Script Editor
Remove (Album Version) - trackinfo

Some Amazon MP3 tracks come down with their names appended with "(Album Version)". This will remove that text from the Song Names of the selected tracks.

tell application "iTunes"

set sel to selection

if sel is not {} then

repeat with aTrack in sel

try

tell aTrack to set name to my replaceChars(get name, " (Album Version)", "")

end try

end repeat

else

display dialog return & "No tracks are selected..." buttons {"Cancel"} default button 1 with icon 0 giving up after 15

return

end if

end tell


on replaceChars(txt, srch, repl)

set AppleScript's text item delimiters to the srch

set the item_list to every text item of txt

set AppleScript's text item delimiters to the repl

set txt to the item_list as string

set AppleScript's text item delimiters to ""

return txt

end replaceChars

Click to open in Script Editor
Remove SOME TEXT From Names - trackinfo

Enter text to be removed from each selected track's name, eg: " (Previously Unreleased)".

tell application "iTunes"

set sel to selection

if sel is not {} then

set myText to ""

repeat until myText is not ""

set myText to text returned of (display dialog "Enter text to remove from selected tracks' names:" default answer "")

end repeat

repeat with aTrack in sel

try

tell aTrack to set name to my replaceChars(get name, myText, "")

end try

end repeat

else

display dialog return & "No tracks are selected..." buttons {"Cancel"} default button 1 with icon 0 giving up after 15

return

end if

end tell


on replaceChars(txt, srch, repl)

set AppleScript's text item delimiters to the srch

set the item_list to every text item of txt

set AppleScript's text item delimiters to the repl

set txt to the item_list as string

set AppleScript's text item delimiters to ""

return txt

end replaceChars

Click to open in Script Editor
Reset Start and Stop Times - trackinfo

Set the Start and Stop times to their default.

tell application "iTunes"

set sel to selection

if sel is not {} then

repeat with t in sel

tell t

try

set {start, finish} to {0, -1}

end try

end tell

end repeat

end if

end tell

Click to open in Script Editor
Set Skipped Count/Skipped Date - trackinfo

iTunes will only increase a track's skipped count if it is skipped during the first 2 to 20 seconds of play. This script will correctly set the skipped count and skipped date no matter when and advance to the next track.

tell application "iTunes"

if player state is not stopped then

set theTrack to current track

set oldSkipCount to (get skipped count of theTrack)

next track

tell theTrack

if (get skipped count) = oldSkipCount then

try

set skipped count to (get skipped count + 1)

set skipped date to (get current date)

end try

end if

end tell

end if

end tell

Click to open in Script Editor
Virgin Again - trackinfo

Set the played count of selected tracks to 0 and set their last played date to blank. (Versions of iTunes before v8.1 must set the played date property to a date.)

tell application "iTunes"

if selection of front browser window is not {} then

set sel to selection of front browser window

repeat with this_track in sel

try

set played date of this_track to missing value

--> pre v8.1 use date "Wednesday, June 16, 1993 12:00:00 AM"

set played count of this_track to 0

end try

end repeat

end if

end tell

Click to open in Script Editor
Site contents © 2001 - 2024 (that's right: 2001) Doug Adams and weblished by Doug Adams. Contact support AT dougscripts DOT com. About.
All rights reserved. Privacy.
AppleScript, iTunes, iPod, iPad, and iPhone are registered trademarks of Apple Inc. This site has no direct affiliation with Apple, Inc.
The one who says "it cannot be done" should not be interrupting the one who is doing it.