Evernote - Copy EN Mac Note Link (Classic)

EDIT: UPDATED Mon, Jan 11, 2016 at 6:57 PM

SCRIPT NAME: Copy EN Note Link (Classic)

DATE: Tue, Dec 15, 2015 VER: 2.1

PURPOSE:
¢ Get Classic link of selected Evernote Note
¢ Copy to Mac Clipboard as RTF (Rich Text Format) so that you can
paste to most apps as a click-able link
¢ Text is EN Note Title
¢ Link is the “Classic”, internal link to EN Mac Note

EN Mac Versions: 5.5.2, 6.0.1+
Mac OS X: Mavericks & Yosemite

AUTHOR: JMichael (member of Discussion.Evernote.com forum)
Please PM me with any bugs/issues/questions


(*
	SCRIPT NAME:	Copy EN Note Link (Classic)
	
	DATE:	Tue, Dec 15, 2015		VER: 2.1
	
	PURPOSE:
		¢  Get Classic link of selected Evernote Note
		¢  Copy to Mac Clipboard as RTF (Rich Text Format) so that you can
			paste to most apps as a click-able link
		¢  Text is EN Note Title
		¢  Link is the "Classic", internal link to EN Mac Note
		
	AUTHOR:		JMichaelTX (member of Discussion.Evernote.com forum)
					Please PM me with any bugs/issues/questions
					
	REQUIRES:
		¢ Satimage.osax for HTML encode (encode entities)
				
	INSTALLATION:
		¢  There are several ways you can install and access this script
		¢  SIMPLE METHOD
			¢  Copy to your personal Scripts folder:  /Users/<YourName>/Library/Scripts/
				(create the Scripts folder if it does not exist)
			¢  Select a Note in Evernote
			¢  Click on the Apple Scripts menu in the upper right area of your menu
			¢  Click on EN Note Link (classic)
			¢  Switch to your target app, and click in the text area where you want the link, and press CMD-V
		¢  OTHER OPTIONS
			¢  FastScripts -- http://www.red-sweater.com/fastscripts/
			¢  Automator
			¢  See article on Veritrope.com for details:  http://veritrope.com/tech/the-basics-using-keyboard-shortcuts-with-applescripts/
	
	REF:
		¢ I was inspired by:
			
			¢ This thread on Discussion.Evernote.com forum:
				https://discussion.evernote.com/topic/60623-how-can-make-the-classic-link-the-default-way-of-linking/
				
				¢ Post by patnpm
					https://discussion.evernote.com/topic/60623-how-can-make-the-classic-link-the-default-way-of-linking/#entry283106
				¢ Post by DanielB
					https://discussion.evernote.com/topic/60623-how-can-make-the-classic-link-the-default-way-of-linking/#entry291008
					
		¢ I adapted the code posted by:
		
			¢ alastor933 in the MacScripter.com forum
			¢ http://macscripter.net/viewtopic.php?pid=148647#p148647
======================================================================
*)

property bolDebug : false -- set to true to turn on diagnostic logs and dialogs.

-- SET THE BELOW PROPERTIES TO REFLECT THE LINK STYLE YOU WANT ---

property gstrFont : "font-family:verdana,geneva,sans-serif;"
property gstrLinkFontSize : "font-size:14px;"
property gStyleLink : "color:blue"


tell application "Evernote"
	set lstSelectedNotes to selection
	
	if lstSelectedNotes ≠ {} then
		
		--- GET THE FIRST NOTE ---
		set oNote to first item of lstSelectedNotes
		
		--- GET THE NOTE TITLE AND CLASSIC NOTE LINK ---
		set strNoteTitle to title of oNote
		set strNoteLink to note link of oNote
		
		log strNoteTitle
		set strNewTitle to ""
		
		--- REPLACE EXTENDED ASCII CHARS BETWEEN 127-253 WITH SPACE --
		
		repeat with iChar from 1 to (length of strNoteTitle)
			set strChar to character iChar of strNoteTitle
			--set strLog to ("[" & iChar & "] " & (ASCII number of strChar) & ": " & strChar)
			--log strLog
			
			if ((ASCII number of strChar) > 126) and ((ASCII number of strChar) < 254) then
				set strNewTitle to strNewTitle & " "
			else
				set strNewTitle to strNewTitle & strChar
			end if
		end repeat
		
		##  --- ENCODE THE NOTE TITLE ---  ##	
		--			(requires the Satimage.osax)
		
		set strNoteTitle to encode entities strNewTitle
		log strNoteTitle
		
		--set strNoteTitle to my encode_text(strNoteTitle, false, false)
		
		--- CREATE THE HTML ANCHOR CODE ---
		set strHTMLLink to my createHTMLLink(strNoteTitle, strNoteLink)
		set strHTMLLink to "<span style=\"" & gstrFont & gstrLinkFontSize & "\">" & strHTMLLink & "</span>"
		
		--- PUT THE HTML CODE ON THE CLIPBOARD AS RICH TEXT (RTF) ---
		my copyHTMLasRTFtoClipboard(strHTMLLink)
		
		set strMsg to strNewTitle
		set strMTitle to "Evernote Internal Link Copied to Clipboard for"
		display notification strMsg with title strMTitle sound name "Hero.aiff"
		
	end if -- lstSelectedNotes ≠ {}
	
end tell

--=====================================
--	SUBPROGRAMS
--=====================================

###””””””””””””””””””””””””””””””””””””””””””””””
#			Create HTML Link:		createHTMLLink
###””””””””””””””””””””””””””””””””””””””””””””””

on createHTMLLink(pstrLinkText, pstrURL)
	
	return "<a href=\"" & pstrURL & "\" style=\"" & gStyleLink & "\">" & pstrLinkText & "<a>"
	
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


---	A sub-routine for encoding high-ASCII characters:


on encode_char(this_char)
	set the ASCII_num to (the ASCII number this_char)
	set the hex_list to {"0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "A", "B", "C", "D", "E", "F"}
	set x to item ((ASCII_num div 16) + 1) of the hex_list
	set y to item ((ASCII_num mod 16) + 1) of the hex_list
	return ("%" & x & y) as string
end encode_char

---A sub-routine for percent encoding strings:


-- this sub-routine is used to encode text 
on encode_text(this_text, encode_URL_A, encode_URL_B)
	set the standard_characters to "abcdefghijklmnopqrstuvwxyz0123456789"
	set the URL_A_chars to "$+!'/?;&@=#%><{}[]\"~`^\\|*"
	set the URL_B_chars to ".-_:"
	set the acceptable_characters to the standard_characters
	if encode_URL_A is false then set the acceptable_characters to the acceptable_characters & the URL_A_chars
	if encode_URL_B is false then set the acceptable_characters to the acceptable_characters & the URL_B_chars
	set the encoded_text to ""
	repeat with this_char in this_text
		if this_char is in the acceptable_characters then
			set the encoded_text to (the encoded_text & this_char)
		else
			set the encoded_text to (the encoded_text & encode_char(this_char)) as string
		end if
	end repeat
	return the encoded_text
end encode_text