What do the pro's mean to my itunes script?

hey guys

Well I don’t have a real question, because the script works basically, but I would like to know what you think of my script and what I can do better. I’m new to AS and I thought why not post my script here and let you judge about it. I’m sure there are a lot of things I can change to make it faster, easier and improve functionality…don’t hold back!

What does the script?
It combine discs in itunes. You know these double CD’s with “disc 1” and “disc 2” in the album names. My script takes those songs (well you have to select them, and tell how many discs you have in total) and combines them to one album.

you get something like this:

number 1: song 1 of disc 1 of album “blabla”
number 2: song 2 of disc 1 of album “blabla”
number 3: song 3 of disc 1 of album “blabla”
number 1: song 1 of disc 2 of album “blabla”
number 2: song 2 of disc 2 of album “blabla”
number 3: song 3 of disc 2 of album “blabla”
number 4: song 4 of disc 2 of album “blabla”
.
.
.

tell application "iTunes"
	
	
	--some variables
	set theAlbums to {}
	set theAlbums_Ref to a reference to theAlbums
	set theFiles to the selection
	set theFiles_Ref to a reference to theFiles
	set theNumber to (count of theFiles)
	set last_tracks to {}
	
	
	--create the list with the album names of each track
	repeat with counter_a from 1 to theNumber
		copy (album of item counter_a of theFiles_Ref) to the end of theAlbums_Ref
	end repeat
	set album_one to item 1 of theAlbums_Ref
	
	
	--ask for the new album name and number of discs
	display dialog "Set Name of Album" default answer album_one buttons {"Cancel", "OK"} default button 2
	copy the result as list to {album_name, button_pressed_1}
	display dialog "How many discs?" default answer "2" buttons {"Cancel", "OK"} default button 2
	copy the result as list to {disc_count, button_pressed_2}
	
	
	--make sample list with length=disc_count
	repeat with counter_ab from 1 to disc_count
		copy counter_ab to the end of last_tracks
	end repeat
	
	
	--search the changes between discs, write number of first tracks to last_tracks (at the position 3 for disc 3...for example)
	repeat with break_counter from 1 to disc_count
		
		repeat with counter_b from (((item break_counter of last_tracks) as integer) + 1) to theNumber
			if (item counter_b of theAlbums_Ref) = (item (counter_b - 1) of theAlbums_Ref) then
				set (item break_counter of last_tracks) to (((item break_counter of last_tracks) as integer) + 1)
			else if (item counter_b of theAlbums_Ref) ≠ (item (counter_b - 1) of theAlbums_Ref) then
				exit repeat
			end if
		end repeat
		
		if (break_counter + 1) > (count of last_tracks) then
			exit repeat
		else
			set (item (break_counter + 1) of last_tracks) to (((item break_counter of last_tracks) as integer) + 1)
		end if
	end repeat
	
	
	(*
	just some lines of an older version of this script
	
	--some warnings, break_1 is now the track number of the last track of disk 1
	if break_1 = 1 then
		display alert "You've only selected 1 File!"
	else if break_1 = theNumber then
		display alert "You've selected only files from one Disk!"
	end if
	*)
	
	
	--to make the next step easier
	set extended_last_tracks to {0} & last_tracks
	
	
	--make the changes to track number, disc number, disc count, album	
	repeat with counter_c from 1 to disc_count
		set last_track_of_previous_disc to (item counter_c of extended_last_tracks)
		repeat with counter_d from (last_track_of_previous_disc + 1) to ((item (counter_c + 1) of extended_last_tracks))
			copy ((counter_d - last_track_of_previous_disc) as integer) to (track number of item counter_d of theFiles_Ref)
			copy (counter_c as integer) to (disc number of item counter_d of theFiles_Ref)
			copy (disc_count as integer) to (disc count of item counter_d of theFiles_Ref)
			copy (album_name) to (album of item counter_d of theFiles_Ref)
		end repeat
	end repeat
end tell

Thanks a lot!

Simon

Hi Simon,

all your counter and index variables are integer numbers,
so you can omit all as integer coercions

Just nitpicking here but you can change the above section to the following. There’s no reason to create variables for the buttons pressed if you don’t need them. It’s just wasting time and memory. Plus, the way you did it the result was already a list so there was no need for the “as list” coercion.

--ask for the new album name and number of discs
display dialog "Set Name of Album" default answer album_one buttons {"Cancel", "OK"} default button 2
set album_name to text returned of result
display dialog "How many discs?" default answer "2" buttons {"Cancel", "OK"} default button 2
set disc_count to text returned of result