Slow script on long lists - Make faster?

Thanks to Jacques (previous thread here → http://bbs.applescript.net/viewtopic.php?id=14328) I have this wonderful script that works fantastically for small lists. However, for large lists (like the master iTunes library - only 1000 songs here), it really bogs down.

sorry lost junky code

bonkers:

A simply way to speed up routines that need to access a long list is to have the action and comparison statements pointed to a reference to the list instead of the list itself. For instance, in your second repeat loop, replace the two instances of the term trk_list with [my trk_list]. That should speed things up considerably. There may be other places in your script that this will help, but I admit that this thing does not work on my machine, so I can’t test it out. Are you sure this script is working for you?

casdvm

casdvm - sorry for the non-working script. a revised one is below

i tried your suggestion, but it didn’t work.

run the script & then skip 2 or 3 songs & wait to see how long it takes to remove those songs from the list.

global trk_list
on run
	tell application "iTunes"
              activate
		set trk_list to get index of every track of library playlist 1
		
		repeat until trk_list is {}
			set limit to length of trk_list
			set dummyList to {}
			play track (item 1 of trk_list) of library playlist 1
			delay 10
			if index of current track is not (item 1 of trk_list) then
				set lst_plyd to index of current track
				repeat with i from 1 to limit
					set this_item to (item i of trk_list)
					if this_item < (item 1 of trk_list) then
						copy this_item to end of dummyList
					else
						if this_item > (index of current track) then
							copy this_item to end of dummyList
						end if
					end if
				end repeat
				set trk_list to dummyList
				log trk_list
			else
				set trk_list to rest of trk_list
			end if
		end repeat
		log trk_list
	end tell
end run
(* using this subroutine, when you skip a track or tracks it will remove those tracks from the master track list & countinue playing the master track list where things left off.
*)

here is the script with your suggestion regarding “a ref to”. i’m pretty sure this is the way to go (according to the AS Language Guide), but I don’t seem to be using it correctly because removing the skipped tracks takes forever still ~1 minute.

global trk_list
on run
	tell application "iTunes"
		activate
		set trk_list to get index of every track of library playlist 1
		set trk_listRef to a reference to trk_list
		
		repeat until trk_list is {}
			set limit to length of trk_list
			set dummyList to {}
			play track (item 1 of trk_list) of library playlist 1
			delay 10
			if index of current track is not (item 1 of trk_list) then
				set lst_plyd to index of current track
				repeat with i from 1 to limit
					set this_item to (item i of trk_listRef)
					if this_item < (item 1 of trk_listRef) then
						copy this_item to end of dummyList
					else
						if this_item > (index of current track) then
							copy this_item to end of dummyList
						end if
					end if
				end repeat
				set trk_list to dummyList
				log trk_list
			else
				set trk_list to rest of trk_list
			end if
		end repeat
	end tell
end run
(* using this subroutine, when you skip a track or tracks it will remove those tracks from the master track list & countinue playing the master track list where things left off.
*)

bonkers:

Thanks for the updated code; it now runs, but I still end up with two empty lists at the end of the run. Both trk_list and dummyList are {} when the playlist has been gone through. I am not sure what this sequence is supposed to do, but the way you have the ifs and the repeats set up, dummyList becomes every item in trk_list greater than the current track playing, trk_list is then set to dummyList, dummyList is reset to {}, and the next track played is the first one in the new trk_list, and the whole cycle starts over again. I am trying to see the point of all this, because no data is retained anywhere in the script, except the variable lst_plyed, and nothing is being done with that item.

I do believe there is a simpler and faster way to work through the playlist, so please explain what the overall goal is and let’s start from there.

casdvm

casdvm - are you looking at the event log while you are running the script? the dummyList is a dummy list, but the trk_list shows up nicely.

sorry if this is confusing to you, but the full script is a bit long to post in its entirety. i tried to explain the purpose of this script in the other thread (see above for link), but here is a summary.


make list1 consisting of every track in playlist x
make list2 by randomizing list1 (i have not included this portion)
play list2

if the user wants to skip a track or tracks, i want all those tracks removed from list2, so that playback continues without replaying a track that was skipped by the user (i hope this makes sense!)


jacques - thanks again for your help, but it will only work with songs played in consecutive order (1,2,3,…). i need it to work with a random ordered playlist.

bonkers:

I was running the script in my user that only has 11 tracks in the playlist to test it out, and I was running it under the Event Log to watch it. I should do a screenshot for you to see, but all I could ever get was the [get index] of whatever. When the [log trk_list] line came up, all that showed was the (**) sign for empty list. I put in a few display dialogs to watch it work in different areas, and could see the dummyList through that, so I know it exists sometimes.

This is a fascinating idea you have hit on, and it will most likely consume me for a few days. I looked at the original thread and now have a good idea of what you are trying to do. I will mess with this some more over the weekend and let you know if I get any brainstorms.

Craig Smith

Hmmm… Very strange.

See if the following works for you.

  1. create a new playlist with 13 tracks
  2. run my final script in the other thread
  3. when the track changes from #2 to #6 go ahead & push the ffwd button 3 times which should make the currently playing track #9
  4. look at the event log & you should see the new playlist as “(3, 5, 12, 1, 4, 11, 10, 13)”
  5. after track #9 plays the next track played should be #3, #5, etc.

let me know…

Jacques - I’m sure your script works, but I’m having trouble modifying it to work in the above script. Could you help me out a bit more?

cheers

still can’t get it to work. any suggestions?

I figured it out thanks to the FAQ http://macscripter.net/faq/get_the_faq.php?id=207_0_5_0_C .

global trk_list
on run
	tell application "iTunes"
		activate
		set trk_list to get index of every track of library playlist 1
		--randomize trk_list using HAS "unsortList" subroutine (not included here)
		
		repeat until trk_list is {}
			set limit to length of trk_list
			set dummyList to {}
			set rmvd_lst to {}
			play track (item 1 of trk_list) of library playlist 1
			delay 5
			if index of current track is not (item 1 of trk_list) then
				set y to index of current track
				set x to item 1 of trk_list
				repeat with i from x to y
					copy i to end of rmvd_lst
				end repeat
				repeat with i from 1 to limit
					if {trk_list's item i} is not in rmvd_lst then ¬
						set dummyList's end to trk_list's item i
				end repeat
				set trk_list to dummyList
			else
				set trk_list to rest of trk_list
			end if
		end repeat
		
	end tell
end run

bonkers:

Glad you got it worked out. I have been worrying about this all week. Nice work.

casdvm