No matter what I try, this folder action script will only run twice before ceasing to function.
Using my trusty Brother printer/scanner, I load up the feeder with copies of paper invoices that have been mailed to me. The goal is to scan the invoices, open a copy of the scan so I can read it and input a PO number, then save a local copy with the PO number to reference in the filename, and finally emailing a copy off to the bookkeeper.
The script works fine with 1 or 2 invoices. On 3 it craps out: the Preview window does not close, the email message is generated, but the scan is not attached, and the script stops processing altogether.
To experiment, I’m simply moving files in and out of the “ToSend” folder. The scanner software scans all the loaded documents, and deposits them all at once in that same folder.
Any assistance is truly appreciated.
on adding folder items to this_folder after receiving these_items
set MyDay to day of (current date)
set MyYr to year of (current date)
set MyMo to month of (current date) as integer
set BeckySent to "" & (path to desktop) & "BeckyInvoices:Sent"
set BeckyToSend to "" & (path to desktop) & "BeckyInvoices:ToSend"
set BeckyEmail to "Becky the Bookkeeper <becky@beckysdomain.com>"
repeat with this_item in these_items
try
tell application "Finder"
open file this_item
tell application "System Events"
activate
set MyPONum to the text returned of (display dialog "What's the PO Number for this Invoice?" default answer "XXX") as text
set name of this_item to MyPONum & "_Inv.png" as string
end tell
delay 1
tell application "Preview" to quit
move this_item to folder BeckySent --replacing yes
end tell
tell application "Mail"
-- activate
set new_msg to make new outgoing message with properties {sender:"Big Promotions CS <cs@bigpromotions.net>", subject:MyPONum & "_Inv.png - Sent on " & MyMo & "/" & MyDay & "/" & MyYr, visible:true, signature:none, content:"Invoice for " & MyPONum & " attached
"}
tell new_msg to make new to recipient at end of to recipients with properties {address:BeckyEmail}
tell content of new_msg to make new attachment with properties {file name:this_item} at after last paragraph
close new_msg saving yes
--send new_msg
--delay 5
end tell
beep
on error error_message number error_number
tell application "Finder"
activate
display dialog error_message buttons {"Cancel"} default button 1 giving up after 120
end tell
end try
end repeat
end adding folder items to
Model: MacBook Pro and Mac Mini
Browser: Safari 525.20
Operating System: Mac OS X (10.5)