Thursday, July 9, 2020

#1 2020-04-03 10:38:12 am

TheBlackBinLinerAtWork
Member
Registered: 2020-01-20
Posts: 5

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
---------------------------------------------------------------------------------------------------------------------------

Offline

 

#2 2020-04-03 11:22:40 am

KniazidisR
Member
Registered: 2019-03-03
Posts: 1259

Re: Applescript send Mail with attachment from Folder Action

Hi,

Replace this:

Applescript:


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

with this:

Applescript:


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

Applescript:

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.

Last edited by KniazidisR (2020-04-03 12:02:56 pm)


Model: MacBook Pro
OS X: Catalina 10.15.4
Web Browser: Safari 13.1
Ram: 4 GB

Offline

 

#3 2020-04-03 11:32:18 am

Yvan Koenig
Member
Registered: 2006-09-14
Posts: 4544

Re: Applescript send Mail with attachment from Folder Action

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

Applescript:

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 :

Applescript:

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 :

Applescript:

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

Offline

 

#4 2020-04-03 11:43:18 am

KniazidisR
Member
Registered: 2019-03-03
Posts: 1259

Re: Applescript send Mail with attachment from Folder Action

Yvan Koenig wrote:

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

Applescript:

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"


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:

Applescript:

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

Last edited by KniazidisR (2020-04-03 11:58:04 am)


Model: MacBook Pro
OS X: Catalina 10.15.4
Web Browser: Safari 13.1
Ram: 4 GB

Offline

 

#5 2020-04-03 12:06:25 pm

Yvan Koenig
Member
Registered: 2006-09-14
Posts: 4544

Re: Applescript send Mail with attachment from Folder Action

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

Applescript:

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

Offline

 

#6 2020-04-03 12:26:25 pm

KniazidisR
Member
Registered: 2019-03-03
Posts: 1259

Re: Applescript send Mail with attachment from Folder Action

Yvan Koenig wrote:

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

Applescript:

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.


Model: MacBook Pro
OS X: Catalina 10.15.4
Web Browser: Safari 13.1
Ram: 4 GB

Offline

 

#7 2020-04-03 12:52:21 pm

Yvan Koenig
Member
Registered: 2006-09-14
Posts: 4544

Re: Applescript send Mail with attachment from Folder Action

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

Last edited by Yvan Koenig (2020-04-03 12:53:08 pm)

Offline

 

#8 2020-04-04 03:12:09 am

TheBlackBinLinerAtWork
Member
Registered: 2020-01-20
Posts: 5

Re: Applescript send Mail with attachment from Folder Action

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

Script works now as expected - brilliant!

This forum rocks!

Offline

 

Board footer

Powered by FluxBB

RSS (new topics) RSS (active topics)