Difference between revisions of "AppleScript: Export Selected Tracks….applescript"

From Wurst-Wasser.net
Jump to: navigation, search
Line 1: Line 1:
 
This is the Script belonging to [[Catalina deleted all my Audiobooks!]]:
 
This is the Script belonging to [[Catalina deleted all my Audiobooks!]]:
 +
 +
global errorMessage
 +
 +
(* Init *)
 +
set errorMessage to ""
 +
 +
(* 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
 +
end timeout
 +
end if
 +
end tell
 +
 +
end tell
 +
end repeat
 +
end if
 +
 +
if length of errorMessage is greater than 0 then
 +
set errorMessage to "The following errors occurred:" & return & return & errorMessage
 +
display dialog errorMessage buttons {"Oh my!"}
 +
end if
 +
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
 +
 +
 +
  
  

Revision as of 21:28, 11 February 2020

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

global errorMessage

(* Init *)
set errorMessage to ""

(* 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
						end timeout
					end if
				end tell
				
			end tell
		end repeat
	end if
	
	if length of errorMessage is greater than 0 then
		set errorMessage to "The following errors occurred:" & return & return & errorMessage
		display dialog errorMessage buttons {"Oh my!"}
	end if
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