PDA

View Full Version : Track Names and Numbers for Album


opticalalchemy
09-15-2004, 09:59 AM
I have an AppleScript that finds all the Track Names in an Album:


on tracksforalbumartist(theAlbum)
tell application "iTunes"
set fixed indexing to true
set theTracks to a reference to (tracks of library playlist 1 whose album is theAlbum)
set theTrackNames to name of theTracks
end tell
end tracksforalbumartist


It takes about 2 sec to search my 5000 song database.

Strangely, when I add Track Numbers to the script it takes
twice as long (4 sec). Add Track Times, takes 3 times as long, etc.

on tracksforalbumartist(theAlbum)
tell application "iTunes"
set fixed indexing to true
set theTracks to a reference to (tracks of library playlist 1 whose album is theAlbum)
set theTrackNames to name of theTracks
set theTrackNumbers to track number of theTracks
copy {theTrackNumbers, theTrackNames} to composite
end tell
end tracksforalbumartist


It seems that iTunes is doing the search for each element. ???
What am I doing wrong? Any ideas.
(The delay is a terrible hit in my realtime graphics application.
Any ideas to improve the speed is appreciated.)

Doug Adams
09-15-2004, 01:39 PM
You're not doing anything wrong. It's the nature of AppleScripts filtering. The more you add to the filter, the longer it will take.

A reference to a list will always go faster. See (http://developer.apple.com/documentation/AppleScript/Conceptual/AppleScriptLangGuide/AppleScript.99.html).

You may want to use iTunes' search instead, using "only albums" as the parameter.

By the way, it isn't necessary to set fixed indexing. You're not changing anything that will affect the indexing :)

opticalalchemy
09-15-2004, 04:11 PM
Thanks Doug.

I am already using a reference to a list... yes?
set theTracks to a reference to (tracks of library playlist 1 whose album is theAlbum)

I will try the "search" command, and see if I get quicker results.

Originally I didn't used fixed indexing, and the tracks came back out-of-order.
I know that fixed indexing is used to keep the indicies consistent during
add / delete operations. But I read somewhere that fixed indexing would give
me ordered results regardless of the list order selected by the user
(Song Name, Album, Artist, Track#).
This works 90% of time. Turns out that I get the order by Date-Added,
which is the order of the tracks on the Album, in 90% of my Library...
but maybe not everyones... Need a better way to do this.

Is there another way ?
Is there a way to click the order-by-track-number through AppleScript...
Haven't found it yet. That would work too.

Doug Adams
09-15-2004, 04:26 PM
I am already using a reference to a list... yes?
set theTracks to a reference to (tracks of library playlist 1 whose album is theAlbum)

No. Create the list then set another variable to a reference to that list.
tell application "iTunes"
set list1 to (file tracks of library playlist 1 whose album is someAlbumName)
set list2 to a reference to list1
-- do stuff with list2
end tell
Forget fixed indexing, you are not altering data so there is no need to fix the indices.

The order of tracks will be based on the sort order of the playlist.

opticalalchemy
09-15-2004, 05:33 PM
Can't quite figure this out.
I'm a programer, but new to AppleScript.
Syntax is still confusing me.

Refered to some samples scripts, to get the following.

on tracksforalbum(theAlbum)

tell application "iTunes"
set trackList to (file tracks of library playlist 1 whose album is theAlbum)
set trackListRef to a reference to trackList
set numTracks to (length of trackList)

set trackNames to {}
repeat with i from 1 to numTracks
set thisTrack to (item i of trackListRef)
tell thisTrack to set trackName to name
set end of trackNames to trackName
end repeat

end tell

end tracksforalbum

Getting NSCannotCreateScriptCommandError at "set thisTrack to (item i of trackListRef)"
Somethin' ain't right. Any tips ?

Thanks in Advance.

Doug Adams
09-15-2004, 06:58 PM
Hmm.

Drop the ref to variable. Strange it does not work. Works better without the "reference to".

I've had trouble with album name stuff in the past, as well. Oh well. Sorry to seemingly steer you wrong.

For what its worth, here's what I've been playing with lately; gettinga list of all albums:tell application "iTunes"

set lib to library playlist 1

set allAlb1 to (album of every file track of lib)

set albref to a reference to allAlb1

set albs to {}
set albsRef to a reference to albs

repeat with i from 1 to length of allAlb1
if item i of albref is not in albsRef then set end of albs to item i of albref
end repeat

log albsRef
end tell

opticalalchemy
09-15-2004, 11:07 PM
The Above Script finds all the Unique Albums in the Library... which is cool,
but not what I need :(
It runs in 10 sec on my library.


Still tryin' though.

Restating the goal: To provide an Album name and get back the
all the Track Names from that album. And (optionally) the Track Numbers for each.

on tracksforalbum( theAlbum )
tell application "iTunes"
set theTracks to a reference to (tracks of library playlist 1 whose album is theAlbum)
set theTrackNames to name of theTracks
set theTrackNumbers to track number of theTracks
copy {theTrackNumbers, theTrackNames} to composite
end tell
end tracksforalbum

Returns: {{1,2,3,4,5},{"Track 01", "Track 02", "Track 03", "Track 04", "Track 05"}}
Do it with just Track Names: 2 sec.
Add Track Numbers (as above): 4 sec.
Add Track Duration: 6 sec.
Add Played Count: 8 sec.
It is utterly consistent. (like it's doing the search for each added property)
I understand that it takes a couple secs to search the whole database and get
the Tracks back. But at that point (small list), it should be very fast
to extract the Track Numbers from the small list of tracks.

I think the answer lies in the reference concept, but since I'm
an AppleScript newbie, haven't quite been able to crack that nut!

Thanks.

*note* Previously I was getting the File Location of the current playing track,
and looked at the contents of that directory to find the other songs on the album.
It was instant and worked fine except that it mangled the track names when
there were accent characters, and the track numbers were not always appended
to the front of the file name, for proper ordering.

appologies for the extended response ;)

opticalalchemy
09-16-2004, 12:47 AM
OK got it! :)

on tracksforalbum(theAlbum)

set trackNames to {}
set trackNumbers to {}
set trackTimes to {}
set trackComposites to {}

tell application "iTunes"
set lib to library playlist 1

set theTracks to (tracks of lib whose album is theAlbum)
set theTracksRef to a reference to theTracks
set numTracks to length of theTracks

repeat with i from 1 to numTracks
set theTrack to (item i of theTracks)
set trackName to name of theTrack
set trackNumber to track number of theTrack
set trackTime to time of theTrack
set the end of trackNames to trackName
set the end of trackNumbers to trackNumber
set the end of trackTimes to trackTime
end repeat

copy {trackNames, trackNumbers, trackTimes} to trackComposites
end tell

end tracksforalbum

Runs in 2 sec regardless of how many properties are requested! Yipee.
Still doesn't use references to list. Bet some more performance could
be acheived, but this is good for me now.

Learning "log" command was the clincher.
Thanks Doug :)