Created
August 3, 2016 05:46
-
-
Save JMichaelTX/603c98d5f12ffcf5be1b080285982437 to your computer and use it in GitHub Desktop.
AppleScript to Create Rich Text Hyperlink with Custom Protocol from Selected Text
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
property gstrScriptName : "Create Hyperlink with Custom Protocol from Selected Text" | |
property gstrScriptVer : "1.0.1" | |
property gstrScriptDate : "Mon, Nov 16, 2015" | |
(* | |
—————————————————————————————————————————————————— | |
PURPOSE: | |
• Based on the User's selection, create and put on Clipboard: | |
• RTF formatted Hyperlink | |
• This supports custom URL Schemes/Protocols not supported by Evernote | |
• Paste the Hyperlink back into the document, replacing the selected text | |
• While this was designed for Evernote Mac, it should work in all apps. | |
• Default font is Verdana 14pt, but can be easily changed below. | |
VERSION HISTORY: | |
CURRENT: (Ver# & Date at top) | |
• Replaced validateURL() function with contains "://" | |
• Removed the CreatedDate variable | |
PRIOR: | |
• 1.0 Nov 11, 2015 First Release | |
AUTHOR: | |
• @JMichaelTX (member of Evernote forum (https://discussion.evernote.com/)) | |
• Please post or PM me with any bugs/issues/questions | |
INSTALLATION: | |
• Copy to your personal Scripts folder: /Users/<YourName>/Library/Scripts/ | |
(create the Scripts folder if it does not exist) | |
OR other location if you prefer. | |
• OPTIONAL: Use FastScripts to easily assign shortcut key | |
REQUIRES: | |
• A number of functions/handlers, all of which are at the bottom of this file. | |
HOW TO USE | |
• Select text in your Evernote Note (or other app document) that you want to be a custom hyperlink | |
• Execute this script, either from the Apple Script menu, or from FastScripts (recommended) | |
METHOD: | |
• IF the user has selected text it will be used as the text for the hyperlink | |
• Otherwise the user will be prompted for the text | |
• Then the user is prompted for the URL, which must be in the format of "protocol://" | |
• The standard HTML hyperlink is created: | |
<a href="protocol://xxx">Display Text</a> | |
• The hyperlink is then copied to standard Clipboard as Rich Text | |
• The user's selected text is replaced by the hyperlink. | |
• If nothing was selected, it will be pasted at the insertion point. | |
—————————————————————————————————————————————————— | |
*) | |
---------------------------------- | |
property bolDebug : false -- set to true to turn on diagnostic logs and dialogs. | |
---------------------------------- | |
--- SET THESE FONT ATTRIBUTES AS YOU LIKE FOR THE HYPERLINK --- | |
-- Use the std HTML CSS Styles | |
property gStyleLink : "color:blue" | |
property gstrFont : "font-family:verdana,geneva,sans-serif;" | |
property gstrLinkFontSize : "font-size:14px;" | |
------------- | |
property LF : linefeed | |
property gstrLastSection : "Beginning of Script" -- used in error handling to provide general location of error | |
property gstrScriptTitle : "Title" | |
property gActiveApp : "" | |
--—————————————————————————————————————————————————— | |
-- GET THE NAME OF THE CURRENT APP -- | |
tell application "System Events" | |
set gActiveApp to name of first application process whose frontmost is true | |
end tell | |
set gstrScriptTitle to (gstrScriptName & " " & gstrScriptVer & " -- " & gstrScriptDate) | |
if bolDebug then | |
beep | |
display dialog "START Script: " & gstrScriptTitle | |
log "Date Created: " & gstrScriptDate | |
end if -- bolDebug | |
set gstrLastSection to "COMPLETE Initialization" | |
###——————————————— | |
try | |
###——————————————— | |
set strCBConfirm to "NONE" --"FULL" | |
--- GET LINK TEXT AND URL FROM USER -- | |
set strLinkText to my getTextforLink() | |
set strURL to my getURLfromUser(strLinkText) | |
--- CREATE HTML HYPERLINK --- | |
set strHTMLLink to my createHTMLLink(strLinkText, strURL) | |
--- COPY TO CLIPBOARD AS RTF (Rich Text Format) --- | |
my copyHTMLasRTFtoClipboard(strHTMLLink) | |
--- PASTE FROM CLIPBOARD TO DOCUMENT (overwrites selected text) -- | |
my pasteClipboard() | |
--- NOTIFY USER SCRIPT IS COMPLETE (if so requested) --- | |
set strMsg to ¬ | |
"▶▶▶ Script COMPLETE ◀◀◀" & ¬ | |
" | |
" & ¬ | |
"Clipboard set to RTF Hyperlink for: " & " | |
" & strLinkText & " | |
" & strURL & ¬ | |
" | |
" & "Script Ver: " & gstrScriptVer & " " & gstrScriptDate | |
if strCBConfirm = "FULL" then | |
--- DISPLAY FULL CONFIRMATION --- | |
beep | |
display dialog (strMsg) with title gstrScriptName | |
else if strCBConfirm = "BRIEF" then | |
--- DISPLAY BRIEF SILENT CONFIRMATION w/ auto-close --- | |
set strMsg to "Clipboard set to RTF Hyperlink for " & strHTMLLink | |
display notification strMsg with title gstrScriptName subtitle "SUCCESS!" | |
else | |
-- Don't display any confirmation -- | |
end if -- strCBConfirm = "FULL" | |
--================= | |
-- END OF MAIN SCRIPT | |
--================= | |
--——————————————— | |
on error pErrMsg number pErrNum -- HANDLE ERRORS FOR ENTIRE SCRIPT | |
--——————————————— | |
set the clipboard to pErrMsg & " | |
" & gstrLastSection | |
--- DISPLAY DIALOG TO USER WITH ERROR INFO & ABORT SCRIPT -- | |
beep | |
display dialog "▶▶▶ YIKES! Something's wrong! ◀◀◀" & ¬ | |
" | |
Err#: " & pErrNum & " | |
Err Msg: " & pErrMsg ¬ | |
& " | |
" & "Last Section: " & gstrLastSection ¬ | |
& " | |
" & ¬ | |
"(Clipboard set to Err Msg & Last Section)" & " | |
Ver:" & gstrScriptVer & " -- " & gstrScriptDate ¬ | |
with title gstrScriptName with icon stop | |
###——————————————— | |
end try | |
###——————————————— | |
###—————————————————————————————————————————————— | |
### FUNCTIONS / HANDLERS | |
###—————————————————————————————————————————————— | |
###—————————————————————————————————————————————— | |
# pasteClipboard() Paste Contents of Clipboard to Current App Document | |
###—————————————————————————————————————————————— | |
on pasteClipboard() | |
activate application gActiveApp | |
delay 0.2 | |
tell application "System Events" | |
keystroke "v" using command down -- this is same as CMD-V | |
end tell | |
end pasteClipboard | |
###—————————————————————————————————————————————— | |
# getTextforLink() Get Text to be used with HTML Hyperlink | |
###—————————————————————————————————————————————— | |
on getTextforLink() | |
set strOldClipboard to (the clipboard) | |
--- COPY USER SELECTED TEXT (if any) --- | |
set the clipboard to "NO SELECTION" | |
activate application gActiveApp | |
delay 0.2 -- MUST have this delay to make sure the "activate" command has completed | |
tell application "System Events" | |
keystroke "c" using command down -- this is same as CMD-C | |
delay 0.2 -- use a small delay to give the App time to complete the copy | |
end tell | |
tell application gActiveApp --gActiveApp | |
set strSelectedText to (the clipboard as text) | |
end tell | |
if strSelectedText = "NO SELECTION" then | |
--- IF NO TEXT SELECTED, PROMPT USER FOR LINK TEXT --- | |
set strMsg to "Enter TEXT for your Hyperlink (no text was selected)" | |
set strAnswer to "" | |
set recResult to display dialog strMsg default answer strAnswer with title gstrScriptName with icon caution | |
set strButton to button returned of recResult | |
set strAnswer to text returned of recResult | |
else | |
--- USE SELECTED TEXT --- | |
set strAnswer to strSelectedText | |
end if | |
--- RESTORE CLIPBOARD --- | |
set the clipboard to strOldClipboard | |
return strAnswer | |
end getTextforLink | |
###—————————————————————————————————————————————— | |
# Create HTML Link: createHTMLLink | |
###—————————————————————————————————————————————— | |
on createHTMLLink(pstrLinkText, pstrURL) | |
set strHTMLLink to "<a href=\"" & pstrURL & "\" style=\"" & gStyleLink & "\">" & pstrLinkText & "<a>" | |
-- MUST include a space character just before the </span> in order to keep font styles -- | |
set strHTMLLink to "<span style=\"" & gstrFont & gstrLinkFontSize & "\">" & strHTMLLink & " </span>" | |
return strHTMLLink | |
end createHTMLLink | |
###—————————————————————————————————————————————— | |
# COPY HTML TO CLIPBOARD AS RTF: copyHTMLasRTFtoClipboard | |
###—————————————————————————————————————————————— | |
on copyHTMLasRTFtoClipboard(pstrHTML) | |
if bolDebug then display dialog "ENTER copyHTMLasRTFtoClipboard" | |
-- REWRITTEN AS RTF AND COPIED TO THE CLIPBOARD | |
set lstrCMD to "echo " & quoted form of pstrHTML & " | textutil -format html -convert rtf -stdin -stdout | pbcopy -Prefer rtf" | |
do shell script lstrCMD | |
if bolDebug then | |
display notification pstrHTML with title "Copy RTF to Clipboard" | |
end if | |
end copyHTMLasRTFtoClipboard | |
###—————————————————————————————————————————————— | |
# GET URL FROM USER: getURLfromUser | |
###—————————————————————————————————————————————— | |
on getURLfromUser(pTextForLink) | |
set strMsg to ¬ | |
"Enter Your URL (can include custom URL Schemes) for:" & " | |
" & pTextForLink & " | |
" & "(Must have format of 'protocol://')" | |
set strAnswer to "" | |
set bolValidURL to false | |
set Title to "Enter Your URL" | |
repeat while (bolValidURL = false) | |
beep | |
set recResult to display dialog strMsg default answer strAnswer with title Title with icon caution | |
set strButton to button returned of recResult | |
set strAnswer to text returned of recResult | |
if bolDebug then | |
log strButton | |
log strAnswer | |
end if | |
set bolValidURL to (strAnswer contains "://") | |
end repeat -- while (bolValidURL = false) | |
return strAnswer | |
end getURLfromUser |
NOTE: Due to changes Evernote has made to EN Mac, as of Ver 6.11.1, this script no longer works. When the hyperlink is pasted into EN Mac, the app removes the link.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
NOTE: This is a script I wrote in Nov 2015 that I am just now publishing in my Gist. Since then I have changed my style and standards, so this script may be somewhat different. It still runs fine.