iTunes - Creating Playlists from folders

Hi,

I’m a new Mac user comming from caos (windows).

I’m trying to re-create all my playlists from my Harddrive, in the same order as Folders but…

Here is the Script (is a copy of the script “Add to iTunes Library”), it work with only one folder but when I drop more than one… die!!!

Could any of you give me a hand with it?

Thanks in advance.

global counter, playlist_status, this_playlist

on run
display dialog “Add to Itunes Library” & return & return & ¬
"Drag files or folders of files onto this droplet to add them to your iTunes library. " & ¬
return & return & ¬
“Supported file types include AIFF, MIDI, and MP3 files.” buttons {“OK”} default button 1
end run

on open these_itemsP
repeat with i from 1 to the count of these_itemsP
set this_item to (item i of these_itemsP)
process_general(this_item)
end repeat
display dialog (the counter as string) & " items have been added to the iTunes library." buttons {“¢”} default button 1 giving up after 3
end open

on process_general(these_items)
create_playlist2(these_items)
set counter to 0
repeat with i from 1 to the count of these_items
set this_item to (item i of these_items)
set the item_info to info for this_item
if folder of the item_info is true then
process_folder(this_item)
else if (alias of the item_info is false) and ¬
((the file type of the item_info is in the type_list) or ¬
the name extension of the item_info is in the extension_list) then
process_item(this_item)
end if
end repeat
end process_general

on process_folder(this_folder)
set these_items to list folder this_folder without invisibles
repeat with i from 1 to the count of these_items
set this_item to alias ((this_folder as text) & (item i of these_items))
set the item_info to info for this_item
if folder of the item_info is true then
process_folder(this_item)
else if (alias of the item_info is false) and ¬
((the file type of the item_info is in the type_list) or ¬
the name extension of the item_info is in the extension_list) then
process_item(this_item)
end if
end repeat
end process_folder

on create_playlist2(lista)
set playlist_status to “OK”
set the listaS to (displayed name of (info for (lista)))
tell application “iTunes”
launch
set this_playlist to make new playlist
set the name of this_playlist to the listaS
end tell
end create_playlist2

on process_item(this_item)
try
tell application “iTunes”
launch
set this_track to add this_item to playlist “Library” of source “Library”
if the playlist_status is “OK” then
duplicate this_track to this_playlist
end if
end tell
set counter to counter + 1
end try
end process_item

There is a nice droplet script at Doug’s AppleScripts for iTunes that should do the trick for you. The name is Playlist Creator and this is the link.

And, just for fun, I edited your original script so that it now functions correctly as a droplet:

global counter
on run
	display dialog "Add to Itunes Library" & return & return & ¬
		"Drag files or folders of files onto this droplet to add them to your iTunes library. " & ¬
		return & return & ¬
		"Supported file types include any type of Audio file." buttons {"OK"} default button 1
end run

on open these_itemsP
	set counter to 0
	tell application "iTunes"
		if not (exists playlist "Unassigned") then
			make new playlist with properties {name:"Unassigned"}
		end if
	end tell
	repeat with i from 1 to the count of these_itemsP
		set this_item to (item i of these_itemsP)
		if folder of (info for this_item) then
			ProcessFolder(this_item)
		else
			ProcessFile(this_item, "Unassigned")
		end if
	end repeat
	display dialog (the counter as string) & " tracks have been added to the iTunes library." buttons {"¢"} default button 1 giving up after 3
end open

to ProcessFolder(mf)
	set fa to (displayed name of (info for (mf)) as string)
	tell application "iTunes"
		if not (exists playlist fa) then
			make new playlist with properties {name:fa}
		end if
	end tell
	tell application "Finder" to set mf_contents to every file in folder mf whose kind contains "Audio"
	repeat with afi in mf_contents
		ProcessFile(afi, fa)
	end repeat
end ProcessFolder

to ProcessFile(m_track, play_list)
	try
		tell application "iTunes"
			add (m_track as alias) to playlist play_list
		end tell
		set counter to counter + 1
	end try
end ProcessFile

Good luck,

thanks Craig, but the script from Doug doesn´t work if you drop more than one folder into the script.

The one that you post create the playlist but doesn´t add the files to it when you have more folders inside.

The Disk that I’m trying to catalog has this structure: Artist / Albums / Files but sometimes is just Artist / Files

Here is an example about the disk structure, the main problem is that the playlist names must be only the first level of folders.

Thanks to everyone.

Hi,

borrowing some lines from Craig’s script,
here is a solution using the shell command find, which is rather the fastest way to import the data.
Also an advantage is, that find searches recursive in folders.
The search is performed for name extensions mp3, m4a, m4b, aif(f) and mid

global counter

on run
	display dialog "Add to Itunes Library" & return & return & ¬
		"Drag files or folders of files onto this droplet to add them to your iTunes library. " & ¬
		return & return & ¬
		"Supported file types include any type of Audio file." buttons {"OK"} default button 1
end run

on open theseItems
	set counter to 0
	tell application "iTunes" to if not (exists playlist "Unassigned") then make new playlist with properties {name:"Unassigned"}
	
	repeat with oneItem in theseItems
		set {name:fName, folder:isfolder} to info for oneItem
		if isfolder then
			set POS to quoted form of POSIX path of oneItem
			set theFiles to paragraphs of (do shell script "find " & POS & " -type f ! -name '.*' | awk '/mp3/||/aif/||/mid/||/m4a/||/m4b/'")
			tell application "iTunes" to if not (exists playlist fName) then make new playlist with properties {name:fName}
			
			repeat with i in theFiles
				set oneFile to POSIX file i as alias
				ProcessFile(oneFile, fName)
			end repeat
		else
			ProcessFile(oneItem, "Unassigned")
		end if
	end repeat
	display dialog (the counter as string) & " tracks have been added to the iTunes library." buttons {"¢"} default button 1 giving up after 3
end open

to ProcessFile(m_track, play_list)
	try
		tell application "iTunes" to add m_track to playlist play_list
		set counter to counter + 1
	end try
end ProcessFile

Yes, I thought about nested folders after I posted that droplet. You should be able to use entire contents of the folders dropped onto the droplet in this manner:

global counter
on run
	display dialog "Add to Itunes Library" & return & return & ¬
		"Drag files or folders of files onto this droplet to add them to your iTunes library. " & ¬
		return & return & ¬
		"Supported file types include any type of Audio file." buttons {"OK"} default button 1
end run

on open these_itemsP
	set counter to 0
	tell application "iTunes"
		if not (exists playlist "Unassigned") then
			make new playlist with properties {name:"Unassigned"}
		end if
	end tell
	repeat with i from 1 to the count of these_itemsP
		set this_item to (item i of these_itemsP)
		if folder of (info for this_item) then
			ProcessFolder(this_item)
		else
			ProcessFile(this_item, "Unassigned")
		end if
	end repeat
	display dialog (the counter as string) & " tracks have been added to the iTunes library." buttons {"¢"} default button 1 giving up after 3
end open

to ProcessFolder(mf)
	set fa to (displayed name of (info for (mf)) as string)
	tell application "iTunes"
		if not (exists playlist fa) then
			make new playlist with properties {name:fa}
		end if
	end tell
	tell application "Finder" to set mf_contents to every file in entire contents of folder mf whose kind contains "Audio"--This should return all the files in all the nested folders
	repeat with afi in mf_contents
		ProcessFile(afi, fa)
	end repeat
end ProcessFolder

to ProcessFile(m_track, play_list)
	try
		tell application "iTunes"
			add (m_track as alias) to playlist play_list
		end tell
		set counter to counter + 1
	end try
end ProcessFile

Although I have not had time to test it today. The only advantage that this has over Stefan’s method of finding files via a shell script is that I use the Finder to get any and all Audio files without having to try to specify all possible file extensions. Other than that, as long as this one works, either droplet should do the trick for all the nested folders, although Stefan’s may indeed be faster.

Hi Craig,

checking whether the kind of the file contains “Audio” finds all audio files but not the MIDI files.
Assuming the hard disk is indexed by spotlight this shell equivalent does the same but is incredibly faster
with many nested folders


...
set theFiles to paragraphs of (do shell script "mdfind -onlyin  " & quoted form of POSIX path of POS & " 'kMDItemKind = \"*MID*\" || kMDItemKind = \"*Audio*\"'")
...

Hey Stefan, that is cool. It’s nice to have you musician/scripting types around for stuff like this!

Thanks Guys,

Both solutions worked perfect!!!

I’ll continue experimenting with applescript… it’s really amazing what you can do!! Probably the same as Visual Basic but without worring about licensing and $$$$$!!

Thanks again,