Paste number 133959: AppleScript iTunes playlist creation

Paste number 133959: AppleScript iTunes playlist creation
Pasted by: PowerUser
When:4 years, 8 months ago
Share:Tweet this! |
Paste contents:
Raw Source | XML | Display As
property allowed_file_types : {"", ""}
property last_playlist_name : ""

on playlist_named(playlist_name)
	local matching_playlists
	tell application "iTunes" to set matching_playlists to (every playlist whose name is playlist_name)
	if length of matching_playlists > 0 then return first item of matching_playlists
	return missing value
end playlist_named

on create_playlist(playlist_name)
	local the_playlist
	set the_playlist to playlist_named(playlist_name)
	if the_playlist = missing value then
		tell application "iTunes" to set the_playlist to make new playlist with properties {name:playlist_name}
	end if
	return the_playlist
end create_playlist

to insert_tracks of file_list into playlist_name
	-- Sanitize the file list
	local start_date, track_list, a_file
	set start_date to current date
	set track_list to {}
	repeat with a_file in file_list
		local a_track
		tell application "iTunes"
			set a_track to add (a_file as alias)
			if (date added of a_track) > start_date then
					local alternate_track
					set alternate_track to (first track in playlist "Library" whose name is (get name of a_track) and album is (get album of a_track) and artist is (get artist of a_track) and kind is (get kind of a_track) and bit rate is (get bit rate of a_track) and disc count is (get disc count of a_track) and disc number is (get disc number of a_track) and track count is (get track count of a_track) and track number is (get track number of a_track) and year is (get year of a_track) and date added < (get date added of a_track))
					set an_alias to get location of alternate_track
					delete a_track
					set a_track to alternate_track
				end try
			end if
		end tell
		copy a_track to the end of track_list
	end repeat
	-- Get the playlist
	local the_playlist
	set the_playlist to create_playlist(playlist_name)
	set last_playlist_name to playlist_name
	-- Do the work
	tell application "iTunes"
		local a_track
		repeat with a_track in track_list
			duplicate a_track to the_playlist
		end repeat
	end tell
end insert_tracks

on prompt_for_playlist_name()
	local playlist_name
	set playlist_name to ""
	repeat while playlist_name = ""
		local dialog_result
		set playlist_name to text returned of (display dialog "Enter a name for the new playlist:" default answer last_playlist_name with title "Name Playlist")
		if playlist_name = "" then
			if playlist_named(playlist_name) is not missing value then
					display dialog "Playlist \"" & playlist_name & "\" already exists. Add the tracks to it?" buttons {"No", "Yes"} default button "Yes" cancel button "No"
				on error
					set playlist_name to ""
				end try
			end if
		end if
	end repeat
	return playlist_name
end prompt_for_playlist_name

on open file_list
	insert_tracks of file_list into prompt_for_playlist_name()
end open

on prompt_for_files()
	return choose file of type allowed_file_types with prompt "Choose files to make into a new playlist" with multiple selections allowed
end prompt_for_files

on run
	open prompt_for_files()
end run

This paste has no annotations.

Colorize as:
Show Line Numbers

Lisppaste pastes can be made by anyone at any time. Imagine a fearsomely comprehensive disclaimer of liability. Now fear, comprehensively.