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?