dougscripts.com

December 28 2010 - 1:19 pm

Get a Track Reference from a File Path

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.

Site contents © 2001 - 2024 (that's right: 2001) Doug Adams and weblished by Doug Adams. Contact support AT dougscripts DOT com. About.
All rights reserved. Privacy.
AppleScript, iTunes, iPod, iPad, and iPhone are registered trademarks of Apple Inc. This site has no direct affiliation with Apple, Inc.
The one who says "it cannot be done" should not be interrupting the one who is doing it.