Where do I put try on error block?

Hello,
Semi-newbie here. I am scripting alot more, but perhaps not better, or cleaner. Anyway, I’ve written the following script in order to concatenate a particular set of pdfs into complete documents. In order for the script to work, two docs have to exist on the desktop, so I put these into properties.


property FileTypeList : {"PDF "}
property CORE_Pdf : (path to desktop as string) & "AMC_Exported_LoRes_PDFs:AMC-005_CORE.pdf"
property CORE_Span_PDF : (path to desktop as string) & "AMC_Exported_LoRes_PDFs:AMC-005_CORE_SPAN.pdf"
property myCompletedFolder : (path to desktop as string) & "Completed_AMC_PDFs:"

set theseItems to (choose file)
set theseItems to theseItems as list
--on open theseItems WILL TURN THIS INTO A DROPLET LATER
tell application "Finder"
	set CORE_is_there to CORE_Pdf as alias
	set CORE_Span_is_there to CORE_Span_PDF as alias
	if not (exists CORE_is_there or CORE_Span_is_there) then
		display dialog "You need to have made a PDF of the English CORE document AND the Spanish CORE document. If you have used the droplet called \"Americhoice_Lo_Res_PDFs.app\", they should be in a folder called \"AMC_Exported_LoRes_PDFs\" on your desktop. If not there, create a folder called \"AMC_Exported_LoRes_PDFs\" on your desktop and place BOTH CORE pdfs in it. This application will now quit." buttons {"OK"}
		
	else
				repeat with thisitem in theseItems
			--tell application "Finder"
			if not (exists folder myCompletedFolder) then
				make new folder at desktop with properties {name:"Completed_AMC_PDFs"}
			end if
			set thisItemInfo to info for thisitem
			
			try
				set thisFiletype to the file type of thisItemInfo
			on error
				set thisFiletype to ""
			end try
			
			if thisFiletype is not in the FileTypeList then
				display dialog "You must drop an AMC pdf on me in order to continue."
			else
				my processItems(thisitem)
			end if
		end repeat
	end if
end tell
--end open

on processItems(thisitem)
	tell application "Finder"
		set originalName to name of thisitem
		set newName to characters 1 thru -4 of originalName & "_combined.pdf"
		set final_file to myCompletedFolder & newName
		
	end tell
	tell application "Adobe Acrobat 7.0 Professional"
		activate
		close documents
		open thisitem
		open CORE_Pdf
		open CORE_Span_PDF
		set masterDoc to document 1
		set file2Insert to document 2
		set spanfile2Insert to document 3
		insert pages masterDoc after 1 from file2Insert starting with 1 number of pages 2
		insert pages masterDoc after 5 from spanfile2Insert starting with 2 number of pages 1
		insert pages masterDoc after 6 from spanfile2Insert starting with 1 number of pages 1
	end tell
	tell application "Adobe Acrobat 7.0 Professional"
		save document 1 to file final_file
		delay 2
		close documents without saving
	end tell
	
end processItems

I am testing this script by not having the CORE documents in the right folder. But instead displaying the dialog box, the script returns an error: “File mac042:Users:andrewlenner:Desktop:AMC_Exported_LoRes_PDFs:AMC-005_CORE.pdf wasn’t found.” I guess that the script is looking for that file (since it is a property) before parsing the “if not exists” line. So I think I should capture the error, so that I can display the dialog box to the user, and not the error message.

So where in the script would I put the try statement, if possible?

Thanks for any advice.

Paeon

When coercing to an alias, an error will be raised if the file doesn’t exist.

Thanks for responding. What I was trying to do was to “break” the app by not having the CORE_pdfs files where they should be. So when the app runs, and the file isn’t ther,e the script could capture the error and run a dialog to tell the user (not me) to go get the file and put it where it belongs.

But I don’t know when the error is occurring. Does AS look for the needed before processing commands? If so, how can I capture the error message? Where should I put the try statement, if a try statement is needed?

P.S. I’m trying to convince my bosses (who hate technology) that Applescript is a good thing, so they will buy me Script Debugger and let me work on scripts during work hours. I’ve got one “VP” on board but I need to convince several more. This script (above) would be really meaningful, if I can get it to work right.

So thanks again. :slight_smile:

Hi Paeon,

Maybe you could try code like follows which creates the paths dynamically during an initialization function? I do not have Adobe Acrobat installed, so maybe the code is messed up :smiley: But as Steve always says: You get the idea :wink:



property FileTypeList : {"PDF "}
property CORE_Pdf : missing value
property CORE_Span_PDF : missing value
property myCompletedFolder : missing value

if not my initscript() then
	tell me
	activate
	display dialog "You need to have made a PDF of the English CORE document AND the Spanish CORE document. If you have used the droplet called \"Americhoice_Lo_Res_PDFs.app\", they should be in a folder called \"AMC_Exported_LoRes_PDFs\" on your desktop. If not there, create a folder called \"AMC_Exported_LoRes_PDFs\" on your desktop and place BOTH CORE pdfs in it. This application will now quit." buttons {"OK"}
	end tell
end if 

set theseItems to (choose file)
set theseItems to theseItems as list
--on open theseItems WILL TURN THIS INTO A DROPLET LATER
tell application "Finder"
	set CORE_is_there to CORE_Pdf as alias
	set CORE_Span_is_there to CORE_Span_PDF as alias
	
	repeat with thisitem in theseItems
		--tell application "Finder"
			if not (exists folder myCompletedFolder) then
				make new folder at desktop with properties {name:"Completed_AMC_PDFs"}
			end if
			set thisItemInfo to info for thisitem
			
			try
				set thisFiletype to the file type of thisItemInfo
			on error
				set thisFiletype to ""
			end try
			
			if thisFiletype is not in the FileTypeList then
				display dialog "You must drop an AMC pdf on me in order to continue."
			else
				my processItems(thisitem)
			end if
	end repeat

end tell
--end open

on processItems(thisitem)
	tell application "Finder"
		set originalName to name of thisitem
		set newName to characters 1 thru -4 of originalName & "_combined.pdf"
		set final_file to myCompletedFolder & newName
		
	end tell
	tell application "Adobe Acrobat 7.0 Professional"
		activate
		close documents
		open thisitem
		open CORE_Pdf
		open CORE_Span_PDF
		set masterDoc to document 1
		set file2Insert to document 2
		set spanfile2Insert to document 3
		insert pages masterDoc after 1 from file2Insert starting with 1 number of pages 2
		insert pages masterDoc after 5 from spanfile2Insert starting with 2 number of pages 1
		insert pages masterDoc after 6 from spanfile2Insert starting with 1 number of pages 1
	end tell
	tell application "Adobe Acrobat 7.0 Professional"
		save document 1 to file final_file
		delay 2
		close documents without saving
	end tell
	
end processItems

on initscript()
set CORE_Pdf to (path to desktop as string) & "AMC_Exported_LoRes_PDFs:AMC-005_CORE.pdf"
set CORE_Span_PDF to (path to desktop as string) & "AMC_Exported_LoRes_PDFs:AMC-005_CORE_SPAN.pdf"
set myCompletedFolder to (path to desktop as string) & "Completed_AMC_PDFs:"

repeat with itempath in {CORE_Pdf, CORE_Span_PDF, myCompletedFolder}
	try
		set itemalias to itempath as alias
	on error
		return false
	end try
end repeat
return true
end

It’s occurring at the line that I quoted.

Thanks to Bruce and Martin!
I put the try statement around the line you quoted, then put “return” to make the script stop running, and it works. However, I am going to try Martin’s suggestion, cause I think a handler will be better in the long run.

Once again, thanks.

Paeon

Try something like this:

on run
	choose file with multiple selections allowed without invisibles
	open result
end run

on open theseItems
	try
		set corePDF to ((path to desktop as Unicode text) & "AMC_Exported_LoRes_PDFs:AMC-005_CORE.pdf") as alias
		set coreSpanPDF to ((path to desktop as Unicode text) & "AMC_Exported_LoRes_PDFs:AMC-005_CORE_SPAN.pdf") as alias
	on error
		display dialog "You need to have made a PDF of the English CORE document AND the Spanish CORE document. If you have used the droplet called \"Americhoice_Lo_Res_PDFs.app\", they should be in a folder called \"AMC_Exported_LoRes_PDFs\" on your desktop. If not there, create a folder called \"AMC_Exported_LoRes_PDFs\" on your desktop and place BOTH CORE pdfs in it. This application will now quit." with icon note buttons {"Cancel"} default button 1
	end try
	
	set completedFolder to (path to desktop as Unicode text) & "Completed_AMC_PDFs:"
	
	tell application "System Events"
		if not (exists folder completedFolder) then
			make new folder at desktop with properties {name:"Completed_AMC_PDFs"}
		end if
	end tell
	
	repeat with thisItem in theseItems
		try
			tell application "System Events"
				set finalFile to completedFolder & text 1 thru 4 of name of thisItem & "_combined.pdf"
				name extension of thisItem
			end tell
			
			if result is not in {"pdf"} then
				error """ & thisItem & "" is not an AMC PDF file." number 1
			end if
			
			tell application "Adobe Acrobat 7.0 Professional"
				activate
				close documents
				
				open thisItem
				open CORE_Pdf
				open CORE_Span_PDF
				
				set masterDoc to document 1
				set file2Insert to document 2
				set spanfile2Insert to document 3

				insert pages masterDoc after 1 from file2Insert starting with 1 number of pages 2
				insert pages masterDoc after 5 from spanfile2Insert starting with 2 number of pages 1
				insert pages masterDoc after 6 from spanfile2Insert starting with 1 number of pages 1
				
				save document 1 to file finalFile
				delay 2
				close documents without saving
			end tell
		on error errMsg number errNum
			display alert "Error " & errNum message errMsg buttons {"Cancel Script", "Skip Item"} default button 2 cancel button 1
		end try
	end repeat
end open