Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save henrik/9293388 to your computer and use it in GitHub Desktop.
Save henrik/9293388 to your computer and use it in GitHub Desktop.
A new home for an old AppleScript.
(*
"ID3 track numbers from filename prefixes" for iTunes
By Henrik Nyh, http://henrik.nyh.se, 2007-01-14.
Based on "Filenames to Song Names" by Doug Adams.
*)
tell application "iTunes"
if kind of container of view of front window is not library or selection of front browser window is {} then
display dialog "Select some tracks in iTunes first." buttons {"Cancel"} default button 1 with icon 2 giving up after 15
end if
set sel to selection of front browser window
set old_fi to fixed indexing
set fixed indexing to true
with timeout of 3000 seconds
repeat with this_track in sel
if class of this_track is file track and this_track's location is not missing value then
try
set trackNumber to my extract_number_prefix(this_track's location)
if trackNumber is not "" then
set this_track's track number to trackNumber
end if
end try
end if
end repeat
end timeout
set fixed indexing to old_fi
try
display dialog "Done!" buttons {"Thanks"} default button 1 giving up after 4
end try
end tell
to extract_number_prefix(loc)
set trackNumber to (last item of my text_to_list(loc as Unicode text, ":"))
set rubyScript to "print $1 if ARGV[0] =~ /^0*(\\d+)/"
return do shell script "ruby -e " & quoted form of rubyScript & " " & quoted form of trackNumber
end extract_number_prefix
on text_to_list(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 text_to_list
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment