title: AppleScript to add multiple tracks from iTunes into a playlist using a text file as the source
body: <p>I have an iTunes playlist I previously backed up into a text file which is in the following format:</p>
  
  <p>"title","artist","track number","album"</p>
  
  <p>I created a sample file using four of these tracks:</p>
  
  <p>"Ritual","Chick Corea Elektric Band II","9","Paint The World"
  <br>"Risk","Deftones","9","Diamond Eyes"
  <br>"Risveglio","Goblin","10","Zombi"
  <br>"Ritual","Ashes Divide","8","Keep Telling Myself It's Alright"</p>
  
  <p>All the tracks from this playlist are currently in iTunes. I want to add each of these tracks into a playlist using AppleScript. I've been able to do it with a single item (example: title) with the following AppleScript:</p>
  
  <pre><code>-- set variables
  set srcFile to "/Users/kjesso/Documents/=Scripting=/AppleScript/ipod_gym_playlist_track_names_sample.txt"
  set allRecords to paragraphs of (read srcFile as «class utf8»)
  set myPlaylist to "Test"
  property okflag : false
  
  -- check if iTunes is running
  tell application "Finder"
      if (get name of every process) contains "iTunes" then ¬
          set okflag to true
  end tell
  if okflag then
      -- if iTunes is running then do this
      tell application "iTunes"
          repeat with aRecord in allRecords
              set results to (every file track of playlist "Library" whose name is aRecord)
              repeat with aTrack in results
                  duplicate aTrack to playlist myPlaylist
              end repeat
          end repeat
      end tell
  else
      -- if iTunes is not running do this
      return "Unable to execute because iTunes is not running"
  end if
  </code></pre>
  
  <p>However, if a duplicate track title is found from a different artist it will just take the first track since the script can't distinguish between different artists with only the "title" as the content. Do array's exist natively in AppleScript?</p>
  
  <p>I assume this would need to be done with a Property List file? Upon further reading online, trying to create an array to do what I wanted (capture the track title, artist, album, etc.), I came across various threads <a href="http://macscripter.net/viewtopic.php?id=39129" rel="nofollow">like this</a> stating it is better to use a Property List? I'm trying to achieve similar to what was done <a href="http://macscripter.net/viewtopic.php?id=42286" rel="nofollow">here</a>, but instead of sending the output to a CSV file I want to send it to a playlist in iTunes.</p>
  
  <p>I created the following sample property list file if I need to use a property list to achieve my goal:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"&gt;
  &lt;plist version="1.0"&gt;
  &lt;dict&gt;
      &lt;key&gt;title&lt;/key&gt;
      &lt;string&gt;"Ritual"&lt;/string&gt;
      &lt;key&gt;artist&lt;/key&gt;
      &lt;string&gt;"Chick Corea Elektric Band II"&lt;/string&gt;
      &lt;key&gt;album&lt;/key&gt;
      &lt;string&gt;"Paint The World"&lt;/string&gt;
  &lt;/dict&gt;
  &lt;dict&gt;
      &lt;key&gt;title&lt;/key&gt;
      &lt;string&gt;"Risk"&lt;/string&gt;
      &lt;key&gt;artist&lt;/key&gt;
      &lt;string&gt;"Deftones"&lt;/string&gt;
      &lt;key&gt;album&lt;/key&gt;
      &lt;string&gt;"Diamond Eyes"&lt;/string&gt;
  &lt;/dict&gt;
  &lt;dict&gt;
      &lt;key&gt;title&lt;/key&gt;
      &lt;string&gt;"Risveglio"&lt;/string&gt;
      &lt;key&gt;artist&lt;/key&gt;
      &lt;string&gt;"Goblin"&lt;/string&gt;
      &lt;key&gt;album&lt;/key&gt;
      &lt;string&gt;"Zombi"&lt;/string&gt;
  &lt;/dict&gt;
  &lt;dict&gt;
      &lt;key&gt;title&lt;/key&gt;
      &lt;string&gt;"Ritual"&lt;/string&gt;
      &lt;key&gt;artist&lt;/key&gt;
      &lt;string&gt;"Ashes Divide"&lt;/string&gt;
      &lt;key&gt;album&lt;/key&gt;
      &lt;string&gt;"Keep Telling Myself It's Alright"&lt;/string&gt;
  &lt;/dict&gt;
  &lt;/plist&gt;
  </code></pre>
  
  <p>Anyone have any ideas on how to get this to work?</p>