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 Menu commands. Just copy and paste 'em to your Script Editor and save to your iTunes Scripts folder...then add a Shortcut.

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

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


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


De-Shuffle All Playlists - control

Turns "Shuffle" off for every playlist.

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


Play Random Playlist - control
tell application "iTunes"
	set last_p to (index of last user playlist)
	my play_this(random number from 1 to last_p)
end tell

to play_this(i)
	tell application "iTunes"
		set view of front browser window to user playlist i
		-- un-comment to shuffle the playlist first
		(*
		repeat 5 times
			set shuffle of playlist i to false
			set shuffle of playlist i to true
		end repeat
		*)
		play user playlist i
	end tell
end play_this


Play Random Track of Random Playlist - control
tell application "iTunes"
	if player state is playing then
		set last_p to (index of last user playlist)
		my play_this(random number from 1 to last_p)
	end if
end tell

to play_this(i)
	tell application "iTunes"
		set view of front browser window to playlist i
		-- un-comment to shuffle the playlist first
		(*
		repeat 5 times
			set shuffle of playlist i to false
			set shuffle of playlist i to true
		end repeat
		*)
		play some track of playlist i
	end tell
end play_this


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
			duplicate selection to curPlaylist
			display dialog "Tracks copied." buttons {"Thanks"} default button 1 giving up after 10
		end if
	end if
end tell


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. Works great when assigned a shortcut.

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


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
			tell current track
				set played count to (get played count) + 1
				set played date to (get current date)
			end tell
		end try
		next track
	end if
end tell


Super Shuffle - control

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


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


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


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


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


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


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


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 search_for_this to text returned of ¬
			(display dialog "Enter the name or first few letters of the playlist you want to view:" default answer "")
		if search_for_this is not "" then exit repeat
	end repeat
	try
		set view of front browser window to ¬
			(get (first playlist whose name starts with search_for_this))
	on error
		display dialog "None of your playlists seem to begin with \"" & ¬
			search_for_this & "\"" buttons {"Cancel"} default button 1
	end try
end tell


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 playlist with properties {name:searchString})
		repeat with t in searchResult
			duplicate t to newPlaylist
		end repeat
		set view of front browser window to newPlaylist
	else
		display dialog "No result." buttons {"Cancel"} default button 1
	end if
end tell


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


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


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 replace_chars(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 replace_chars(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 replace_chars


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 replace_chars(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 replace_chars(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 replace_chars


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


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


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


This column is printer-friendly.
Site contents © 2001 - 2009 Doug Adams and weblished by Doug Adams, dougscripts AT mac DOT com.
All rights reserved. Legal.
AppleScript, iTunes, and iPod are registered trademarks of Apple Inc.
Image 01 Image 01 Image 01 Image 01 Image 01 Image 01 Image 01 Image 01 Image 02 Image 03 Image 03 Image 03 Image 03