strange problems with Applescript in iTunes

Hi,

I wrote a small script that gets all song from the album playing, sorts them and puts them into one string.
When I tested the script it worked fine, but when I ran it from an iTunes Plug-in it messes up the whole iTunes Applescript:
First the same script won’t run again from the Script editor (suddenly the first line sent to iTunes creates an inunderstandable error) when inside any visualization (e.g. Apple’s).
Then not even Frontrow is able to communicate with iTunes any more, not even after I restarted iTunes.

Can someone please take a look at the script and tell me why it might mess up things.
Thank you!

(If the script is flawless then it might be a problem of running script from inside iTunes.)

Greeting from Germany
Christoph Vogelbusch

Here is my script:

tell application "iTunes"
	set theAlbum to the album of current track
	set allTracks to search library playlist 1 for theAlbum only albums
	
	set trackNameList to {}
	repeat (the number of items in allTracks) times
		set noOfLowest to 1
		set lowestTrackNo to track number of (item noOfLowest of allTracks)
		repeat with i from 2 to (the number of items in allTracks)
			if track number of (item i of allTracks) < lowestTrackNo then
				set noOfLowest to i
				set lowestTrackNo to track number of (item noOfLowest of allTracks)
			end if
		end repeat
		set lowestTrack to item noOfLowest of allTracks
		if noOfLowest is 1 then
			if not (the number of items in allTracks is 1) then
				set allTracks to (items 2 thru -1 of allTracks)
			end if
		else if noOfLowest is (the number of items in allTracks) then
			set allTracks to (items 1 thru (noOfLowest - 1) of allTracks)
		else
			set allTracks to (items 1 thru (noOfLowest - 1) of allTracks) & (items (noOfLowest + 1) thru -1 of allTracks)
		end if
		if track number of lowestTrack is 0 then
			set end of trackNameList to name of lowestTrack
		else
			set end of trackNameList to (track number of lowestTrack) & ". " & (name of lowestTrack) as string
		end if
	end repeat
	
	set allTrackNames to 0
	repeat with trackName in trackNameList
		if allTrackNames is 0 then
			set allTrackNames to trackName
		else
			set allTrackNames to allTrackNames & return & trackName
		end if
	end repeat
end tell
return allTrackNames

Are you sorting track by track number? When I use either of these scripts, the results are already in the proper order.

set allTracks to search library playlist 1 for theAlbum only albums

set allTracks to name of tracks of library playlist 1 whose album is theAlbum

If the items are sorted correctly, then something like this should work:

tell application "iTunes"
	set currentAlbum to album of current track
	set allTrackNames to name of tracks of library playlist 1 whose album is currentAlbum
	
	set ASTID to AppleScript's text item delimiters
	set AppleScript's text item delimiters to {return}
	set allTrackNames to allTrackNames as Unicode text
	set AppleScript's text item delimiters to ASTID
end tell

return allTracks

Yes, I am sorting by track number.
Originally they have the order that is set in the Library.

Your script is very interesting.
What I did was create strings that include the track number and the name “1. A first song” like (and then attached them to a ling string including “returns”.
What I want is actually would like is that the result from the AppleScript that is delivered to my Cocoa Application is an array that includes the names of the current album sorted by the track number.

but I don’t know how to return a NSArray of NSString.

I hink I will go with your approach and leave the sorting out for the moment, if this solves the main problem and hope for a good sorted solution.

Greeting from Germany
Christoph Vogelbusch