JMichaelTX
7/5/2016 - 11:29 PM

AppleScript to Append HTML to Existing Evernote Note, or Create New Note (EN Mac)

AppleScript to Append HTML to Existing Evernote Note, or Create New Note (EN Mac)

(*
===============================================================================
  Append to Existing Note OR Create New Note
===============================================================================

VER:   1.2    LAST UPDATE:   2016-07-05

PURPOSE:
  • Search for Existing Note by Title and Notebook
  • IF found, append HTML
  • ELSE Create New Note

AUTHOR:    JMichaelTX
          Find any bugs/issues or have suggestions for improvement?
          Contact me via PM or at blog.jmichaeltx.com/contact/

REQUIRED:
  1.  Mac OS X Yosemite 10.10.5+
  2.  Mac Applications
      • EN Mac 6.7.1+
        

INSTALLATION:   See http://tinyurl.com/install-as

REF:  The following were used in some way in the writing of this script.
  1.  Respond to topic in Evernote forum:
    https://discussion.evernote.com/topic/97499-check-if-a-note-exists-applescript/
    
CHANGE LOG:
  • 1.2    2016-07-05
          • ADD setup variables for delimiters
          • ADD info to Multi-Note Choice Dialog
          
  • 1.1    2016-07-05
          • ADD User Prompt to Confirm/Enter Note Title and Notebook
          • Open selected/new note in its own window
          
  • 1.0    2016-07-05
          • Initial release
    
DISCLAIMER:
  • This script is provided for EDUCATIONAL PURPOSES ONLY
  • It has had very limited testing, and may contain errors & bugs
  • The user of this script assumes all risk and liability
  • This script does write to your Evernote account, so it is best to
    first test using a Test Account
  • The script is provided "as is", without warranty of any kind, express or
    implied, including but not limited to the warranties of merchantability,
    fitness for a particular purpose and noninfringement. In no event shall the
    authors or copyright holders be liable for any claim, damages or other
    liability, whether in an action of contract, tort or otherwise, arising from,
    out of or in connection with the software or the use or other dealings in the
    script.
===============================================================================
   
*)
use AppleScript version "2.4" -- Yosemite (10.10) or later
use scripting additions

--~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
--  SCRIPT SETUP --
--~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
-- Change the below variables to fit your use case

set noteTitleToFind to "TEST Note"
set nbName to "TestExtractText"
set tagList to {"TEST"}
set noteHTMLAppend to "<div>Append to Found Note</div>"
set noteContentStr to "New Note was created."
set startDelim to "|["
set endDelim to "]|"
--~~~~~~~ END OF SETUP ~~~~~~~~~~~~~~~~~~~~~

set ansRec to display dialog ¬
  "ENTER Note Title to Search For:" default answer noteTitleToFind ¬
  with title (name of me) ¬
  with icon caution

set noteTitleToFind to text returned of ansRec

set ansRec to display dialog ¬
  "ENTER Notebook Search For:" default answer nbName ¬
  with title (name of me) ¬
  with icon caution

set nbName to text returned of ansRec


tell application "Evernote"
  
  --- GET ALL NOTES THAT MATCH TITLE AND NOTEBOOK ---
  
  set noteList to find notes "notebook:\"" & nbName & "\" intitle:\"" & noteTitleToFind & "\""
  set numNotes to count of noteList
  
  if (numNotes = 1) then -- ONE NOTE FOUND --
    set oNoteSel to item 1 of noteList
    
  else if (numNotes = 0) then -- NO NOTES FOUND ---
    display dialog "Note NOT Found with Title:" & return & noteTitleToFind
    set oNoteSel to ""
    
  else --- MULTIPLE NOTES FOUND ---
    
    set pickList to {}
    set iNote to 0
    
    --- BULID PICK LIST OF MATCHING NOTES ---
    
    repeat with oNote in noteList
      set iNote to iNote + 1
      set modDate to (modification date of oNote)
      set modDateStr to short date string of modDate
      set end of pickList to (startDelim & iNote & endDelim & "  " & modDateStr & " | " & (title of oNote))
    end repeat
    
    --- ASK USER TO PICK ONE NOTE ---
    
    set noteSelected to choose from list pickList ¬
      with title (name of me) ¬
      with prompt ¬
      "Multiple Notes found for Title:" & return & noteTitleToFind ¬
      & return & return & "Select the Note to Use in Notebook: " & nbName ¬
      & return & "(Press OK without selection for new note)" OK button name ¬
      "OK" cancel button name ¬
      "Cancel" multiple selections allowed false ¬
      with empty selection allowed
    
    if noteSelected is false then error number -128 -- user cancelled
    
    if noteSelected ≠ {} then
      set iNote to my extractIndex(noteSelected, startDelim, endDelim)
      set oNoteSel to item iNote of noteList
    else
      set oNoteSel to ""
    end if
  end if
  
  --- PROCESS NOTE ---
  
  if oNoteSel ≠ "" then --- APPEND TO EXISTING NOTE ---
    
    set noteTitle to title of oNoteSel
    append oNoteSel html noteHTMLAppend
    display dialog "HTML appended to Note: " & return & noteTitle
    
  else --- CREATE NEW NOTE ---
    
    set oNoteSel to create note with text noteContentStr title noteTitleToFind notebook nbName tags tagList
    display dialog "NEW Note Created: " & return & noteTitleToFind
  end if
  
  open note window with oNoteSel
  
end tell

--~~~~~~~~~~~~~~~~~~ END OF MAIN SCRIPT ~~~~~~~~~~~~~~~~~

--~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
on extractIndex(pList, pStartDelim, pEndDelim)
  --–––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––
  (*  VER: 1.0    2016-07-05
    PURPOSE:  Extract Index number embedded in Text of List Item
    PARAMETERS:
      • pList    : List to use
      • pStartDelim  : Start Delimiter, like "[{"
      • pEndDelim    : End Delimiter, like "}]"
      
    RETURNS:  Number found between delimiters
    
    AUTHOR:  JMichaelTX
  *)
  
  --~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  
  local itemSel, indexNum
  
  set itemSel to item 1 of pList as text
  
  set indexNum to (text ((offset of pStartDelim in itemSel) ¬
    + (length of pStartDelim)) thru ¬
    ((offset of pEndDelim in itemSel) - 1) of itemSel) as integer
  
  return indexNum
  
end extractIndex
###END~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~