Jump to content

Recommended Posts

Posted (edited)

This a function for adding new notes to your Evernote database.

Example usage:

#include "CreateNote.au3"

_CreateNote("Test note text", "", "tag one, tag two, tag three", "This is a test note title")

_CreateNote("", @ScriptDir & "\note.txt", "", "This is a test note composed of a file")
If @error Then ConsoleWrite("_CreateNote failed with @error " & @error & @CRLF)

The function (save it as CreateNote.au3):

#include-once
#include <AutoItConstants.au3>
#include <StringConstants.au3>

; #FUNCTION# ====================================================================================================================
; Name ..........: _CreateNote
; Description ...: Adds a note to Evernote for Windows through ENScript.exe.
; Syntax ........: _CreateNote([$sText = ""[, $sFile = ""[, $sTags = ""[, $sTitle = ""[, $sNotebook = ""[, $sDate = ""[, $sENScript = ""]]]]]]])
; Parameters ....: $sText     - Plain text for the note body.
;                  $sFile     - Path of a plain text file to use as the note body. Pass $sText or $sFile, not both.
;                  $sTags     - Comma separated tag names. Tags that don't exist yet are created.
;                  $sTitle    - Note title.
;                  $sNotebook - Notebook name. A notebook that doesn't exist yet is created.
;                  $sDate     - Creation date as "YYYY/MM/DD hh:mm:ss". Leave empty for the current time.
;                  $sENScript - Full path of ENScript.exe. Leave empty to use the path Evernote registers under App Paths.
; Return values .: Success - 1
;                  Failure - 0 and sets @error:
;                  |1 - Neither $sText nor $sFile was given
;                  |2 - Both $sText and $sFile were given
;                  |3 - $sFile doesn't exist
;                  |4 - $sDate isn't in "YYYY/MM/DD hh:mm:ss" format
;                  |5 - ENScript.exe wasn't found
;                  |6 - ENScript.exe couldn't be started
;                  |7 - ENScript.exe reported a failure. @extended holds its exit code.
; Remarks .......: Needs ENScript.exe, which Evernote for Windows 3.0 and later installs. Evernote now documents it as part of
;                  its deprecated legacy API, so newer clients may not include it.
; Link ..........: https://dev.evernote.com/doc/articles/enscript.php
; Example .......: _CreateNote("Buy milk", "", "errands, home", "Shopping")
; ===============================================================================================================================
Func _CreateNote($sText = "", $sFile = "", $sTags = "", $sTitle = "", $sNotebook = "", $sDate = "", $sENScript = "")
    Local $iError = __CreateNote_Validate($sText, $sFile, $sDate)
    If $iError Then Return SetError($iError, 0, 0)
    If $sENScript = "" Then $sENScript = __CreateNote_FindENScript()
    If $sENScript = "" Then Return SetError(5, 0, 0)

    Local $sCommand = __CreateNote_Quote($sENScript) & " createNote" & __CreateNote_Args($sFile, $sTags, $sTitle, $sNotebook, $sDate)
    Local $iPID = Run($sCommand, "", @SW_HIDE, $STDIN_CHILD)
    If @error Then Return SetError(6, 0, 0)
    ; ENScript reads the note body from stdin when no /s file is given.
    StdinWrite($iPID, $sText)
    StdinWrite($iPID)
    ProcessWaitClose($iPID)
    Local $iExitCode = @extended
    If $iExitCode <> 0 Then Return SetError(7, $iExitCode, 0)
    Return 1
EndFunc   ;==>_CreateNote

; Returns the _CreateNote @error code for invalid arguments, or 0 when they are usable.
Func __CreateNote_Validate($sText, $sFile, $sDate)
    If $sText = "" And $sFile = "" Then Return 1
    If $sText <> "" And $sFile <> "" Then Return 2
    If $sFile <> "" And Not FileExists($sFile) Then Return 3
    If $sDate <> "" And Not StringRegExp($sDate, "^\d{4}/\d\d/\d\d \d\d:\d\d:\d\d$") Then Return 4
    Return 0
EndFunc   ;==>__CreateNote_Validate

; Evernote 4.0 and later registers ENScript.exe under App Paths. HKLM reads from a 32-bit script on 64-bit Windows can
; be redirected to WOW6432Node, so the 64-bit view is checked as well.
Func __CreateNote_FindENScript()
    Local Const $sAppPath = "\SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\ENScript.exe"
    Local $sPath = RegRead("HKLM" & $sAppPath, "")
    If @error Then $sPath = RegRead("HKLM64" & $sAppPath, "")
    Return StringReplace($sPath, '"', "")
EndFunc   ;==>__CreateNote_FindENScript

Func __CreateNote_Args($sFile, $sTags, $sTitle, $sNotebook, $sDate)
    Local $sArgs = ""
    If $sFile <> "" Then $sArgs &= " /s " & __CreateNote_Quote($sFile)
    If $sTitle <> "" Then $sArgs &= " /i " & __CreateNote_Quote($sTitle)
    If $sNotebook <> "" Then $sArgs &= " /n " & __CreateNote_Quote($sNotebook)
    If $sDate <> "" Then $sArgs &= " /c " & __CreateNote_Quote($sDate)
    Local $sTrimmed
    For $sTag In StringSplit($sTags, ",", $STR_NOCOUNT)
        $sTrimmed = StringStripWS($sTag, $STR_STRIPLEADING + $STR_STRIPTRAILING)
        If $sTrimmed <> "" Then $sArgs &= " /t " & __CreateNote_Quote($sTrimmed)
    Next
    Return $sArgs
EndFunc   ;==>__CreateNote_Args

; Quotes one command line argument so the Microsoft C runtime parses it back unchanged, including any
; double quotes and trailing backslashes it contains.
Func __CreateNote_Quote($sArg)
    Local $sEscaped = StringRegExpReplace($sArg, '(\\*)"', '$1$1\\"')
    $sEscaped = StringRegExpReplace($sEscaped, '(\\+)$', '$1$1')
    Return '"' & $sEscaped & '"'
EndFunc   ;==>__CreateNote_Quote

Edit, October 2026: the code in this post had become garbled, so I rewrote it. It needs ENScript.exe from Evernote for Windows. Evernote lists ENScript under its deprecated legacy API, so current versions may not include it.

Edited by therms
Rewrote the garbled code

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...