Page 1 of 1

Attaching File To An Email Using AppleScript

PostPosted: Sun Nov 03, 2013 4:36 pm
by Electric Monk
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

Re: Attaching File To An Email Using AppleScript

PostPosted: Mon Nov 04, 2013 4:23 pm
by Mr_Noodle
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.

Re: Attaching File To An Email Using AppleScript

PostPosted: Mon Apr 07, 2014 7:57 pm
by pieman100
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

Re: Attaching File To An Email Using AppleScript

PostPosted: Tue Apr 08, 2014 12:03 pm
by Mr_Noodle
Not sure but you shouldn't be using this script anyways without at least fixing what I noted earlier.