Applescript to burn audio files.

I have finally finished a script to burn audio files using iTunes. I use “simplesound” plug in from 24u software to create the .AIFF sound file in Filemaker Pro. A reference to it is stored in Filemaker Pro. The script looks for the parameters on a preferences layout in Filemaker Pro and sends feedback to the same Preferences layout. I know it is not the most elegant script, so if anyone has suggestions to improve the script please let me know. Note I run this script from a Perform Applescript step in Filemaker Pro

Kevin

(*
this script is adapted from

“Whack Current Track” for iTunes
written by Doug Adams
dougadams@mac.com

Also from Applescript by Hanaan Rosenthal, and help and suggestions from www.macscripter.net, and www.fmforums.com

Kevin Miller
*)

– Taken from a preferences layout in Filemaker that contains parameters I want to pass to this applescript
tell application “FileMaker Pro”
set this_file to cell “_cMyFolder_AS” & “savedaudio:” & cell “ToBurnSound”
set pList to cell “Filteredalbumname”
set partist to cell “Filteredartistname”
set pyear to cell “YearOfVoicenote” as number
– gwarn is a setting in Filemaker Pro database to know how much feedback the user wants
set gwarn to cell “gwarned” as number
end tell

–Test this_file to see if it exists
tell application “Finder”
– returns true or false
set file_exists to (exists file this_file)
end tell

if (file_exists) then

--test to see if file is an AIFF sound file
tell application "System Events"
	set what_kind_of_file to (kind of (info for file this_file))
end tell

if what_kind_of_file is "AIFF Audio File" then
	
	-- add audio file referenced in Filemaker Pro to iTunes
	set new_track to my add_sound_to_iTunes(pList, pyear, this_file, partist, gwarn)
	
	-- tell iTunes to burn the CD
	set {addenda, user_cancel} to (my Burn_the_CD(pList))
	
	-- notify user of status of Burn CD routine
	activate me
	if gwarn = 1 then
		display dialog addenda buttons {"I understand"} default button 1 with icon 1 giving up after 10
	else
		display dialog addenda buttons {"I understand"} default button 1 with icon 1
	end if
	
	--Pass result of Burn_the_CD routine to Filemaker pro so it be used later in a Filemaker script
	tell application "FileMaker Pro"
		set cell "ToBurnSound" to user_cancel
	end tell
	
	--delete iTunes track that we added from Filemaker Pro
	set addenda to my remove_track_from_iTunes(new_track, pList)
	
	--notify user of status of iTunes delete routine
	activate me
	if gwarn = 1 and addenda does not contain "track could not be deleted" then
		display dialog addenda buttons {"I understand"} default button 1 with icon 1 giving up after 10
	else
		display dialog addenda buttons {"I understand"} default button 1 with icon 1
	end if
else
	set addenda to "The sound file is not the correct type to burn. Contact tech support or record a new voice note and try to burn a CD again."
	set user_cancel to "wrong file type"
	my alert_user_and_cancel(addenda, user_cancel, gwarn)
end if

else
set addenda to “The sound file is corrupted or could not be found on your computer. Contact tech support or record a new voice note and try to burn a CD again.”
set user_cancel to “no file found”
my alert_user_and_cancel(addenda, user_cancel, gwarn)

end if

on add_sound_to_iTunes(pList, pyear, this_file, partist, gwarn)
tell application “iTunes”
activate
make new playlist with properties {name:pList}
set new_track to (add this_file as alias)
–allow time to import track or you might get a file permissions error
delay 2
set dbid to database ID of new_track
set this_track to some track of library playlist 1 whose database ID is dbid
duplicate this_track to playlist pList
set artist of new_track to partist
set album of new_track to pList
set genre of new_track to “Meditation”
set year of new_track to pyear
return new_track

end tell

end add_sound_to_iTunes

on Burn_the_CD(pList)

-- still not good way to determine if the user has quit iTunes manually
activate me
display dialog "Do not use this computer while iTunes is Burning the CD." & return & return & " Follow the instructions in the iTunes window to insert a Blank CD. Do not quit iTunes; this program will do that for you." buttons {"I understand"} default button 1 with icon 1 giving up after 10
tell application "iTunes"
	activate
	try
		set view of front browser window to user playlist pList
		tell application "System Events"
			tell process "iTunes"
				click the menu item "Burn Playlist to Disc" of the menu "File" of menu bar 1
			end tell
		end tell
		-- set m to be a counter to give up after 10 minutes
		set m to 0
		repeat until m = 600
			if (kind of container of view of front browser window is audio CD) then
				delay 2
				tell application "System Events"
					tell process "iTunes"
						click the menu item "Eject Disc" of the menu "Controls" of menu bar 1
					end tell
				end tell
				set addenda to "Disc burn successful. I will now delete the track from iTunes and return to your database. The voice note in your database has not been touched."
				set user_cancel to "CD Burn successful"
				return {addenda, user_cancel}
				exit repeat
			else
				delay 1
				set m to (m + 1)
			end if
		end repeat
		if m = 600 then
			set addenda to "Your CD was not burned because iTunes stopped responding. I will now delete the track from iTunes and return your database. The voice note in your database has not been touched."
			set user_cancel to "iTunes timed out"
			return {addenda, user_cancel}
		end if
	on error number the_error
		set addenda to "Your CD was not burned because of an unexpected error. I will now delete the track from iTunes and return to your database. The voice note in your database has not been touched."
		set user_cancel to "CD Burn error"
		return {addenda, user_cancel}
	end try
end tell

end Burn_the_CD

on remove_track_from_iTunes(new_track, pList)

tell application "iTunes"
	activate
	
	set track_name to name of new_track
	set ofi to fixed indexing
	set fixed indexing to true
	
	try
		set dbid to database ID of new_track
		set cla to class of new_track
		
		set floc to (get location of new_track)
		
		delete (some track of library playlist 1 whose database ID is dbid)
		
		set addenda to "Done. The track has been deleted from iTunes."
		delete user playlist pList
		if cla is file track then
			set addenda to "Done. The track has been deleted from iTunes and its file has been moved to the Trash."
			set file_deleted to my delete_the_file(floc)
			if file_deleted = "error" then
				set addenda to "Done. However, the file in iTunes could not be moved to the Trash."
			end if
		end if
	on error
		set addenda to "The track could not be deleted from iTunes. Please open iTunes manually and delete the Playlist " & pList & ". Also delete from the library track " & track_name & ". Please tell tech support this happened."
		set fixed indexing to ofi
		return addenda
	end try
	set fixed indexing to ofi
	quit
	return addenda
end tell

end remove_track_from_iTunes

to delete_the_file(floc)
try
– tell application “Finder” to delete floc
do shell script "mv " & quoted form of POSIX path of (floc as string) & " " & quoted form of POSIX path of (path to trash as string)
return “OK”
on error
return “error”
end try
end delete_the_file

to alert_user_and_cancel(message, user_cancel, gwarn)
if gwarn = 1 then
display dialog message buttons {“I understand”} default button 1 with icon 0 giving up after 20
else
display dialog message buttons {“I understand”} default button 1 with icon 0
end if
tell application “FileMaker Pro”
activate
set cell “ToBurnSound” to user_cancel

end tell

end alert_user_and_cancel

Model: MacBook
Browser: Safari 419.3
Operating System: Mac OS X (10.4)