Sunday, November 19, 2017

#1 2015-06-20 08:26:25 am

adjudd1402
Member
Registered: 2015-06-20
Posts: 13

Applescript for attaching files to Mac Mail.. help.

Hi all, I'm working on an applescript to work as the end part of a Filemaker Pro script. It runs at the end by running the below script to allow a user of a FMpro database to add multiple attachments (that have just been exported) to an email with Mac Mail. I have got as far as the below. The new email is generated and the dialog window comes up to add the attachments. Unfortunately when they are selected and you press choose. They don't attach. Any ideas? 



tell application "Mail"
set frontWindow to window
set newMessage to make new outgoing message
tell newMessage
set theAttachment to choose file with multiple selections allowed
end tell
end tell

Offline

 

#2 2015-06-20 08:35:25 am

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

Re: Applescript for attaching files to Mac Mail.. help.

Hi,

first of all, welcome to MacScripter.
Second of all, please post questions which don't involve Xcode (Apple's developer environment) in the AppleScript | OS X section.
Third of all, please use the AppleScript tags for code

An attachment in Mail is an object, so you have to create one attachment object for each file in the content of the message

Applescript:


set theAttachments to choose file with multiple selections allowed
tell application "Mail"
   set newMessage to make new outgoing message
   repeat with anAttachment in theAttachments
       tell newMessage
           tell content to make new attachment with properties {file name:anAttachment} at after the last paragraph
       end tell
   end repeat
end tell

Last edited by StefanK (2015-06-20 08:36:25 am)


regards

Stefan

Offline

 

#3 2015-06-20 08:47:59 am

adjudd1402
Member
Registered: 2015-06-20
Posts: 13

Re: Applescript for attaching files to Mac Mail.. help.

Great, thank you! Do you know if there is a way to make the new email itself (the window) come to the front (as it is appearing behind Filemaker Pro ?) Also, is there a way to add spacing between each attachment in the body of the email?

Offline

 

#4 2015-06-20 08:56:41 am

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

Re: Applescript for attaching files to Mac Mail.. help.

Add a line to activate Mail and to make the message window visible

Applescript:


set theAttachments to choose file with multiple selections allowed
tell application "Mail"
   activate
   set newMessage to make new outgoing message with properties {visible:true}
…

Regarding the spacing I have no idea, but as the content property of a message is plain text, it might be possible to add characters


regards

Stefan

Offline

 

#5 2015-06-20 09:08:55 am

adjudd1402
Member
Registered: 2015-06-20
Posts: 13

Re: Applescript for attaching files to Mac Mail.. help.

Thanks so much. I would for example want to add the filenames of the attachments between each one.. tricky..

Offline

 

#6 2015-06-20 09:20:20 am

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

Re: Applescript for attaching files to Mac Mail.. help.

actually the filenames are displayed anyway in the message


regards

Stefan

Offline

 

#7 2015-06-20 09:23:48 am

adjudd1402
Member
Registered: 2015-06-20
Posts: 13

Re: Applescript for attaching files to Mac Mail.. help.

thanks, they are not in words..?   it just adds the images as attachments (in this case jpegs).

kind regards

Offline

 

#8 2015-06-20 09:59:03 am

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

Re: Applescript for attaching files to Mac Mail.. help.

try this

Applescript:


set attachmentFiles to choose file with multiple selections allowed
set theContent to ""
set attachmentCount to (count attachmentFiles) * 3
repeat with aFile in attachmentFiles
   tell application "System Events" to set fileName to name of aFile
   set theContent to theContent & fileName & return & return & return
end repeat

tell application "Mail"
   activate
   set newMessage to make new outgoing message with properties {content:theContent, visible:true}
   repeat with anAttachment in attachmentFiles
       tell newMessage
           tell content to make new attachment with properties {file name:anAttachment} at after paragraph (-attachmentCount + 1)
           set attachmentCount to attachmentCount - 3
       end tell
   end repeat
end tell


regards

Stefan

Offline

 

#9 2015-06-20 11:10:39 am

adjudd1402
Member
Registered: 2015-06-20
Posts: 13

Re: Applescript for attaching files to Mac Mail.. help.

amazing. thank you

Offline

 

#10 2015-06-20 11:34:25 am

adjudd1402
Member
Registered: 2015-06-20
Posts: 13

Re: Applescript for attaching files to Mac Mail.. help.

Sorry to be fussy, just wondering how to make the text appear below the images. Also, would there be a way to leave out the extension part (.jpeg).

Thanks so much.

Offline

 

#11 2015-06-20 12:01:52 pm

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

Re: Applescript for attaching files to Mac Mail.. help.

here we go

Applescript:


set attachmentFiles to choose file with multiple selections allowed
set theContent to ""
set attachmentCount to (count attachmentFiles) * 3
repeat with aFile in attachmentFiles
   tell application "System Events" to set {fileName, fileExtension} to {name, name extension} of aFile
   set baseName to text 1 thru ((get offset of "." & fileExtension in fileName) - 1) of fileName
   set theContent to theContent & return & return & return & baseName
end repeat

tell application "Mail"
   activate
   set newMessage to make new outgoing message with properties {content:theContent, visible:true}
   repeat with anAttachment in attachmentFiles
       tell newMessage
           tell content to make new attachment with properties {file name:anAttachment} at after paragraph -attachmentCount
           set attachmentCount to attachmentCount - 3
       end tell
   end repeat
end tell


regards

Stefan

Offline

 

#12 2015-06-20 12:05:12 pm

adjudd1402
Member
Registered: 2015-06-20
Posts: 13

Re: Applescript for attaching files to Mac Mail.. help.

Fantastic, thanks so much!! One (hopefully last) question. In making the new email any signature as set up within mac mail preferences get erased. Would there be a way to keep that in?

Offline

 

#13 2015-06-20 12:18:44 pm

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

Re: Applescript for attaching files to Mac Mail.. help.

try to insert these lines before the last end tell line and replace mySignature with the proper signature name

Applescript:


delay 1
set message signature of newMessage to signature "mySignature"


regards

Stefan

Offline

 

#14 2015-06-20 12:51:32 pm

adjudd1402
Member
Registered: 2015-06-20
Posts: 13

Re: Applescript for attaching files to Mac Mail.. help.

Thanks for your help!

Offline

 

#15 2015-06-21 06:51:10 am

adjudd1402
Member
Registered: 2015-06-20
Posts: 13

Re: Applescript for attaching files to Mac Mail.. help.

hi there, this might be a bit outside the box but I wondered if there was a way to step the applescript to automatically create a new email and automatically add attachments to it that were made / created on the desktop in the last say 5-10 seconds?

That would skip the step of the user having to add the attachments.

This would always work because this applescript runs a second after the user have exported their chosen images from the database so the desktop (always to the desktop).

thank you!

Offline

 

#16 2015-06-21 07:02:21 am

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

Re: Applescript for attaching files to Mac Mail.. help.

Basically it's possible with a folder action.
A folder action is attached to a specific folder and the associated script is triggered whenever a file is added to the folder.

The generic folder action code for adding items

Applescript:


on adding folder items to this_folder after receiving attachmentFiles
   â€¦
end adding folder items to

wraps the whole script and replaces the choose file line


regards

Stefan

Offline

 

#17 2015-06-21 08:31:19 am

adjudd1402
Member
Registered: 2015-06-20
Posts: 13

Re: Applescript for attaching files to Mac Mail.. help.

Thanks, so in my case I would need to add the first part at the beginning of my script and the last part at the end?

How do you make it state "desktop" and how does it know to grab any recent images there, could be 2 could be 10 etc?

Sorry, pretty new to all this..

thank you!

Offline

 

#18 2015-06-21 08:49:23 am

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

Re: Applescript for attaching files to Mac Mail.. help.

adjudd1402 wrote:

Thanks, so in my case I would need to add the first part at the beginning of my script and the last part at the end?


Yes, exactly

adjudd1402 wrote:

How do you make it state "desktop" and how does it know to grab any recent images there, could be 2 could be 10 etc?


please read the folder action documentation how to attach an action to a folder and a script to the action.
Folder Actions Reference

However you might run into a problem when the whole set of images is not added at the same time.
For example 3 files have been exported from somewhere and are added to the folder one per second, in this case the folder action is triggered three times, which is not what you want.
Now it gets complicated because you need something like a timeout logic "create the message after 10 seconds of inactivity". This is quite difficult to accomplish in conjunction with a folder action


regards

Stefan

Offline

 

#19 2016-04-28 10:41:33 am

japino
Member
Registered: 2008-03-18
Posts: 13

Re: Applescript for attaching files to Mac Mail.. help.

I'm reviving this topic because I cannot get something to work. My script looks like this:

Applescript:

tell application "Finder"
   set myFolder to (path to desktop as text) & "From FileMaker"
   set attachmentFiles to get every item of (entire contents of folder myFolder)
   
   set mySubject to "This is my subject"
   
   set theContent to "Hi all" & return & return & "Blablabla" & return & return & "Best regards," & return & "Me"
   
   set attachmentCount to (count attachmentFiles) * 3
   repeat with aFile in attachmentFiles
       set theContent to theContent & return & return & return
   end repeat
   
   tell application "Mail"
       activate
       set newMessage to make new outgoing message with properties {subject:mySubject, content:theContent, visible:true}
       tell newMessage
           make new to recipient with properties {address:"blabla@blabla.com"}
       end tell
       
       repeat with anAttachment in attachmentFiles
           tell newMessage
               set anAttachment to anAttachment as alias
               tell content to make new attachment with properties {file name:anAttachment} at after paragraph (-attachmentCount + 1)
               set attachmentCount to attachmentCount - 3
           end tell
       end repeat
   end tell
end tell

It works well for more than 1 attachment. But if there is just 1 attachment, the attachment isn't added to the mail. It would be great if someone more knowledgable could take a look because I've been trying to understand what's going on for the last hour or so, and I didn't succeed.

Offline

 

#20 2016-04-29 03:43:36 am

japino
Member
Registered: 2008-03-18
Posts: 13

Re: Applescript for attaching files to Mac Mail.. help.

Never mind. I figured it out.

To get the files, I now use:

Applescript:

set attachmentFiles to get files of folder myFolder

And in the repeat loop I now use "as alias":

Applescript:

           repeat with anAttachment in attachmentFiles
               tell newMessage
                   set anAttachment to anAttachment as alias
                   tell content to make new attachment with properties {file name:anAttachment} at after paragraph (-attachmentCount + 1)
                   set attachmentCount to attachmentCount - 3
               end tell
           end repeat

Hope this helps some later reader.

Offline

 

Board footer

Powered by FluxBB

RSS (new topics) RSS (active topics)