Automator to copy the file name and apply DropBox link.

I'm a photographer and I share pictures via dropbox. I'd like to set up a script to copy the name of the dropbox folder in finder, paste it into an email subject line, and then paste it into the body of the message. I then need to copy the drop-box link of the folder and apply that link to the text in the email.


I got it copying the folder name into the subject field. And it's actually pasting the folder name into the body of the email as well, although I'm not sure why. I get stuck when I want it to copy the dropbox link though.


See the attached pictures for what I have so far.

Posted on Mar 23, 2021 9:09 PM

Reply

Similar questions

25 replies

Mar 26, 2021 9:58 AM in response to VikingOSX

Viking,


That's a nifty hack for extracting the variable mid-stream. I like that.


However, I'm trying to get my head around this approach. What does it do that a pure AppleScript wouldn't do? I mean, if the URL is on the clipboard at the invocation of the script, surely you could reference that rather than go through the machinations of extracting the variable's UID, no?

Apr 1, 2021 1:50 PM in response to VikingOSX

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 ("&nbsp;&nbsp;<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

Apr 9, 2021 1:14 AM in response to VikingOSX

Due to security provisions in Catalina and Big Sur, on the initial run of your Automator application, you will receive dialog challenges (definitely plural) comparable to the following that want access to control other applications. Grant this access by always clicking OK, because if you click Don't Allow, you will pay dearly to get the application running properly because these accesses have to then be set up manually. In the following, email is the name of the application:




Apr 28, 2021 8:43 AM in response to VikingOSX

That seems to work great when I run the app through the little "play" button in the top right corner of automator. But when I try to launch it by double clicking the app I've saved, a little gear shows up on my menu bar (top right corner by date and time), it spins for like 3 seconds, disappears, and then nothing else happens. Any thoughts on what might be causing this? The script itself seems to be working correctly, since it works when I run it from within automator. I've tried googling around by I'm not finding any solutions.

This thread has been closed by the system or the community team. You may vote for any posts you find helpful, or search the Community for additional answers.

Automator to copy the file name and apply DropBox link.

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