dougscripts.com

frontpage : scripts : here

Spare Parts

Use these example templates in your own scripts. Also see the Missing Menu Commands page for a small trove of scriptlets.

Selection Routines

Single Selected Track
Get a reference to a single selected track.
Each Selected Track
Get a list of references to each selected track.
Select Playing or Single Selected Track
Get a reference to the current track if player is paused or playing; otherwise, check for a single selected track.
Selected Tracks or Entire Playlist
Repeat with a reference to each of the selected tracks; or, if no tracks are selected, repeat with a reference for each track in the currently selected playlist.

Handlers

Is iTunes Running
Returns true or false.
Choose/Restore Encoder
Change encoders on-the-fly and restore Preferences-set encoder afterwards.
Nuke Track 1
Delete a track from iTunes entirely.
Nuke Track 2
Delete a track from iTunes entirely and delete its file immediately (and unrecoverably).
Nuke Track 3
Delete a track from iTunes entirely and move its file to the Trash.
textToList(), listToText(), replaceChars()
Frequently helpful text and list handlers.
Basic Idle Routine
Checks for new current track. Save as Stay-Open Application.
List Manually Managed iPods
Sets two corresponding lists to manually managed iPod names and source references
Export Artwork as File to Folder
Supply an iTunes track reference, path to folder as text, and name of new file as text.
Rating to Text Stars
Pass the value of a track’s rating property.
Single Selected Track - snippet

Get a reference to a single selected track.

tell application "iTunes"

set sel to selection

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

set aTrack to item 1 of sel

-- do something with aTrack

--

else

display dialog return & "Select a single track first..." buttons {"Cancel"} ¬

default button 1 with icon 0 giving up after 15

return

end if

end tell

click
Each Selected Track - snippet

Get a list of references to each selected track.

tell application "iTunes"

set sel to selection

if sel is not {} then

repeat with aTrack in sel

-- do something with aTrack

--

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

click
Select Playing or Single Selected Track - snippet

Get a reference to the current track if player is paused or playing; otherwise, check for a single selected track.

tell application "iTunes"

if player state is not stopped then

set aTrack to current track

else

set sel to selection

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

set aTrack to item 1 of sel

else

display dialog return & "No track is playing or selected..." buttons {"Cancel"} ¬

default button 1 with icon 0 giving up after 15

return

end if

end if

-- do something with aTrack

--

end tell

click
Selected Tracks or Entire Playlist - snippet

Repeat with a reference to each of the selected tracks; or, if no tracks are selected, repeat with a reference for each track in the currently selected playlist.

tell application "iTunes"

set sel to selection

if sel is not {} then

repeat with aTrack in sel

-- do something with aTrack

--

end repeat

else

set thePlaylist to view of the front browser window

repeat with i from 1 to (index of last track of thePlaylist)

set aTrack to track i of thePlaylist

-- do something with aTrack

--

end repeat

end if

end tell

click
Is iTunes Running - handler

Returns true or false.

on hookIsRunning()

tell application "iTunes" to return running

end hookIsRunning

click
Choose/Restore Encoder - handler

Change encoders on-the-fly and restore Preferences-set encoder afterwards.

global encoderBackup


tell application "iTunes"

my chooseEncoder()

-- do something with new encoder

--

my restoreEncoder()

end tell


to chooseEncoder()

tell application "iTunes"

set myEncoders to name of every encoder

set encoderBackup to name of current encoder

set myNewEncoder to (choose from list myEncoders with prompt "Choose an encoder..." default items (encoderBackup as list)) as text

if myNewEncoder is "false" then error number -128

set current encoder to encoder myNewEncoder

end tell

end chooseEncoder


to restoreEncoder()

tell application "iTunes" to set current encoder to encoder encoderBackup

end restoreEncoder

click
Nuke Track 1 - handler

Delete a track from iTunes entirely.

to nukeTrack(aTrack)

-- pass a track reference as aTrack

tell application "iTunes"

try

delete (some track of library playlist 1 whose persistent ID is (get persistent ID of aTrack))

end try

end tell

end nukeTrack

click
Nuke Track 2 - handler

Delete a track from iTunes entirely and delete its file immediately (and unrecoverably).

to nukeTrackAndDeleteFile(aTrack)

-- pass a track reference as aTrack

tell application "iTunes"

try

set loc to missing value

if (get class of aTrack) is file track then set loc to quoted form of POSIX path of ((get location of aTrack) as text)

delete (some track of library playlist 1 whose persistent ID is (get aTrack's persistent ID))

if (loc is not missing value) then do shell script "rm " & loc

return true

on error

return false

end try

end tell

end nukeTrackAndDeleteFile

click
Nuke Track 3 - handler

Delete a track from iTunes entirely and move its file to the Trash.

to nukeTrackAndTrashFile(aTrack)

-- pass a track reference as aTrack

tell application "iTunes"

try

set loc to missing value

if (get class of aTrack) is file track then set loc to (get location of aTrack)

delete (some track of library playlist 1 whose persistent ID is (get aTrack's persistent ID))

if (loc is not missing value) then my trashFile(loc)

return true

on error

return false

end try

end tell

end nukeTrackAndTrashFile


to trashFile(loc)

tell application "Finder"

try

delete loc

on error

error number 1

end try

end tell

end trashFile

click
textToList(), listToText(), replaceChars() - handler

Frequently helpful text and list handlers.

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


on textToList(txt, delim)

set saveD to AppleScript's text item delimiters

try

set AppleScript's text item delimiters to {delim}

set theList to every text item of txt

on error errStr number errNum

set AppleScript's text item delimiters to saveD

error errStr number errNum

end try

set AppleScript's text item delimiters to saveD

return (theList)

end textToList


on listToText(theList, delim)

set saveD to AppleScript's text item delimiters

try

set AppleScript's text item delimiters to {delim}

set txt to theList as text

on error errStr number errNum

set AppleScript's text item delimiters to saveD

error errStr number errNum

end try

set AppleScript's text item delimiters to saveD

return (txt)

end listToText

click
Basic Idle Routine - handler

Checks for new current track. Save as Stay-Open Application.

property currentDBID : missing value

property idleTime : 15


on run

set currentDBID to missing value

-- other initialization here...

end run


on idle

if my hookIsRunning() then

tell application "iTunes"

if player state is playing then

set curTrack to current track

set thisDBID to (get curTrack's database ID)

if thisDBID is not currentDBID then

set currentDBID to thisDBID

-- do something with curTrack here...

--

end if

end if

end tell

end if

return idleTime

end idle


on hookIsRunning()

tell application "iTunes" to return running

end hookIsRunning

click
List Manually Managed iPods - handler

Sets two corresponding lists to manually managed iPod names and source references

global iPodSelected, listOfSources, listOfNames


-- the variables will be set after handler is run:

-- listOfSources corresponds with listOfNames


tell application "iTunes"

my checkForDevices()

log listOfSources

log listOfNames

-- for example:

try

set iPodSelected to choose from list listOfNames

on error m number n

log n

log m

end try

end tell


to checkForDevices()

set iPodSelected to missing value

set listOfSources to {}

set listOfNames to {}

tell application "iTunes"

set lp to every source whose kind is iPod

if lp is {} then return

repeat with src in lp

my checkManagement(src)

end repeat

end tell

if listOfSources is {} then

display dialog return & "No manually managed iPods are mounted..." buttons {"Cancel"} default button 1 with icon 0 giving up after 15 with title my_title

error number -128

end if

end checkForDevices


to checkManagement(p)

tell application "iTunes"

try

set x to (make new user playlist at p)

delete x

set end of listOfSources to p

set end of listOfNames to (name of p)

end try

end tell

end checkManagement

click
Export Artwork as File to Folder - handler

Supply an iTunes track reference, path to folder as text, and name of new file as text.

tell application "iTunes"

set sel to (first item of selection)

# pass (iTunes track reference, path to folder as text, name of file as text)

my exportArtwork_(sel, (path to desktop from user domain) as text, "myartworkpic")

end tell


to exportArtwork_(theTrack, exportFolder, artworkName)

tell application "iTunes"

try

tell theTrack to set {artworkData, imageFormat} to {(raw data of artwork 1), (format of artwork 1) as text}

on error

# probably no artwork

return false

end try

end tell

set ext to ".png"

set fileType to "PNG"

if imageFormat contains "JPEG" then

set ext to ".jpg"

set fileType to "JPEG"

end if

set pathToNewFile to (exportFolder & artworkName & ext) as text

# if file with same name exists in same location then delete it

# optional

try

do shell script "rm " & quoted form of POSIX path of pathToNewFile

end try

try

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

set eof fileRef to 0

write artworkData to fileRef starting at 0

close access fileRef

on error m

try

close access fileRef

end try

return false

end try

try

tell application "System Events" to set file type of (pathToNewFile as alias) to fileType

on error m

# may not be critical

log ("ERROR: " & m)

end try

return true

end exportArtwork_

click
Rating to Text Stars - handler

Pass the value of a track’s rating property.

to displayTrackRatingStars(r)

-- pass a track's rating value

set rez to ""

repeat (r div 20) times

set rez to (rez & («data utxt2605» as Unicode text)) as text

end repeat

if r mod 20 = 10 or r = 10 then

set rez to (rez & («data utxt00BD» as Unicode text)) as text

end if

return rez

end displayTrackRatingStars

click
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.