2

For any given track in my iTunes library, I can easily ask for the physical location of the file like this,

tell application "iTunes"
    set ilib to library playlist named "Library"    
    set z to (get first track in ilib)
    get location of z
end tell

But I want to do the reverse. If I know the physical location of the file, I'd like to get to the track in the Library, if it has been added. I tried it like this

set x to (choose file)
tell application "iTunes"
    set ilib to library playlist named "Library"
    set y to (get track in ilib whose location is x)
end tell

but it fails...

error "iTunes got an error: Can’t get track of library playlist
id 6085 of source id 77 whose location = alias \"OS X 
Partition:Users:...:somefile.m4a\"." number -1728 from track of 
library playlist id 6085 of source id 77 whose location = alias 
"OS X Partition:Users:...:file.m4a"

Any ideas how I can search the location property without resorting to some nasty loop?

EDIT:

Well, the good news is that my old macbook can loop through 2100 tracks in about 5 secs, so it is not so bad... :)

1 Answer 1

2

This won't do precisely what you ask, but it will provide the track reference in the variable "trackRef" from the file location specified by "filepath" if it is in the library. If the file is not in the library, it will be added.

tell application "iTunes"
    try
        set trackRef to (add POSIX file "filepath" as alias)
        refresh trackRef
    end try
end tell

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .