Page 1 of 1

Script No Longer Attaching File

PostPosted: Tue Dec 29, 2015 11:36 am
by Zyniker
I am currently using a script to attach a file in a monitored folder to an email and then send that email (or at least compose it and leave it open). While attempting to update the script to pull the addressee and email address from the file name, it would appear that I somehow broke the script and it is no longer attaching the file as it once did. I cannot seem to figure out where in the script the error lies. Here is the script I am using:

Code: Select all
on hazelProcessFile(theFile)
   set theAttachment1 to (POSIX path of theFile)
   
   -- remove path from FileName
   set text item delimiters to ":"
   set FileName to last text item of (theFile as text)
   
   -- remove the extension (last item when separated by ".")
   set AppleScript's text item delimiters to "."
   set SansExtension to every text item of FileName
   set last text item of SansExtension to ""
   set FileName to SansExtension as text
   
   -- parse the name with the "—"
   set AppleScript's text item delimiters to "—"
   set clientName to first text item of FileName
   set clientEmail to last text item of FileName
   
   tell application "Airmail 2"
      activate
      set theMessage to make new outgoing message with properties {subject:"New Invoice", content:"Please find attached, infra, the current month's invoice."}
      tell theMessage
         set sender to "billing@example.com"
         make new to recipient at end of to recipients with properties {name:clientName, address:clientEmail}
         make new mail attachment with properties {filename:theAttachment1}
         compose
      end tell
   end tell
end hazelProcessFile

Re: Script No Longer Attaching File

PostPosted: Tue Dec 29, 2015 12:57 pm
by Mr_Noodle
Search the forums as this has already come up. In short, El Capitan is bugged and you need to add a delay to work around it.

Re: Script No Longer Attaching File

PostPosted: Tue Dec 29, 2015 1:45 pm
by Zyniker
I've already attempted the delay method. I added a significant (i.e., 10s) delay into the script before sendmessage/compose, and the script still fails to attach the file.

Re: Script No Longer Attaching File

PostPosted: Tue Dec 29, 2015 4:05 pm
by Zyniker
I am now working with an updated script:

Code: Select all
on hazelProcessFile(theFile)
   
   set theAttachment1 to (POSIX path of theFile)
   tell application "System Events" to set {name:Nm, name extension:Ex} to theFile
   set FileName to text 1 thru ((get offset of "." & Ex in Nm) - 1) of Nm
   
   -- parse using —
   set {TID, text item delimiters} to {text item delimiters, "—"}
   set clientName to text item -3 of FileName
   set clientEmail to text item -1 of FileName
   set text item delimiters to TID
   
   tell application "Airmail 2"
      activate
      set theMessage to make new outgoing message with properties {subject:"New Invoice from Company", content:"Please find attached, infra, the current month's invoice.    If you have any questions, please feel free to respond to this email.  One-time payments may be made using the following secure form on our website: https://example.com/payment/.  Thank you for your continued business."}
      tell theMessage
         set sender to "billing@example.com"
         delay 1
         make new mail attachment with properties {filename:theAttachment1}
         delay 1
         make new to recipient at end of to recipients with properties {name:clientName, address:clientEmail}
         delay 1
         compose
      end tell
   end tell
end hazelProcessFile


This works the same as the other code, and has the same problem (i.e., it fails to attach the file). I have tried delays of up to a minute and placed them nearly everywhere in the script (and at various points in the Hazel routine, too). Nothing, thus far, seems to have resulted in a working script. The Console provides nothing of any real use, but here's the output nonetheless:

Code: Select all
2015-12-29 11:50:48.293 hazelworker[1817] Processing folder Invoice Staging
2015-12-29 11:50:50.449 hazelworker[1817] Client—2015-12-29-2015-INV-1000393.pdf: Rule To Be Emailed matched.
2015-12-29 11:50:50.449 hazelworker[1817] [File Event] File moved: Client—2015-12-29-2015-INV-1000393.pdf moved from folder /Volumes/Folder/Invoices/Invoice Staging to folder /Volumes/Folder/Invoices/Invoice Staging/To Be Emailed.
2015-12-29 11:50:51.520 hazelworker[1818] ###main load address: 0x10a9d7000
2015-12-29 11:50:51.521 hazelworker[1818] ###Noodle load address: 0x10aae7000
2015-12-29 11:50:51.521 hazelworker[1818] ###CK load address: 0x10aaab000
2015-12-29 11:50:51.538 hazelworker[1818] Processing folder To Be Emailed
2015-12-29 11:50:52.649 hazelworker[1817] Done processing folder Invoice Staging
2015-12-29 11:50:53.682 hazelworker[1818] Client—2015-12-29-2015-INV-1000393.pdf: Rule Sample (Client) matched.
2015-12-29 11:50:53.682 hazelworker[1818] [File Event] File renamed: /Volumes/Folder/Invoices/Invoice Staging/To Be Emailed/Client—2015-12-29-2015-INV-1000393.pdf renamed to /Volumes/Folder/Invoices/Invoice Staging/To Be Emailed/Client—2015-12-29-2015-INV-1000393—client@example.com.pdf.
2015-12-29 11:50:53.714 hazelworker[1819] ###main load address: 0x10f503000
2015-12-29 11:50:53.715 hazelworker[1819] ###Noodle load address: 0x10f618000
2015-12-29 11:50:53.715 hazelworker[1819] ###CK load address: 0x10f5da000
2015-12-29 11:50:53.747 hazelworker[1819] Processing folder Invoice Staging
2015-12-29 11:50:54.110 hazelworker[1818] [File Event] File moved: Client—2015-12-29-2015-INV-1000393—client@example.com.pdf moved from folder /Volumes/Folder/Invoices/Invoice Staging/To Be Emailed to folder /Volumes/Folder/Invoices/Invoice Staging/To Be Emailed/Sent.
2015-12-29 11:50:55.154 hazelworker[1821] ###main load address: 0x100f82000
2015-12-29 11:50:55.155 hazelworker[1821] ###Noodle load address: 0x101096000
2015-12-29 11:50:55.155 hazelworker[1821] ###CK load address: 0x101058000
2015-12-29 11:50:55.175 hazelworker[1821] Processing folder Sent
2015-12-29 11:50:55.847 hazelworker[1819] File To Be Emailed is busy. Skipping for now.
2015-12-29 11:50:57.276 hazelworker[1821] Client—2015-12-29-2015-INV-1000393—client@example.com.pdf: Rule Sort into Subfolders by Month matched.
2015-12-29 11:50:57.277 hazelworker[1821] [File Event] File moved into subfolder: /Volumes/Folder/Invoices/Invoice Staging/To Be Emailed/Sent/2015-12(Dec)/Client—2015-12-29-2015-INV-1000393—client@example.com.pdf sorted from folder /Volumes/Folder/Invoices/Invoice Staging/To Be Emailed/Sent to subfolder 2015-12(Dec).

Re: Script No Longer Attaching File

PostPosted: Tue Dec 29, 2015 7:09 pm
by Mr_Noodle
Unless you can demonstrate this script working fine outside of Hazel, but only failing within Hazel, there's not much I can do. You'll have to file a bug with Apple as this seems to be a problem with Mail's AppleScript on El Cap. You might want to also investigate alternatives. Maybe Mail's Automator actions don't have this problem. Or you can try some other mail delivery service instead of relying on Mail.app.

Re: Script No Longer Attaching File

PostPosted: Tue Dec 29, 2015 11:16 pm
by Zyniker
I've actually tried this script (obviously modified) in both Mail and Airmail (the script pasted in this thread is the Airmail version).

Re: Script No Longer Attaching File

PostPosted: Mon Jul 17, 2017 3:15 pm
by VladimirJirasek
Mr_Noodle wrote:Unless you can demonstrate this script working fine outside of Hazel, but only failing within Hazel, there's not much I can do. You'll have to file a bug with Apple as this seems to be a problem with Mail's AppleScript on El Cap. You might want to also investigate alternatives. Maybe Mail's Automator actions don't have this problem. Or you can try some other mail delivery service instead of relying on Mail.app.


Hi, I can confirm that the script does not work even outside Hazel. For example, I tested th below (obviously anonymised email addresses). This looks like Apple script problem.

set theAttachment1 to "/Users/vladimirjirasek/Downloads/file.csv"
set subject_ to "Filed receipt"
set the_content to "This is a receipt filed into the receipt folder"
tell application "Mail"
set newMessage to make new outgoing message with properties {subject:subject_, content:the_content & return & return}
tell newMessage

set visible to false
set sender to "xxx@xxxx.com"
make new to recipient at end of to recipients with properties {address:"yyy@yyyy.eu"}
make new attachment with properties {file name:theAttachment1} at after the last paragraph

(* change save to send to send*)
send --<<<<---------------- change save to send to send or send to save to save to drafts
(* change save to send to send*)
end tell
end tell