AppleScript: Export Selected Tracks….applescript

From Wurst-Wasser.net
Jump to navigation Jump to search

This is the Script belonging to Catalina deleted all my Audiobooks!:

global errorMessage
global counter

(* Init *)
set errorMessage to ""
set counter to 0

(* Get folder to save the files in *)
tell application "Finder"
	activate
	try
		set exportFolder to choose folder with prompt "Please choose a folder to save the selected tracks into:"
		
		#log ordner
		
	on error (e)
		log "Error: " & e
		return
	end try
	# Debug
	#	set posixpath to (path to desktop folder as text) & "Export:"
	#	set macospath to folder posixpath
	#	set exportFolder to macospath
end tell

(* Script to export selected tracks with nice folders *)
tell application "iTunes"
	activate
	if selection is not {} then -- there ARE tracks selected...
		
		set sel to a reference to selection
		set trackCount to count sel
		if trackCount is greater than 23 then
			set msg to "Are you sure, you want to export " & trackCount & " tracks?"
			set res to display dialog msg buttons {"Sure!", "Actually, no."}
			if button returned of res is not "Sure!" then
				return
			end if
		end if
		
		(* Export track by track *)
		repeat with aTrack in sel (* is of type "file track" *)
			tell aTrack
				#get class of aTrack
				
				(* Get the relevant information of this track, could be used directly, though *)
				set artistName to artist
				if artistName is "" then set artistName to "Unknown Artist"
				tell me to set artistName to changeString of artistName from ":" to "_" (* Toast the colons *)
				set albumName to album
				if albumName is "" then set albumName to "Unknown Album"
				tell me to set albumName to changeString of albumName from ":" to "_" (* Toast the colons *)
				set isPartOfCompilation to compilation
				if (isPartOfCompilation is true) then set artistName to "Compilations"
				set fileReference to location
				set filePath to (fileReference as text)
				log fileReference
				#					get size of file fileReference
				log filePath
				#				end tell
				
				(* Check whether the file exists *)
				tell application "Finder" (* If the file for this track does not exist, do some mecker *)
					if (not (exists file filePath)) then
						set errorMessage to errorMessage & "File is missing: " & artistName & " - " & albumName & " (Path: " & filePath & ")" & return
					else (* Create the necessary folders *)
						(* First, the artist folder *)
						set artistFolder to (exportFolder as string) & artistName & ":"
						log "artistFolder: " & artistFolder
						if (not (exists folder artistFolder)) then
							make new folder at exportFolder with properties {name:artistName}
						end if
						
						(* Second, the album folder *)
						set albumFolder to (artistFolder as string) & albumName & ":"
						log "albumFolder: " & albumFolder
						if (not (exists folder albumFolder)) then
							make new folder at (folder artistFolder) with properties {name:albumName}
						end if
						
						(* Copy the actual track file *)
						with timeout of 240 seconds (* some Audiobooks are quite large! *)
							log "Copying from " & filePath & " -> " & albumFolder
							tell application "Finder" to duplicate file filePath to albumFolder without replacing
							set counter to counter + 1
						end timeout
					end if
				end tell
				
			end tell
		end repeat
	end if
	
	with timeout of 0 seconds
	 	if length of errorMessage is greater than 0 then
		 	set errorMessage to "The following errors occurred:" & return & return & errorMessage
		 	display dialog "Copied " & counter & " of " & trackCount & " files." & return & return & "But:" & return & errorMessage buttons {"Oh my!"}
	 	else
		 	display dialog "Copied " & counter & " of " & trackCount & " files." buttons {"Really?"}
	 	end if
	end timeout
end tell



to changeString of t from s to r
	set d to text item delimiters
	set text item delimiters to s
	set t to t's text items
	set text item delimiters to r
	tell t to set t to beginning & ({""} & rest)
	set text item delimiters to d
	t
end changeString





ToDos:

  • Add on error on every critical action.
  • Report details about copied files.