Error -1708 calling handler, but only if inside `tell`

I have a script that does some processing of Mail message attachments, and I wrote a little handler to increment a counter to later display in a dialog how many attachments it processed. The script was working fine before I added this display, but as soon as I added the handler to increment the counter, it started failing – or at least not incrementing the counter.

I tried to reduce it to the smallest expression of that code that would exhibit the error, and I’m kind of stumped at the result. (2nd script below)

Here is the complete Mail script, as is, that still processes the attachments, but every single time, pdfCount appears to stay at 0 and it displays “No PDFs found.”

-- Process attachments of selected messages to print 4x6" shipping labels
-- Created by Mark Boszko on 2025-07-20
-- Copyright (c) 2025 Mark Boszko. All rights reserved.


-- PDF Counter
global pdfCount
set pdfCount to 0

tell application "Mail"
	try
		set theSelection to the selection
		set theFolder to (POSIX path of (path to temporary items))
		if the length of theSelection is less than 1 then error "One or more messages must be selected."
		repeat with theMessage in theSelection
			repeat with theAttachment in mail attachments of theMessage
				try
					if downloaded of theAttachment then
						set theFile to theFolder & (name of theAttachment)
						tell theAttachment to save in theFile
						-- Check to make sure it's a PDF
						tell application "System Events" to set {fType, nExt} to ({file type, name extension} of file theFile)
						if (fType is "PDF ") or (nExt is "pdf") then
							set thePath to quoted form of (POSIX path of theFile)
							-- shell script tries to crop and automatically print if result is roughly 4x6"
							-- else opens the cropped PDF result in Preview
							do shell script "~/Applications/crop_pdf_and_print_label.sh " & thePath
							pdfadd()
						end if
					end if
				end try
			end repeat
		end repeat
	on error error_message number error_number
		if error_number is not -128 then display alert "Mail" message error_message as warning
	end try
	if pdfCount is equal to 0 then
		set theDisplayText to "No PDFs found."
	else
		set theDisplayText to (pdfCount as string) & " PDF(s) processed."
	end if
	-- Just in case Preview was opened, let's re-activate Mail
	activate
	display dialog theDisplayText giving up after 5
end tell


on pdfadd()
	global pdfCount
	set pdfCount to pdfCount + 1
end pdfadd

Here is the most concise version that I could come up with that exhibits the error:

global pdfCount
set pdfCount to 0

tell application "Mail"
	repeat 3 times
		pdfadd()
	end repeat
	
	if pdfCount is equal to 0 then
		set theDisplayText to "No PDFs found."
	else
		set theDisplayText to (pdfCount as string) & " PDF(s) processed."
	end if
	
	display dialog theDisplayText giving up after 5
end tell

on pdfadd()
	global pdfCount
	set pdfCount to pdfCount + 1
end pdfadd

In this concise version, if I remove the tell application "Mail" and the corresponding end tell, this code works perfectly fine, and increments the counter, but with the tell, I get this error:

error "Mail got an error: Can’t continue pdfadd." number -1708

In the full script, I am in the middle of looping though the attachments in Mail, when I call pdfadd(). so it feels like I can’t break out of the tell there, to avoid this error.

I’m rather confused at what I’m doing wrong here, and why incrementing a variable seems to throw a wrench into the works?

This is a very common error.
Applications don’t know about handlers. Tou have to tell AppleScript to perform the handler by adding the my keyword

tell application "Mail"
	repeat 3 times
		my pdfadd()
	end repeat
    -- …
1 Like

Aha! Thank you. I didn’t realize that was the case. Works now, as you say, with my.

Interestingly, with the full original script, it seems if the return value of the shell script is non-zero, it still fails to increment the counter. But if the shell script returns 0, it does increment as planned. Is a non-zero return triggering the try block to fail, and skip the pdfadd() command? I suppose that would make sense. I may have to adjust to account for that.

Yes, it is.
If it’s crucial to increment the counter catch the error in on error like in the outer try block.

1 Like

Thanks! I changed the exit code of the shell script to 3 if it’s a size error for the cropped PDF, and left the code as 1 if there was a more general file error, so I could display a custom dialog for the size warning.

I modified that try block inside the loop to handle either one (excerpted below), and the pdfadd() handler now also works at all times, as expected.

				try
					if downloaded of theAttachment then
						set theFile to theFolder & (name of theAttachment)
						tell theAttachment to save in theFile
						-- Check to make sure it's a PDF
						tell application "System Events" to set {fType, nExt} to ({file type, name extension} of file theFile)
						if (fType is "PDF ") or (nExt is "pdf") then
							my pdfadd()
							-- shell script that crops the label and prints
							set thePath to quoted form of (POSIX path of theFile)
							-- tries to crop and automatically print if result is roughly 4x6"
							-- else opens the cropped PDF result in Preview
							do shell script "~/Applications/crop_pdf_and_print_label.sh " & thePath
						end if
					end if
				on error error_message number error_number
					if error_number is equal to 3 then
						-- this is a size error; the label is not 4x6"
						-- the cropped PDF will open automatically in Preview for manual review
						activate
						display alert "PDF not 4×6\"" message error_message as warning giving up after 10
						
					else if error_number is not -128 then
						-- error -128 is User Canceled, which we can ignore
						-- this is likely an error 1, which is a file problem
						activate
						display alert "Error: Exit Code " & (error_number as string) message error_message as warning
					end if
				end try

Thanks again for the help!