Re: Get iTune File Track by Alias
Re: Get iTune File Track by Alias
- Subject: Re: Get iTune File Track by Alias
- From: Michael Terry <email@hidden>
- Date: Sun, 18 Apr 2004 21:33:27 -0700
On Apr 15, 2004, at 8:24 AM, Nigel Smith wrote:
Yes, what you tried should have worked. None of the alternatives I
thought of are very fun or fast. This is the best I could come up
with:
How about using a binary search? I don't know how fast this is for a
lot of
tracks -- I don't have many in iTunes, myself...
----------
tell application "iTunes"
set testFile to location of file track 15 of playlist "Library"
set theList to location of every file track of playlist "Library"
if theList contains testFile then
set trackNum to binSearch(testFile, theList, 1) of me
return file track trackNum of playlist "Library"
end if
end tell
on binSearch(aTerm, aList, aNumber)
if (count of aList) = 1 then
return aNumber
else
set midItem to (((count of aList) / 2) as integer)
copy items 1 thru midItem of aList to tmp
if tmp contains aTerm then
binSearch(aTerm, tmp, aNumber)
else
binSearch(aTerm, items (midItem + 1) thru -1 of aList,
<line-break>
aNumber + midItem)
end if
end if
end binSearch
----------
What's the speed like for you people with monster playlists?
Pretty awful, but significantly better than the one I posted. I've only
got about 650 songs in iTunes, but my handler was taking around 11
seconds per, while yours came in at around 6. It seemed to scale
proportionally, too, which I was surprised about. I thought 'contains'
got worse with bigger searches, but I took out half my music files and
tried again, and yours took 3 seconds, half as long.
No matter what priority you give execution speed, though, that's still
a tough pill to swallow. But then I remembered that recent versions of
iTunes keep the library organized such that for any track and
corresponding file, the song name can be derived from the file name and
the album name matches the file's folder's name. So I hope we can have
a natural and fast solution like the following[1]:
name of getTrackForAlias(choose file)
--> "Never Is A Promise"
on getTrackForAlias(aliasKey)
try
tell application "Finder" to tell {name, name extension, container's
name} of item aliasKey to set {targName, targAlbum} to {first item's
text 1 thru -((count middle item) + 2), last item}
tell application "iTunes" to return first file track of library
playlist 1 where its name is targName and its album is targAlbum
on error eMsg number eNum
error "Can't getTrackForAlias: " & eMsg number eNum
end try
end getTrackForAlias
This might not work on some previous versions of iTunes, but I'm not
sure.
Mike
[1] Or click here: <
http://tinyurl.com/2zvu8> to have the script open
in Script Editor 2 with line breaks as God intended.
_______________________________________________
applescript-users mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/applescript-users
Do not post admin requests to the list. They will be ignored.