How to download iTunes song (via iTunes Match) using AppleScript

applescriptitunesitunes-match

I'm trying to figure how to download the file for iTunes songs which are are not yet downloaded to my local machine (and reside in the iTunes Match Cloud) using AppleScript or any other library.

My goal is to have a script running locally on a cron that downloads any 5 star rated song. As I stream most of my music via iTunes Match, I want an automated way to download those high rated songs.

I looked around and figured something like this would work:

tell application "iTunes"
    activate
    set results to (every file track of playlist "Library" whose rating is 100)
    repeat with t in results
        download t
    end repeat
end tell

Although it just blows up:

error "iTunes got an error: item 1 of
  {file track id 86657 of library playlist id 61224 of source id 66, .... } 
  doesn’t understand the “download” message."

Any help would be appreciated.

Best Answer

tell application "iTunes"
  set matchedSongs to tracks of library playlist 1 whose rating is 100 and cloud status is matched
  set uploadedSongs to tracks of library playlist 1 whose rating is 100 and cloud status is uploaded

  set results to matchedSongs & uploadedSongs

  repeat with aTrack in results
    if class of aTrack is shared track then
      try
        download aTrack
      end try
    end if
  end repeat
end tell

This does the job!

I couldn't figure how to do a nested condition on the whose so I simply acquire both set of searches for matched and uploaded songs and combined them.

The try and if just ensure we download the valid tracks if they don't exist locally.