Non-filepath-dependent exported playlists droplet

GO TO ADMIN PANEL > ADD-ONS AND INSTALL VERTIFORO SIDEBAR TO SEE FORUMS AND SIDEBAR

S2_Mac

New member
Joined
Oct 24, 2006
Messages
4,878
Points
0
Location
About 3 feet in front of the monitor
As noted in this thread on exporting iPod playlists, iTunes doesn't actually require filepaths when importing an iTunes-exported playlist; track, artist, and album names that match existing iTunes tracks are good enough. w00t! I wrote a couple of lines of Perl to strip filepath info (and also smart criteria) from XML playlist exports, and wrapped it in Applescript to deal with choosing and writing the xml files. Works great for me.

Feel free to use it, but note that I've only tested it with Leopard. I see no reason it won't work under Snow Kitty or Lion, but with Apple you never know...<g>

To use the code, paste everything below into a Script Editor window; then save as an Application Bundle (no Stay Open, no Run Only, no Startup Screen -- this is intended as a simple "droplet" application). Then, drop one or more exported-as-XML playlists onto the app and see what you get....

NOTE: this droplet overwrtes the files that are dropped onto it, so always work from copies of your important exported playlists....

Code:
(*
	Exported Playlist Path Stripper v0.6
	by S2, August 14, 2011
	=--=--=--=--=--=--=--=--=--=--=--=
	This droplet strips all the filepath info from playlists exported as XML files. 
	Once filepaths are removed, the exported playlist can be imported into any 
	other iTunes that contains the same tracks, no matter where the files are 
	located.. (Specifically, if tracks with the same name, artist, and album tags 
	exist in the other iTunes, the playlist will fully import.)
	
	The dropet also strips all "smart info" from an exported playlist, allowing the 
	contents of smart playlists to be imported without the baggage of the smart 
	criteria changing the list.
	
	This can be handy for replacing playlists when an iTunes library file has been 
	lost or improperly transferred to another computer, requiring the creation of 
	a new library or the use of different filepaths. (Of course, the playlists must 
	have been exported before the library file was lost or transferred ;-) It's also 
	useful for sharing playlists among friends who own the same CDs, since the 
	playlist no longer depends on specific filepaths.
	
	The droplet only works on exported iTunes playlists that have been saved as 
	xml files. One or more such files can be dropped onto the app; dropped foders 
	will be ignored. When run as a script (or when double-clicked as an app), a 
	single exported xml playlist can be processed through an Open File dialog.
	
	NOTE: this droplet =overwrites= the contents of the exported playlist files; 
	you should always work on =copies= of your exported playlists.
	
	For questions, comments, or bugs, post at the iLounge AppleScript board at:
	http://forums.ilounge.com/forumdisplay.php?f=62
	
	This script is released under the MIT license.
	
*)


global src_path

-- user is running the droplet as a script or as a double-clicked program
on run
	try
		set src_file to choose file "Choose an XML playlist exported from iTunes:" of type {"public.text"} without invisibles
	on error errText number errNum
		-- seems doubtful we'll ever get a real error here, but just in case....
		-- display message for any error other than -128 "user cancelled"
		if errNum is not -128 then
			display dialog ("Could not locate the selected file:" & return & "  Error " & (errNum as text) & return & "  " & errText) buttons {"Cancel"} default button 1
		end if
		return
	end try
	-- pass the alias returned by Choose File to the Open command
	open {src_file}
end run

-- user has dropped *something* onto this app
-- (one or more files, one or more folders, mix of files/folders)
on open dropped_items
	set success_flag to false
	repeat with this_item in dropped_items
		set err_flag to false
		set this_info to (info for this_item without size)
		if folder of this_info is false then
			if name extension of this_info is "xml" then
				-- convert alias to a path that Perl can understand
				set src_path to (escape_spaces(POSIX path of (this_item as text))) as string
				
				-- store the result of the perl routine...
				set stripped_xml to strip_paths()
				
				-- ...and use it to overwrite the source file
				try
					set file_handle to open for access (this_item as alias) with write permission
				on error
					set err_flag to true
					display dialog "Couldn't open the file “" & name of this_info & "” for writing; skipping this file" buttons "Bummer" default button 1 with icon 2
				end try
				if not err_flag then
					try
						set eof of file_handle to 0 -- overwrite the file
						write stripped_xml to file_handle starting at eof
						close access file_handle
						set success_flag to true -- set flag on any success
					on error
						close access file_handle
					end try
				end if
			end if
		end if
	end repeat
	
	-- alert user to overall fail
	if not success_flag then
		display dialog "Oops!" & return & "No files were processed. Make sure you select exported iTunes playlists saved as .xml files." buttons {"OK"} default button 1
	end if
end open

on strip_paths()
	set the_cmd to "perl -e 'local $/ = undef; my $s = <>;
$s =~ s:^\\s*<key>Location</key><string>(.*?)</string>\\n::mg;
$s =~ s:\\s*<key>Smart Info</key>.*(?=\\n\\s+<key>Playlist Items</key>)::sg;
print $s;' " & src_path
	return do shell script the_cmd
end strip_paths

on escape_spaces(txt)
	set {TID, text item delimiters} to {text item delimiters, " "}
	set {txt_items, text item delimiters} to {every text item of txt, "\\ "}
	set {txt, text item delimiters} to {txt_items as string, TID}
	return txt
end escape_spaces
 
Top