Evernote — Create Note and Get Note Link

Description

Michael Schechter recently asked if I knew of a way to get Note Links in Evernote for newly-created Notes. Seemed like it should be easy to do with AppleScript, given that Evernote created a "note link" definition for people like Michael to play with but, when you'd try to use it on a newly created item, AppleScript would return a very frustrating missing value where the link should be. After a little trial and error, I found that using some special types of repeat loops seemed to do the trick. The code below is a sample to give interested scripters an idea of how to use it in their own projects. The basic concept is this: Our script checks with Evernote to see if the app is currently synching its data with (using the isSynchronizing action) and, if so, pauses until it's finished. It then creates our new note and tells Evernote to synchronize. This begins the process of refreshing the note's information and making the link available to AppleScript. While this is happening, AppleScript keeps checking with Evernote to see if the link is available yet. Once it is, it writes it to a variable.

The Code

(*
◸ Veritrope.com
Get Note Links in Evernote
VERSION 1.0
January 24, 2013

// UPDATE NOTICES
     ** Follow @Veritrope on Twitter, Facebook, Google Plus, and ADN for Update Notices! **

// SUPPORT VERITROPE!
     If this AppleScript was useful to you, please take a second to show your love here:
    http://veritrope.com/support

// SCRIPT INFORMATION AND UPDATE PAGE
     http://veritrope.com/code/get-note-links-in-evernote-for-newly-created-notes/

// REQUIREMENTS
     More details on the script information page.

// CHANGELOG
    1.0 INITIAL RELEASE

// TERMS OF USE:
     This work is licensed under the Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License.

     To view a copy of this license, visit http://creativecommons.org/licenses/by-nc-sa/3.0/ or send a letter to Creative Commons, 444 Castro Street, Suite 900, Mountain View, California, 94041, USA.


*)


(*
======================================
// MAIN PROGRAM
======================================
*)


--RESET VALUES
set myNote to {}
set noteLink to missing value

tell application "Evernote"
   
    --TEST TO SEE IF EVERNOTE IS CURRENTLY SYNCHRONIZING...
    repeat until isSynchronizing is false
        --THIS EMPTY LOOP WILL PAUSE SCRIPT UNTIL PREVIOUS SYNC IS FINISHED
    end repeat
   
    --CREATE THE NOTE
    set myNote to create note with text "HI!"
   
    --SYNCHRONIZE WITH EVERNOTE'S SERVERS
    synchronize
   
    --PAUSE UNTIL THERE IS A VALUE FOR NOTE LINK
    repeat while noteLink is missing value
        --GET THE NOTE LINK FOR THE CURRENT NOTE
        set noteLink to (note link of myNote)
    end repeat
   
end tell