Hey Viking,
Thank you so much for taking the time to help me with this!!!! You're amazing.
I'm trying to make a couple changes but I'm not much of a coder and I seem to have broken it. For my use case, I'd like the subject of the e-mail to simply be the name of the folder, without the bit that says "Folder Name: ". I'd also like the anchor text to be that same folder name automatically. Last, I'd like to pre-fill in the email addresses and then just have a different script that I save for each set of recipients. My email has a signature that it automatically applies to the end, so I also removed the "best, Jake" part. I also attempted to change the font size of the body to 12pt instead of 17pt.
Any guidance as to what I did wrong? The error I get is "[-1728] can't get selection"
-- NSSharingService_HMTL_Mail.applescript
(*
Construct HTML email within Apple Mail by using NSSharingService
Assumptions for this script previous to running it:
1) Dropbox folder is selected in the Finder
2) Hyperlink to the Drop Box folder is previously copied to the clipboard
This script was adapted from and made possible by Sal Soghoian from the the following
link:
https://forum.latenightsw.com/t/create-mac-mail-msg-with-rich-text-content-that-also-has-cc-address-filled-in/2037
Tested: macOS 11.2.3
VikingOSX, 2021-03-28, Apple Support Communities, no warranties expressed or implied
*)
use framework "Foundation"
use framework "AppKit"
use AppleScript version "2.4" -- Yosemite or later
use scripting additions
# set DEBUG to true if you want to automatically send the email
property DEBUG : false
property NSString : a reference to current application's NSString
property NSAttributedString : a reference to current application's NSAttributedString
property NSUTF8StringEncoding : a reference to current application's NSUTF8StringEncoding
property NSSharingService : a reference to current application's NSSharingService
property NSSharingServiceNameComposeEmail : a reference to current application's NSSharingServiceNameComposeEmail
property NSCharacterSet : a reference to current application's NSCharacterSet
# validate inputs
try
set anchorTxt to name of item 1 of (get selection)
set recipients to "marketing@xxxxx.com, sales@xxxxx.com, admin@xxxxx.com"
# ensure drop box folder was selected before running the script
# so we can actually get its name
tell application "Finder"
if (get selection) is {} then error number -128
set messageSubject to name of item 1 of (get selection)
end tell
set aURL to (the clipboard)
if (aURL does not start with "http") then error number -128
set anchor_link to my make_anchor_link(aURL, anchorTxt) as text
on error errmsg number errNo
my error_handler(errNo, errmsg)
return
end try
# mail body
set msgHTML to "<!DOCTYPE html>
<html lang=\"en\">
<head>
<meta charset=\"UTF-8\">
<meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\">
<title>Document</title>
</head>
<body>
<!-- sadly one cannot use style block in the HTML head section -->
<p style=\"font-family:sans-serif;font-size:1em;\">Hey!</p>
<p style=\"font-family:sans-serif;font-size:1em;\">Here's your pictures from " & anchor_link & " </p>
<p>
<p style=\"font-family:sans-serif;font-size:1em;\">And here's your Zillow 3D Tour Link:</p>
</body>
</html>"
set theSource to NSString's stringWithString:msgHTML
set theData to theSource's dataUsingEncoding:NSUTF8StringEncoding
set anAttributedString to NSAttributedString's alloc()'s initWithHTML:theData documentAttributes:{}
-- USE THE MAIL SHARING SERVICE TO CREATE A NEW MAIL MESSAGE
set aSharingService to NSSharingService's sharingServiceNamed:(NSSharingServiceNameComposeEmail)
if aSharingService's canPerformWithItems:{"someone@somewhere.com"} then
set aSharingService's subject to messageSubject
set aSharingService's recipients to recipientAddresses
tell aSharingService to performSelectorOnMainThread:"performWithItems:" withObject:{anAttributedString} waitUntilDone:false
end if
if DEBUG then
# click the send button on the open compose window
tell application "System Events"
tell process "Mail"
set frontmost to true
keystroke "D" using {shift down, command down}
end tell
end tell
end if
return
on make_anchor_link(aURL, anchor)
return (" <a href=\"" & aURL & "\">" & anchor & "</a>") as text
end make_anchor_link
on error_handler(nbr, msg)
return display alert "[ " & nbr & " ] " & msg as critical giving up after 10
end error_handler