Applescript - email file as attachment problem

Get help. Get answers. Let others lend you a hand.

Moderator: Mr_Noodle

Hi,
I'm trying to grab files with a certain extension from a folder once Carbon Copy Cloner has copied them from a drive and ten email them one at a time as attachments.
My rules for this section are:
Date added is today
Extension is XXX
For testing purposes I then get Hazel to email me that it worked and has completed.

The confirmation email worked however the email with file attachment failed.
Here's the relevant log section:
2016-06-07 20:02:56.142 hazelworker[959] [Error] AppleScript failed: Error executing AppleScript on file /Users/MacMini/Dropbox/Apps/Bryton/160603053919.fit.
2016-06-07 20:02:56.251 hazelworker[959] OSAScript error: {
NSLocalizedDescription = "Mail got an error: User canceled.";
NSLocalizedFailureReason = "User canceled.";
OSAScriptErrorAppAddressKey = "<NSAppleEventDescriptor: [0x0,40040 \"Mail\"]>";
OSAScriptErrorAppNameKey = Mail;
OSAScriptErrorBriefMessageKey = "User canceled.";
OSAScriptErrorMessageKey = "Mail got an error: User canceled.";
OSAScriptErrorNumberKey = "-128";
OSAScriptErrorRangeKey = "NSRange: {0, 0}";
}

I had no interaction with it so I'm not sure why it keeps saying 'User canceled'
Here's the applescript I used;
Code: Select all
set theAttachment1 to (POSIX path of theFile)
set subject_ to "SUBJECT"
set the_content to "CONTENT"
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 "xxxxxxxxx@xxxxxx.com"
      make new to recipient at end of to recipients with properties {address:"XXXXXXX@XXXXX.XXX"}
      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


Mr Seagull
MrSeagull
 
Posts: 36
Joined: Tue Jun 07, 2016 8:26 am

You might want to try using Automator instead of AppleScript to see if it works any better. Otherwise, have you tried running the script outside of Hazel?
Mr_Noodle
Site Admin
 
Posts: 11255
Joined: Sun Sep 03, 2006 1:30 am
Location: New York City

Hi,
Well yes I have tried using Automator. It was Automator's failings that led me to spend $45 on your product.
Automator's Folder Actions don't even seem to reliably trigger these days. Even for a very simple copy action.
Second, Automators ability to distinguish new files, or compare a source & target, doesn't exist. Automator can't reliably filter finder items until they've been Spotlight indexed, causing errors.

I'll try my script (actually copied from a post in these forums) outside Hazel and see.

Mr Seagull
MrSeagull
 
Posts: 36
Joined: Tue Jun 07, 2016 8:26 am

OK, so it just worked on a test file in a test folder, so I'll go back & triple check the original set up.
MrSeagull
 
Posts: 36
Joined: Tue Jun 07, 2016 8:26 am

OK, I think this is working and I think it may have been unacceptable curly braces from the pasted-in Applescript.
However, I started testing it on a different computer, and as the target folder resides on Dropbox the Date Added rule matched every file in the folder and spat out many many emails.

What is the best way to detect files that have been added since the rule last triggered? Is it:
Date Last Modified is after Date Last Matched?

Mr Seagull
MrSeagull
 
Posts: 36
Joined: Tue Jun 07, 2016 8:26 am

For files added, Date added is after date last matched is what you want.
Mr_Noodle
Site Admin
 
Posts: 11255
Joined: Sun Sep 03, 2006 1:30 am
Location: New York City

Thanks Mr Noodle

Mr Seagull
MrSeagull
 
Posts: 36
Joined: Tue Jun 07, 2016 8:26 am


Return to Support