Search code examples
listcomparisonapplescriptitunesalias

Applescript: Finding a track in a playlist by it's file system path


Can't pick a track from a playlist based on it's file system path (location). See below.

I put arrows before the result of each command.

In the first line, I successfully find the first track from the playlist "Testing" with genre "R&B".

Why doesn't the fourth line work ? It looks like I am doing the same as line 1 only searching by file-system location instead of by genre.

Is there another way to do this ?

tell application "iTunes"....
  set t to first track of playlist "Testing" whose genre is "R&B"
   --> file track id 18476 of user playlist id 18414 of source id 72 of application "iTunes"

  set l to location of track 1 of playlist "Testing"
   --> alias "Lion:Users:cworth:Desktop:a.mp3"

  set f to POSIX file "/Users/cworth/Desktop/a.mp3" as alias
   --> alias "Lion:Users:cworth:Desktop:a.mp3"

  set tt to first track of playlist "Testing" whose location is f
   --> error "iTunes got an error: A descriptor type mismatch occurred." number -10001 to item
...end tell

Solution

  • Studied up on Applescript and got it working with an explicit loop over the tracks: (see below)

    A possible explanation for why the example above doesn't work is that the 'first track of playlist "Testing" whose location is...' returns a list of object specifiers (e.g. location of first track of playlist "Testing", etc.) instead of a list of actual locations - i.e. what's missing is the "get" command that returns the actual locations that you see inside the repeat loop below. That would explain the descriptor type mismatch, maybe ? I don't have the Applescript expertise or motivation to go any further, now that I've got it working, but I'm glad that it is!

    tell application "iTunes"
    set f to POSIX file "/Users/cworth/Music/04 It's A Man's, Man's, Man's World.mp3"
    set a to f as alias
    set z to missing value
    repeat with tr in tracks of playlist "Testing"
        set ll to get location of tr
        if ll is a then set z to ll
    end repeat
    end tell
    log z