Importing Track Data From Text Files
If you have a text file containing track data—those non-standard text indicies that accompany a FLAC file, for example, or an export from a database—you can get that data into your iTunes tracks. It'll take a little work on your part, but this method sure beats copy-and-pasting to each track by hand.[jun 4 '04]
Below is a script which will automate the process of getting tag info from a text file into the correct iTunes tracks. The script is listed beneath this overview on how it works and how to customize it yourself.
As of Snow Leopard (OS 10.6), Script Editor.app has been renamed AppleScript Editor.app and is located in your /Appplications/Utilities/ folder. This article refers to Script Editor.app but AppleScript Editor.app also applies.
Pre-requisites
I'm presuming that your text file is a series of track data separated by carriage returns; each line of track data contains tab-delimited data, although any delimiter is servicable (a comma or dash, for example; you will have to make this adjustment in the script, replacing "tab"). Below is a screenshot of the sort of format I'm talking about. This particular file is a Tex-Edit Plus text file. The tabs and carriage returns are highlighted:
There must be consistency here. Each line of data must be in the same order. I don't imagine this will be a problem for most cases. In our example, the order is Song Name, Artist, Album, and Rating.
In order to get that data to some tracks in iTunes, you'll want to create a playlist that contains those tracks and whose Play Order is identical to the order of tracks in the text file. You can set a playlist's Play Order by Control-clicking on the playlist name in the Source column and selecting "Copy to Play Order" in the contextual menu pop-up. In versions of iTunes before 4.5, Control-click on any of the playlist's tracks to get the pop-up.
What our AppleScript will do is take each line of the text file, parse it for data, and copy each piece of data to the appropriate tag in the track corresponding to the line of the text file. Conceptually, it looks like this:
In the script below, a repeat loop is used to traverse through each line of the text file. You must make certain that there is a one-to-one correspondence between line x from the text file and track x in the playlist. Remember: AppleScript doesn't care what the track sounds like and therefore doesn't care what info gets sent to the track's tags.
Making Modifications
Here's how the script below works.
When you first run the script, the routines in the on run handler are executed. The script will get a reference to the playlist you have selected, then you will choose the text file. The text file is opened and a list is created, each item of which is a line from the text file. A repeat loop goes though each line one at a time. Each line is parsed by tab (in this script, anyway) and a list of the text items will be created. The list would look something like this: {"Coma Girl", "Joe Strummer & The Mescaleros","Streetcore","80"}. The distribute_the_data() handler is passed this list of data and the index of the track. It contains the statements that distribute the items in the list to the particular properties of the track
You will need to modify the distribute_the_data() handler to match your data. Here is that particular portion of the script:
to distribute_the_data(thisLineOfData, i) tell application "iTunes" set name of track i of thePlaylist to item 1 of thisLineOfData set artist of track i of thePlaylist to item 2 of thisLineOfData set album of track i of thePlaylist to item 3 of thisLineOfData set rating of track i of thePlaylist to (item 4 of thisLineOfData) as integer end tell end distribute_the_data
Remember, this handler works with each line of text file data and each corresponding track one at a time. Notice how a particular track property—name, for example—is assigned the value from a particular item from the thisLineOfData list. The index of the item corresponds to the order in which it appears in the line of data from the text file. So, item 2 is assigned to the artist, item 3 assigned to album, item 4 (coerced to an integer) is assigned to rating. Consult iTunes' AppleScript Dictionary for additional track property information.
Here is the script. You can click a link below to open it in Script Editor. Feel funny about that? Copy the text from this page and paste it into a Script Editor window.
-- global variable accessible to all handlers (subroutines) global thePlaylist -- this is the handler YOU must edit to distribute_the_data(thisLineOfData, i) tell application "iTunes" -- FOR EXAMPLE: -- track i is each selected track in order, no need to change that -- item 1, 2 ,3 , etc, corresponds to -- each item in the row of data from a text file -- YOU will have to arrange the data according -- to how your text file has been exported -- strings set name of track i of thePlaylist to item 1 of thisLineOfData set artist of track i of thePlaylist to item 2 of thisLineOfData set album of track i of thePlaylist to item 3 of thisLineOfData -- numbers set rating of track i of thePlaylist to (item 4 of thisLineOfData) as integer end tell end distribute_the_data -- this is what runs when you activate the script on run select_iTunes_playlist() set fileRef to choose file with prompt "Locate text file of track information..." tell application "Finder" try set fileRefr to (open for access fileRef) set thisInfo to (read fileRefr as list using delimiter linefeed) close access fileRefr on error m number n display dialog "There has been an error:" & return & return & m buttons {"Cancel"} default button 1 with icon 0 end try end tell tell application "iTunes" set trackcount to count tracks of thePlaylist if trackcount is (length of thisInfo) then set fixed indexing to true with timeout of 10000 seconds repeat with i from 1 to trackcount my distribute_the_data(my text_to_list((item i of thisInfo), tab), i) end repeat end timeout set fixed indexing to false -- finished display dialog "Done." buttons {"Thanks"} default button 1 with icon 1 giving up after 15 else display dialog "The number of entries in the text file is not the same as the number of tracks in the selected playlist." buttons {"Cancel"} default button 1 with icon 0 end if end tell end run to select_iTunes_playlist() tell application "iTunes" activate try set thePlaylist to a reference to (get view of front window) on error display dialog "Please select a playlist by clicking on it in the Source pane." buttons {"Cancel"} default button 1 with icon 2 giving up after 15 end try display dialog "You have selected playlist " & return & return & "\"" & (get name of thePlaylist) & "\"" & return & return & "If this is correct, click \"OK\"." end tell end select_iTunes_playlist -- converts a text string (txt) to a list delimited by given string (delim) 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