help with iTunes script

set ifolder to "Mac HD:Miro Downloads:"
tell application "Finder" to set thefolders to every folder of folder ifolder
tell application "Finder"
	set thefiles to every file of folder 1 of folder ifolder
end tell
repeat with afile in thefiles
	set afile to afile as alias
	tell application "iTunes"
		add afile to playlist "ABC" of source "Library"
	end tell
end repeat

I am currently using this script on only one folder as you can see in

set thefiles to every file of folder 1 of folder ifolder

I want to use it on all folders of ifolder. (I don’t want to use folder actions because I will have to attach a folder action every time a new folder is created in ifolder)

I have the following problems:

  1. I have used playlist “ABC” above but actually every folder’s tracks must go in a separate playlist in iTunes.
    e.g. folder name is AB_C then the tracks in it should go in playlist ABC

folder name is X_Y-- then the tracks in it should go in playlist XYZ.

This will require creating two lists – one containing the names of folders and the other containing the names of playlists. I want to relate these two lists to each other.

  1. I want to check if the track already exists in the playlist so that it is not duplicated.

Thanks

Hm, nice Idea…
this script scans your download for any folders containing media data that iTunes can play,
and adds them to a corresponding playlist within iTunes
duplicates will be removed and playlist names no not contain _
just give it a try and see =)

-- hubionmac.com 10.10.2009
-- scans download folder for folders containing media data iTunes can play
-- creates a corresponding playlist within itunes
property special_bigChars : {"Ä", "Å", "Ç", "É", "Ñ", "Ö", "Ü", "À", "Ã", "Õ", "Ÿ", "Â", "Ê", "Á", "Ë", "È", "Í", "Î", "Ï", "Ì", "Ó", "Ô", "Ò", "Ú", "Û", "Ù"}
property special_smallChars : {"ä", "å", "ç", "é", "ñ", "ö", "ü", "à ", "ã", "õ", "ÿ", "â", "ê", "á", "ë", "è", "í", "î", "ï", "ì", "ó", "ô", "ò", "ú", "û", "ù"}
property action_list02 : {"01-Abcd Efg", "02-Abcd efg", "03-ABCD EFG", "04-abcd efg"}

global folder2Scan, actionid


tell application "Finder"
	set actionid to (my get_selection_index(action_list02, "Format of Playlist_names (cancel for no changes)", false))
	
	set folder2Scan to folder "Downloads" of home
	
	set thefolders to every container of folder2Scan
	
	--get my download folder
	set thefolders_list to my makeDownloadPlaylist()
	
	--clean the folderlist from old playlist // option dialog would be nice
	my clean_iTunes_folder(thefolders_list)
	-- make a folder playlist called itunes or get reference to it	
	repeat with thefolder in thefolders
		set foldername to my do_string(actionid, my replace_chars((name of thefolder), "_", " "))
		--now the script loops through every folder, creates a corrensponding playlist
		-- if there created playlist is empty after all, it will be removed again =)
		tell application "iTunes"
			set current_playlist to my check_playlist(thefolders_list, foldername)
			add (thefolder as alias) to current_playlist
			if (count of every track of current_playlist) = 0 then
				delete current_playlist
			else
				--clean the playlist from duplicates
				my cleanPlaylist(current_playlist)
			end if
		end tell
	end repeat
	
end tell

display dialog "done...
***commerical***
created by hubionmac.com :-P
***commerical***"


on check_playlist(folderplaylist, playlistname2check)
	tell application "iTunes"
		set thelists to every user playlist of source 1 whose special kind is none and smart is false
		set fpl_lists to {}
		repeat with i in thelists
			try
				if parent of i = folderplaylist then
					set fpl_lists to fpl_lists & {i}
				end if
			end try
		end repeat
		repeat with i in fpl_lists
			if (name of i) as text = playlistname2check then
				return i
			end if
		end repeat
		make new playlist with properties {name:playlistname2check} at folderplaylist
	end tell
end check_playlist

on makeDownloadPlaylist()
	tell application "iTunes"
		--	set thelists to name of every user playlist of source 1 whose special kind is none and smart is false
		if (count of every folder playlist) = 0 or (name of every folder playlist) does not contain "Downloads" then
			tell source 1 to make new folder playlist with properties {name:"Downloads"}
			set fpl to folder playlist "Downloads" of source 1
		else
			set fpl to folder playlist "Downloads" of source 1
		end if
		return fpl
	end tell
end makeDownloadPlaylist

on cleanPlaylist(theplaylist)
	tell application "iTunes"
		set idlist to {}
		repeat with i from the (count of tracks) of theplaylist to 1 by -1
			set currentID to database ID of track i of theplaylist
			if currentID is in the idlist then
				delete track i of theplaylist
			else
				if ((location of track i of theplaylist) as text) starts with folder2Scan then
					set idlist to idlist & {currentID}
				else
					delete track i of theplaylist
				end if
			end if
		end repeat
	end tell
end cleanPlaylist


on replace_chars(this_text, search_string, replacement_string)
	if this_text contains the search_string then
		set AppleScript's text item delimiters to the search_string
		set the item_list to every text item of this_text
		set AppleScript's text item delimiters to the replacement_string
		set this_text to the item_list as string
		set AppleScript's text item delimiters to ""
	end if
	return do_string(actionid, this_text)
end replace_chars



on do_string(actionid, the_string)
	if actionid = {} then
		return the_string
	else
		set actionid to actionid as integer
		set finalstring to ""
		if actionid = 1 then
			repeat with i from 1 to count of characters of the_string
				set test_char to character i of the_string
				if i = 1 then
					set finalstring to makebig(test_char)
				else
					set prev_char to character (i - 1) of the_string --last character of finalstring as text
					if followed_by_bigchar(prev_char) = true then
						set finalstring to finalstring & makebig(test_char)
					else
						set finalstring to finalstring & makesmall(test_char)
					end if
				end if
			end repeat
		else if actionid = 2 then
			set firstchar to makebig(character 1 of the_string)
			repeat with i from 2 to count of characters of the_string
				set test_char to character i of the_string
				set finalstring to finalstring & makesmall(test_char)
			end repeat
			set finalstring to (firstchar & finalstring) as text
		else if actionid = 3 then
			repeat with i from 1 to count of characters of the_string
				set test_char to character i of the_string
				set finalstring to finalstring & makebig(test_char)
			end repeat
		else if actionid = 4 then
			repeat with i from 1 to count of characters of the_string
				set test_char to character i of the_string
				set finalstring to finalstring & makesmall(test_char)
			end repeat
		end if
		return finalstring
	end if
end do_string



on followed_by_bigchar(test_char)
	if ((ASCII number of test_char) is greater than 64) and ¬
		((ASCII number of test_char) is less than 91) then
		return false
	else if ((ASCII number of test_char) is greater than 96) and ¬
		((ASCII number of test_char) is less than 123) then
		return false
	else if special_bigChars contains test_char then
		return false
	else if test_char = "'" then
		return false
	else
		return true
	end if
end followed_by_bigchar


on makesmall(test_char)
	--when it's a normal character
	if ((ASCII number of test_char) is greater than 64) and ¬
		((ASCII number of test_char) is less than 91) then
		return (ASCII character ((ASCII number of test_char) + 32))
		--when it's a special character
	else if test_char is in special_bigChars then
		repeat with i from 1 to count of special_bigChars
			if item i of special_bigChars = test_char then
				return item i of special_smallChars
			end if
		end repeat
		--when it's something else
	else
		return test_char
	end if
end makesmall

on makebig(test_char)
	--when it's a normal character
	if ((ASCII number of test_char) is greater than 96) and ¬
		((ASCII number of test_char) is less than 123) then
		return (ASCII character ((ASCII number of test_char) - 32))
		--when it's a special character
	else if test_char is in special_smallChars then
		repeat with i from 1 to count of special_bigChars
			if item i of special_smallChars = test_char then
				return item i of special_bigChars
			end if
		end repeat
		--when it's something else
	else
		return test_char
	end if
end makebig

on get_selection_index(action_list, theprompt, mult_selection)
	set theselection to choose from list action_list multiple selections allowed mult_selection with prompt theprompt
	set returnlist to {}
	repeat with theselected in theselection
		set i to 1
		repeat with theaction in action_list
			if theselected as text = theaction as text then
				set returnlist to returnlist & i as list
			end if
			set i to i + 1
		end repeat
	end repeat
	return returnlist
end get_selection_index

on clean_iTunes_folder(folder_playlist_2_clean)
	tell application "iTunes"
		set thelists to every user playlist of source 1 whose special kind is none and smart is false
		repeat with i in thelists
			try
				if parent of i = folder_playlist_2_clean then
					delete i
				end if
			end try
		end repeat
	end tell
end clean_iTunes_folder

Great, thanks. I will be able to modify it to suit my needs after studying the pattern of naming of folders in depth. I did not do it earlier because I did not anticipate that anyone would take such a deep interest in my problems.
I was expecting two different lists–one of playlist names and the other of folder names and linking them sequentially------meaning item 1 of one list would correspond to item 1 of other list. But you have given me a better solution, so that I don’t have to manually add items in those two lists with time.
Many thanks for your efforts, again, hubionmac.