-- AppleScript to create a named iTunes playlist from a list of file locations -- -- v1, Mark Kriegsman, November 28, 2012 -- kriegsman@tr.org -- -- Theory of operation: -- Ask iTunes for the Location of every file track (a few seconds) -- Ask iTunes for the DBID of every file track (a few seconds) -- Pray that the two lists come back in the same order (they seem to) -- Use linear search through the Location list to get the 'index' of the sought track (fast enough) -- Use that index to look up in the DBID list what the DBID of the sought track is -- Make Playlist containing those DBIDs -- -- Notes: -- This is not terribly robust, tested, or error-proofed. Seems to mostly work. -- You should specify location paths in classic Mac style, like this: -- "Mac HD:Users:mek:Music:Johzy K:Thousand Kisses.mp3" -- If the playlist exists, it will be emptied out -- Locations for which a track DBID cannot be found are silently skipped -- AppleScript is not a true friend. A true friend would never do that to you. set thelocs to {"Mac HD:Users:mek:Music:Johzy K:Thousand Kisses.mp3", "Mac HD:Users:mek:Music:Morgan Page:The Longest Road.mp3", "This:File:Does:Not Exist.mp3", "Mac HD:Users:mek:Music:Manu Riga:Everything Comes To An End_Solar Fields Remix.mp3"} -- get Location and DBID lists from iTunes init_itunes_location_dbid_globals() -- make the playlist make_named_playlist_from_track_locations("Test Playlist", thelocs) -- done -- -- -- global file_track_locations global file_track_ids global file_track_locations_big_string on init_itunes_location_dbid_globals() tell application "iTunes" set m to library playlist 1 -- "Music" set file_track_locations to location of every file track of m set file_track_ids to database ID of every file track of m end tell set file_track_locations_big_string to file_track_locations as string end init_itunes_location_dbid_globals on find_offset_of_element_in_list(lst, elt) set i to 1 repeat with e in lst -- coerce everything to a string: files, aliases, what have you if e as string is elt as string then return i end if set i to i + 1 end repeat return 0 -- not found end find_offset_of_element_in_list on get_track_dbid_by_location_string(locstring) -- start with quick string-contains test if file_track_locations_big_string contains locstring then set idx to find_offset_of_element_in_list(file_track_locations, locstring) if idx is 0 then -- slow no return 0 else set dbid to item idx of file_track_ids return dbid end if else -- fast 'no' return 0 end if end get_track_dbid_by_location_string on demand_playlist(plname) tell application "iTunes" if not (exists playlist plname) then make new user playlist with properties {name:plname} else delete every track of user playlist plname end if end tell end demand_playlist on make_named_playlist_from_track_locations(plname, listoflocations) set listofdbids to {} repeat with loc in listoflocations set dbid to get_track_dbid_by_location_string(loc) if dbid is not 0 then set end of listofdbids to dbid end if end repeat demand_playlist(plname) tell application "iTunes" repeat with dbid in listofdbids set t to (some track whose database ID is dbid) copy t to end of playlist plname end repeat end tell end make_named_playlist_from_track_locations -- fin --