Applescript send Mail with attachment from Folder Action

Hi all,

I have the below script someone on here cleaned for me for use with Outlook which worked as expected.
I’m now looking to utilise exactly the same script but for use with Apple’s proprietary email program “Mail” and I can’t seem to get it to work.

As with the Outlook script, I want it to open/activate Mail, begin a new email to a specific email address, with the body text as below, dependent on what is clicked in the dialog that pops up once the Folder Action is initiated.

I’ve managed to get as far as opening up the application, starting a new email with the body text and the subject line. However, I can’t get the files to attach or the specific email address to be placed in the recipient field. FYI I don’t want the email to send - I want that to be still a manual operation.

Any clues what I’m doing wrong?


on adding folder items to thisFolder after receiving DroppedFiles
set myFile to item 1 of DroppedFiles
tell application “Finder” to set NameOfFile to name of myFile
try
set x to button returned of (display dialog NameOfFile & return & return ¬
& “Amend or a v1 Build?” buttons {“Amend”, “v1 Build”, “Cancel”} default button 2)
on error number -128
display notification “You Picked Cancelled”
return
end try

if x is "Amend" then
	set mySubject to NameOfFile & " AMEND FOR APPROVAL"
	set myContent to "Attached is a low res PDF for approval for the amendment to job " & NameOfFile
else
	set mySubject to NameOfFile & " v1 BUILD FOR APPROVAL"
	set myContent to "Attached is a low res PDF for Version 1 Build approval on the job " & NameOfFile
end if

tell application "Mail"
	activate
	tell (make new outgoing message with properties {subject:mySubject, content:myContent})
		make new attachment with properties {file:myFile}
		make new recipient with properties {address:"specificemail@someone.co.uk"}
		open
	end tell
	activate
end tell

end adding folder items to
end

Hi,

Replace this:


make new attachment with properties {file:myFile}
make new recipient with properties {address:"specificemail@someone.co.uk"}
open

with this:


make new attachment with properties {file name:myFile} at after last paragraph
make new recipient at end of to recipients with properties {address:"specificemail@someone.co.uk"}

NOTE: Code line

tell application "Finder" to set NameOfFile to name of myFile

no need here, because file name property works with aliases fine. Second activate Mail.app no need too.

I’m not sure that it’s what puzzles you but I found an oddity in this piece of code

try
	set x to button returned of (display dialog NameOfFile & return & return ¬
		& "Amend or a v1 Build?" buttons {"Amend", "v1 Build", "Cancel"} default button 2)
on error number -128
	display notification "You Picked Cancelled"
	return
end try

Clicking Cancel in your dialog doesn’t issue error number -128.
It just return the button “Cancel”

to achieve your goal you must use :

set x to button returned of (display dialog NameOfFile & return & return ¬
	& "Amend or a v1 Build?" buttons {"Amend", "v1 Build", "Cancel"} default button 2)
if x = "Cancel" then
	tell me to display notification "You Picked Cancelled"
	return
end if

I’m accustomed to use :

try
	set x to button returned of (display dialog NameOfFile & return & return ¬
		& "Amend or a v1 Build?" buttons {"Amend", "v1 Build", "Cancel"} default button 2 cancel button 3)
on error number -128
	tell me display notification "You Picked Cancelled"
	return
end try

in which clicking Cancel (or pressing Escape) really issue the error number -128

Yvan KOENIG running High Sierra 10.13.6 in French (VALLAURIS, France) vendredi 3 avril 2020 19:31:30

Here the OP has right syntax. Returning “Cancel” always accompanied with “User cancelled” error number -128. This is as the interruption in the old days DOS. You can’t catch this button returning other way. But you can catch button “cancel” , “CANCEL”, “cAncel” an so on. Only not “Cancel”. This is special case.

So, to return this special button was pressed you can do only this:

try
	set x to button returned of (display dialog NameOfFile & return & return ¬
		& "Amend or a v1 Build?" buttons {"Amend", "v1 Build", "Cancel"} default button 2)
on error number -128
	set x to "Cancel"
end try

I apologizes, clicking Cancel in this code will never issue an error number -128
I double checked before posting.

try
	set x to button returned of (display dialog NameOfFile & return & return ¬
		& "Amend or a v1 Build?" buttons {"Amend", "v1 Build", "Cancel"} default button 2)
on error number -128
	display notification "You Picked Cancelled"
	return
end try

The error is issued only if the button is defined as the cancel button, what I did in my second proposal.

Yvan KOENIG running High Sierra 10.13.6 in French (VALLAURIS, France) vendredi 3 avril 2020 20:06:20

Have you enabled displaying notifications for your Script Editor? The “Cancel” button is special case as I say, and in the AppleScript is defined as cancel button by default.

You continue to forget that, HAPPILY, everybody is NOT using English.
On non English systems, it’s not the button named Cancel which issue the error number -128 if it’s not defined as the cancel button.

In France it’s “Annuler” which behave this way.
In Italy, it’s “Annula
In Germany, it’s “Abbrechen
and so on …

Using cancel button “Cancel” in an instruction triggering display dialog is the unique way to guarantee that clicking such button will issue error number -128.

Yvan KOENIG running High Sierra 10.13.6 in French (VALLAURIS, France) vendredi 3 avril 2020 20:52:14

KniazidisR thank you for fixing this.
Thanks for your input too Yvan.

Script works now as expected - brilliant!

This forum rocks!