Apple Event: May 7th at 7 am PT

Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

Adding Hyperlinked Image Attachment To an E-mail

tell application "Mail"


set thePath to choose file

tell application "System Events"

set theAttachment to path of thePath

end tell


set theSender to "name@damain.com"



-- In the unedited script, the content would be entered via dialog boxes.

set theSubject to "Testing: 1, 2, 3…"

set theBody to "This is a test."



-- In the unedited script, the recipient information would be retrieved from a txt file.

set emailAddress to "recipient@domain.com"

set emailName to "John Smith"


set theContent to emailName & "," & return & return & theBody & return & return


set theMessage to makenewoutgoing messagewith properties {subject:theSubject, content:theContent, visible:true}

set sender of theMessage to theSender


tell theMessage


makenewto recipientat end of every to recipientwith properties {address:emailAddress}


makenewattachmentwith properties {file name:theAttachment as alias}

end tell



--send theMessage


end tell

Posted on Jan 22, 2014 10:09 AM

Reply
17 replies

Jan 23, 2014 6:01 AM in response to Ratboy

When editing this question, I acidentally removed the question I am asking:


I'm creating an AppleScript to send an e-mail to a list of contacts. The script requests the subject and content of the e-mail and creates a new message with the sender's address and a personal salutation froma file. The script also allows the option to include an attachment. I would like to be able to add a hyperlinkto the image, but I can't figure out how to do this. Below is an edited, simplified version of my script without any code that I don't believe is pertinent to the question:

Jan 23, 2014 4:20 PM in response to Ratboy

The following version of your script, tested under OS X 10.9.1, seems to do the trick.


tell application "Mail"


set thePath to choose file

tell application "System Events"

set theAttachment to path of thePath

end tell


set theSender to "name@damain.com"


-- In the unedited script, the content would be entered via dialog boxes.

set theSubject to "Testing: 1, 2, 3…"

set theBody to "This is a test."


-- In the unedited script, the recipient information would be retrieved from a txt file.

set emailAddress to "recipient@domain.com"

set emailName to "John Smith"


set theContent to emailName & "," & return & return & theBody & return & return


set theMessage to makenewoutgoing messagewith properties {subject:theSubject, content:theContent, visible:true}

set sender of theMessage to theSender


tell theMessage

makenewto recipientat end of every to recipientwith properties {address:emailAddress}

makenewattachmentwith properties {file name:theAttachment as alias}

end tell


set theLink to "http://www.domain.com"

set the clipboard totheLink


tell application "System Events"

repeat until (UI element 1 of group -1 of UI element 1 of scroll area 1 of window 1 of process "Mail" whose subrole is "AXTextAttachment") exists

end repeat

key code 125 using {command down} -- command + down arrow

key code 123 using {shift down} -- shift + left arrow

keystroke "k" using {command down} -- Edit > Add Link…

keystroke "v" using {command down} -- Edit > Paste

keystrokereturn-- click "OK"

key code 126 using {command down} -- command + up arrow

endtell


--send theMessage


end tell


See Graphic User Interface (GUI) Scripting.

Under Mavericks, see also Accessibility Preferences and GUI Scripting.


Message was edited by: Pierre L. (slightly modified code)

Jan 24, 2014 3:15 AM in response to Pierre L.

Pierre L. wrote:


The following version of your script, tested under OS X 10.9.1, seems to do the trick.


I can only test on Leopard but unless I am mistaken this fails to do what is required in 2 respects:


  1. Clicking the image in the received email does not open a browser at the hyperlink
  2. The received email image is attached and not embedded in the html - this is slightly confused because Apple Mail always defines 'inline' images even when they are strictly not inline but attached, ie after the message and not within it


So far my research indicates that many people have tried to do this without any very satisfactory results. The only way I have found so far is to embed the binary UTF8 image data within HTML in the script. This works reliably and is very flexible but it has the following disadvantages:


  1. images have to be converted to UTF8
  2. the sender needs to know how to use HTML
  3. it only works with the email set to invisible in the script


There are several script based (not AppleScript) utilities that can do this.


I will document my slightly manual AppleScript method here later.

Jan 24, 2014 5:39 AM in response to Neville Hillyer

Neville Hillyer wrote:

I can only test on Leopard but unless I am mistaken this fails to do what is required in 2 respects:



  1. Clicking the image in the received email does not open a browser at the hyperlink
  2. The received email image is attached and not embedded in the html - this is slightly confused because Apple Mail always defines 'inline' images even when they are strictly not inline but attached, ie after the message and not within it


  1. I have tested the script again with a 2 MB JPEG picture attached to the email (copying and pasting the posted version, and changing only the to recipient address). Clicking the image in the received email did open the Domain.com Website. (Did you try replacing the repeat block in the script with something like “delay 3?)
  2. I don't actually understand your second point (I must say I don't know very much about Mail.app). Why should the image be “embedded in the html”, and what does that mean? By the way, I didn't change anything to the OP's code, I just added a few lines.


Message was edited by: Pierre L.

Jan 24, 2014 6:32 AM in response to Pierre L.

GOLD STAR!!!


This works for me with a couple of tweeks:


  1. I added "activate" after "tellapplication "Mail"" because the script was pasting the URL into the AppleScript Editor window.
  2. I added a delay before sending the message, because it would otherwise get sent without giving System Events enough time to perform its actions.


Would you explain exactly how "repeatuntil (UI element 1 ofgroup -1 ofUI element 1 ofscroll area 1 ofwindow 1 ofprocess "Mail" whosesubroleis "AXTextAttachment") exists" works. I understand that it's going through the message looking for an attachment, but I'm unclear each command's role in the process ("group -1" in particular). If I were to place the attachment in the middle of the message (e.g.: "makenewattachmentwith properties {file name:theAttachment as alias} at before paragraph -7"), would that change the code you added?


Thanks!

Jan 24, 2014 6:29 AM in response to Pierre L.

I can see how it is intended to work in later versions of Mail now that I have replaced the following with delay 4.


repeat until (UI element 1 of group -1 of UI element 1 of scroll area 1 of window 1 ofprocess "Mail" whose subrole is "AXTextAttachment") exists

end repeat

It does not work in my Mail 3.6 as there are no keyboard shortcuts for Link > Add.


However it appears to be an excellent solution to the OP's problem.

Jan 24, 2014 6:45 AM in response to Neville Hillyer

Whilst my solution is not very good I will put it here for the record. Change both email addresses to test.


set src1 to "src='data:image/gif;base64,R0lGODlhCAAIAPMAAP////zzBf9kAt0IBvIIhEYApQAA1AKr6h+3F ABkEVYsBZBxOsDAwICAgEBA

QAAAACH5BAEAAA8ALAAAAAAIAAgAQwQS8Mkx5KMYW2p37dcXZtwkelYEACH+HWNsaXAyZ2lmIDAu

Ny4yIGJ5IFl2ZXMgUGlndWV0ADs=' "


tellapplication"Mail"


setmsgtomakenewoutgoing messagewith properties{sender:"peter@pan.never",subject:"test"}


tellmsg


makenewto recipientwith properties{address:"wendy@pan.never"}

set html content to "<html><body bgcolor='#eee' text='#00f'>

<!-- Use normal html with inline style after this line - do not use double quotes -->

<br>

<hr style='color:pink; background-color:pink; height:4px; width:60%; text-align:middle'>

<br>

<p style='color:navy; font-size:xx-large'>Thanks for the email.</p>

<p style='color:brown; font-size:x-large'>I would appreciate feedback about your project.</p>

<p style='color:blue; font-size:medium'>Please contact me if you experienced any difficulties.</p>

<p style='text-align:center'><a href='http://links.zero.eu.org/'><img " & src1 & " hspace=30 vspace=20><img " & src1 & " hspace=30 vspace=20><img " & src1 & " hspace=30 vspace=20></a></p>

<p style='color:navy; font-size:medium'>Sincerely

<br><br>

Peter Pan</p>

</body></html>"


send


endtell

endtell

Jan 24, 2014 7:28 AM in response to Ratboy

Ratboy wrote:


GOLD STAR!!!


This works for me with a couple of tweeks:


  1. I added "activate" after "tellapplication "Mail"" because the script was pasting the URL into the AppleScript Editor window.
  2. I added a delay before sending the message, because it would otherwise get sent without giving System Events enough time to perform its actions.


Would you explain exactly how "repeatuntil (UI element 1 ofgroup -1 ofUI element 1 ofscroll area 1 ofwindow 1 ofprocess "Mail" whosesubroleis "AXTextAttachment") exists" works. I understand that it's going through the message looking for an attachment, but I'm unclear each command's role in the process ("group -1" in particular). If I were to place the attachment in the middle of the message (e.g.: "makenewattachmentwith properties {file name:theAttachment as alias} at before paragraph -7"), would that change the code you added?


Thanks!



You're right. One should always activate an application before using GUI Scripting. You could also have placed “activate” immediately before the “tellapplication "System Events"” line.


The repeat block


repeat until (UI element 1 of group -1 of UI element 1 of scroll area 1 of window 1 of process "Mail" whose subrole is "AXTextAttachment") exists

end repeat

just means: “Wait until the attachment exists at the end of the message.” group -1 is exactly the same as lastgroup and refers to an attachment placed at the end of the message. If you were to place the attachment in the middle of the message, for example before paragraph -7, the repeat block would become:

repeat until (UI element 1 of group 6 of UI element 1 of scroll area 1 of window 1 of process "Mail" whose subrole is "AXTextAttachment") exists

end repeat

I used Accessibility Inspector to get the information about the attachment in your original script:


User uploaded file


Message was edited by: Pierre L.

Jan 24, 2014 8:08 AM in response to Ratboy

Many thanks for your feedback.


Here's an improved version of the code that could be added to the script from your original post. It takes into account both of the tweeks you had to make to the first version.


set theLink to "http://www.domain.com"

set the clipboard totheLink

activate

tell application "System Events"

repeat until (UI element 1 of group -1 of UI element 1 of scroll area 1 of window 1 of process "Mail" whose subrole is "AXTextAttachment") exists

end repeat

key code 125 using {command down} -- command + down arrow

key code 123 using {shift down} -- shift + left arrow

keystroke "k" using {command down} -- Edit > Add Link…

keystroke "v" using {command down} -- Edit > Paste

keystrokereturn-- click "OK"

repeat until (menu item "Edit Link…" of menu 1 of menu bar item "Edit" of menu bar 1 of process "Mail") exists

end repeat

end tell

Jan 24, 2014 1:38 PM in response to Pierre L.

Below is a version which works on older Macs with Mail 3.6. Change both email addresses to test it. I am not sure why I could not get Pierre's version to work on my Mac.


Am I correct in thinking that none of these except my HTML version will work with multiple images or with a single image with text above and below it?



tellapplication"Mail"


setthePathtochoose file

set txt to "Wendy" & return & return & "This is a test." & return & return


setmsgtomakenewoutgoing messagewith properties{sender:"peter@pan.never",subject:"Test",content:txt,visible:true}

set the clipboard to "http://www.domain.com"

tell application "System Events" to set attach to path of thePath


tellmsg


makenewto recipientwith properties{address:"wendy@pan.never"}


makenewattachmentwith properties{file name:attachasalias}

tell application "System Events" to tell process "Mail"

repeat until enabled of menu item "Remove Attachments" of menu 1 of menu bar item "Message" of menu bar 1


endrepeat


key code125using{command down}-- command + down arrow


key code123using{shift down}-- shift + left arrow

delay 1


clickmenu item"Add…"ofmenu1ofmenu item"Link"ofmenu1ofmenu bar item"Edit"ofmenu bar1

delay 2


keystroke"v"using{command down}-- Edit > Paste


keystrokereturn-- click "OK"


endtell


send


endtell

endtell

Jan 24, 2014 4:18 PM in response to Neville Hillyer

Neville Hillyer wrote:


Below is a version which works on older Macs with Mail 3.6. Change both email addresses to test it. I am not sure why I could not get Pierre's version to work on my Mac.


In order for your new version to work also under Mavericks, you would have to replace


click menu item "Add…" of menu 1 of menu item "Link" of menu 1 of menu bar item "Edit" of menu bar 1


with something like this:

tell menu 1 of menu bar item "Edit" of menu bar 1

considering numeric strings

if version of application "Mail" < "7.1" then

click menu item "Add…" of menu 1 of menu item "Link"

else

click menu item "Add Link…"

end if

end considering

end tell


I don't know, however, is "7.1" is the right value to use as a threshold.


Am I correct in thinking that none of these except my HTML version will work with multiple images or with a single image with text above and below it?


I would say so.

Adding Hyperlinked Image Attachment To an E-mail

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple ID.