Attaching File To An Email Using AppleScript

Talk, speculate, discuss, pontificate. As long as it pertains to Hazel.

Moderators: Mr_Noodle, Moderators

Hi all,

I'm trying to add the finishing touches to a Hazel rule that will create a new email, add the recipients details, populate it with predetermined text, attach a file, and send it. I've got everything nailed except for the file attachment. I've written my first AppleScript, and it works really well, except for that last little bit! :? I can attach a specific file in a specific folder, as in the example below, but ideally I'd like to be able to configure the rule so that the file attached to the email is the file that triggered the Hazel rule.

(To give you some context, I'm using the rule to send timesheets of my team to management, so the name of the file changes every month - eg. 'Marty McFly - Timesheet - 04112013' this week, but 'Marty McFly - Timesheet - 11112013' next week.)

Here's what I've got so far:

Code: Select all
tell application "Mail" to activate

set recipientName to "Boss"
set recipientAddress to "boss@domain.com”
set theSubject to “Email Subject”
set theContent to "Hi there,

Please see attached file.

Cheers,

Chris

"
set theAttachment to "Macintosh HD:Users:WR:Downloads:Apple.eps"

tell application "Mail"
   set theMessage to make new outgoing message with properties {subject:theSubject, content:theContent, visible:true}
   tell theMessage
      make new to recipient with properties {name:recipientName, address:recipientAddress}
      make new attachment with properties {file name:theAttachment} at after the last character
   end tell
end tell


Is there an easy way to attach the file that triggered the rule to an email, either using Hazel or AppleScript?

Thanks everyone.

Chris
Electric Monk
 
Posts: 1
Joined: Sun Nov 03, 2013 4:09 pm

Please do not post questions in the Tips forum (see the sticky article up top there). I've moved it to a more appropriate forum.

As for your script, you need to use "theFile" (no quotes) as the file instead of the hardcoded one you have.
Mr_Noodle
Site Admin
 
Posts: 11193
Joined: Sun Sep 03, 2006 1:30 am
Location: New York City

The attached script doesnt work for me. Give me "EXPECTED END OF LINE" errors everywhere. Im sure there's something off here. Suggestions?

Code: Select all
tell application "Mail" to activate

set recipientName to "Boss"
set recipientAddress to "boss@domain.com”
set theSubject to “Email Subject”
set theContent to "Hi there,

Please see attached file.

Cheers,

Chris

"
set theAttachment to "Macintosh HD:Users:WR:Downloads:Apple.eps"

tell application "Mail"
   set theMessage to make new outgoing message with properties {subject:theSubject, content:theContent, visible:true}
   tell theMessage
      make new to recipient with properties {name:recipientName, address:recipientAddress}
      make new attachment with properties {file name:theAttachment} at after the last character
   end tell
end tell
pieman100
 
Posts: 1
Joined: Mon Apr 07, 2014 5:18 pm

Not sure but you shouldn't be using this script anyways without at least fixing what I noted earlier.
Mr_Noodle
Site Admin
 
Posts: 11193
Joined: Sun Sep 03, 2006 1:30 am
Location: New York City


Return to Open Discussion