Correspondent Wayne B. recently wondered if there is a way to get a reference to a track in iTunes based on its file path. Well, unfortunately, you can’t do something like this:
tell application "iTunes" set trackRef to (get some file track of library playlist 1 whose location is "some/file/path.ext") end tell
That will generate an error. But you can trick iTunes into giving you the track reference by using the add command–if you are certain the file is already in iTunes’ database. When you add the file iTunes will check its database for the the corresponding library track for you and if it exists will give up the reference–and it won’t re-add it:
set theFile to choose file -- or however you get the file path tell application "iTunes" try set trackRef to (add theFile as alias) tell trackRef log (get name) # and so on... end tell end try end tell
But remember that if the file isn’t already in iTunes’ database then iTunes will add the file as a matter of course, which may not be what you want. So this trick may work best only when you know a file is currently in your library.
You will not want to use the open command instead of add, because open will compel iTunes to play the file in addition to any add-housekeeping.
Looks like some minor fixes are available in iTunes 10.1.1.