Wednesday, September 18, 2019

#1 2019-09-06 05:22:39 pm

TedTedTed
Member
Registered: 2019-09-06
Posts: 2

Mail attachments with AppleScript in Catalina

The following works in Mojave, to attach a file to a new message:

Applescript:


on send_email(myAttachment)
tell application "Mail"
activate

set theMessage to make new outgoing message
tell theMessage
set filefn to (POSIX file myAttachment) as Unicode text
tell content to make new attachment with properties {file name:filefn as alias} at after last paragraph
end tell
end tell
end send_email

send_email("/path/to/file")

But in Catalina (beta 7), we get the error:

Can’t make POSIX file "/path/to/file" of «class bcke» id 1 of application "Mail" into type Unicode text.

Last edited by TedTedTed (2019-09-07 03:14:44 am)


Filed under: mail, Attachments

Offline

 

#2 2019-09-06 08:03:57 pm

Shane Stanley
Member
From:: Australia
Registered: 2002-12-07
Posts: 5894

Re: Mail attachments with AppleScript in Catalina

Try it like this:

Applescript:

on send_email(myAttachment)
set filefn to (POSIX file myAttachment)
tell application "Mail"
activate

set theMessage to make new outgoing message
tell theMessage
tell content to make new attachment with properties {file name:filefn as alias} at after last paragraph
end tell
end tell
end send_email


Shane Stanley <sstanley@myriad-com.com.au>
www.macosxautomation.com/applescript/apps/
latenightsw.com

Offline

 

#3 2019-09-07 03:14:20 am

TedTedTed
Member
Registered: 2019-09-06
Posts: 2

Re: Mail attachments with AppleScript in Catalina

Thank you, Shane.

Now I’m having trouble in converting that code to allow for multiple attachments (again, I can do it in Mojave), so I could do something like

Applescript:

send_email({"/first/file/path", "/second/file/path"})

A repeat with inside tell application Mail gives a similar error as before, while constructing a new list outside tell application Mail (as in your code) and then using that new list inside produces no error but also doesn’t attach the files.

Offline

 

#4 2019-09-07 05:43:55 am

Shane Stanley
Member
From:: Australia
Registered: 2002-12-07
Posts: 5894

Re: Mail attachments with AppleScript in Catalina

Did you try something like this:

Applescript:

on send_email(myAttachments)
   repeat with i from 1 to count of items of myAttachments
       set item i of myAttachments to (item i of myAttachments) as POSIX file
   end repeat
   tell application "Mail"
       activate
       
       set theMessage to make new outgoing message
       tell theMessage
           repeat with aFile in myAttachments
               tell content to make new attachment with properties {file name:aFile} at after last paragraph
           end repeat
       end tell
   end tell
end send_email


Shane Stanley <sstanley@myriad-com.com.au>
www.macosxautomation.com/applescript/apps/
latenightsw.com

Offline

 

#5 2019-09-07 05:53:15 am

StefanK
Member
From:: St. Gallen, Switzerland
Registered: 2006-10-21
Posts: 11597
Website

Re: Mail attachments with AppleScript in Catalina

@TedTedTed: You can get rid of a lot of type conversions (and of a second repeat loop in this case) if you are using HFS paths ("Macintosh HD:path:to:file.ext"). There are APIs for all standard folders without hard-coding the path

Applescript:

on send_email(myHFSAttachments)
   tell application "Mail"
       activate
       set theMessage to make new outgoing message
       tell content of theMessage
           repeat with anAttachment in myHFSAttachments
               make new attachment with properties {file name:(alias anAttachment)} at after last paragraph
           end repeat
       end tell
   end tell
end send_email

Last edited by StefanK (2019-09-07 05:54:20 am)


regards

Stefan

Offline

 

Board footer

Powered by FluxBB

RSS (new topics) RSS (active topics)