JMichaelTX
1/24/2016 - 12:54 AM

[EN] Classic - Evernote Mac - Put Classic Note Link on Clipboard as Rich Text using Note Title -- AppleScript

[EN] Classic - Evernote Mac - Put Classic Note Link on Clipboard as Rich Text using Note Title -- AppleScript

(*
  SCRIPT NAME:  Copy EN Note Link (Classic)
  
  DATE:  Sat, Jan 23, 2016    VER: 2.1.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)
      (Free D/L & info at http://tinyurl.com/Satimage-Osax-DL )
        
  INSTALLATION:
    • See http://tinyurl.com/install-as
  
  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
    
    --- 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