Skip to content

Instantly share code, notes, and snippets.

@evernotegists
Created April 4, 2013 20:06
Show Gist options
  • Select an option

  • Save evernotegists/5313743 to your computer and use it in GitHub Desktop.

Select an option

Save evernotegists/5313743 to your computer and use it in GitHub Desktop.

Revisions

  1. evernotegists created this gist Apr 4, 2013.
    189 changes: 189 additions & 0 deletions gistfile1.applescript
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,189 @@
    -- This script includes examples for using AppleScript to perform several

    -- useful tasks with Evernote.

    -- Each section illustrates a chunk of Evernote's AppleScript interface,

    -- and each section also cleans up after itself.

    -- Please refer to the Evernote application's scripting dictionary for

    -- detailed information and BE CAREFUL: operations that would normally

    -- confirmation from the user (such as deleting notes, etc) are

    -- completed without warning when invoked from AppleScript!

    tell application "Evernote"
    -- create, rename, and delete notebooks
    if (not (notebook named "AppleScriptNotebook1" exists)) then
    -- NOTE also check out the "create notebook" command
    make notebook with properties {name:"AppleScriptNotebook1"}
    end if
    set name of notebook "AppleScriptNotebook1" to "AppleScriptNotebook2"
    -- WARNING there is no confirmation!
    delete notebook "AppleScriptNotebook2"

    ------------------------------------
    -- create, rename, and delete tags
    if (not (tag named "AppleScriptTag1" exists)) then
    make tag with properties {name:"AppleScriptTag1"}
    -- create a sub-tag
    make tag with properties {name:"AppleScriptTag2", parent:tag named "AppleScriptTag1"}
    end if

    set name of tag "AppleScriptTag1" to "AppleScriptTag3"
    -- WARNING there is no confirmation, and all subtags are deleted as well!
    delete tag "AppleScriptTag3"

    ------------------------------------
    -- create notes, four ways
    -- 1: with plain text
    set notebook1 to create notebook "AppleScriptNotebook1"
    create note title "Note 1" with text "Here is my new text note" notebook notebook1
    -- 2: with html
    create note title "Note 2" with html "<strong>Here is my new HTML note</strong>" notebook notebook1
    -- 3: with the data from a URL
    create note title "Note 3" from url "http://www.evernote.com/media/images/logo.png" notebook notebook1
    -- 4: with the data from a file
    create note title "Note 4" from file "/path/to/a/file.txt" notebook notebook1

    -- cleanup
    delete notebook1

    ------------------------------------
    -- move note between notebooks
    create notebook "AppleScriptNotebook1"
    create notebook "AppleScriptNotebook2"
    create note title "Note 1" with text "Moving note" notebook "AppleScriptNotebook1"
    move note 1 of notebook "AppleScriptNotebook1" to notebook "AppleScriptNotebook2"

    -- cleanup
    delete notebook "AppleScriptNotebook1"
    delete notebook "AppleScriptNotebook2"

    ------------------------------------
    -- delete individual notes
    create notebook "AppleScriptNotebook1"
    create note title "Note 1" with text "Delete this note!" notebook "AppleScriptNotebook1"
    delete note 1 of notebook "AppleScriptNotebook1"

    -- cleanup
    delete notebook "AppleScriptNotebook1"

    ------------------------------------
    -- assign, unnasign tags
    create notebook "AppleScriptNotebook1"
    set note1 to create note title "Note 1" with text "Note 1" notebook "AppleScriptNotebook1"
    set note2 to create note title "Note 2" with text "Note 2" notebook "AppleScriptNotebook1"
    set note3 to create note title "Note 3" with text "Note 3" notebook "AppleScriptNotebook1"

    set tag1 to make tag with properties {name:"AppleScriptTag1"}
    set tag2 to make tag with properties {name:"AppleScriptTag2", parent:tag named "AppleScriptTag1"}
    set tag3 to make tag with properties {name:"AppleScriptTag3", parent:tag named "AppleScriptTag1"}

    assign tag1 to note1
    assign {tag2, tag3} to {note2, note3}

    unassign tag1 from note1
    unassign {tag2, tag3} from {note2, note3}

    -- cleanup
    delete notebook "AppleScriptNotebook1"
    delete tag "AppleScriptTag1"

    ------------------------------------
    -- open a note in its own window
    set notebook1 to create notebook "AppleScriptNotebook1"
    set note1 to create note with text "Note 1" notebook notebook1
    open note window with note1

    -- cleanup
    delay (3) -- wait to be sure new window has opened
    close window 1
    delete notebook1

    ------------------------------------
    -- change the query used in a note collection window
    -- (assumes the default note collection window is open and is window 1)
    set notebook1 to create notebook "AppleScriptNotebook1"
    set note1 to create note with text "ONLY ME!" notebook notebook1
    set query string of window 1 to "notebook:AppleScriptNotebook1"

    -- cleanup
    delete notebook1

    ------------------------------------
    -- open a new collection window with a specific query
    set notebook1 to create notebook "AppleScriptNotebook1"
    set note1 to create note with text "ONLY ME!" notebook notebook1
    open collection window with query string "notebook:AppleScriptNotebook1"

    -- cleanup
    delay (3) -- wait to be sure new window has opened
    close window 1
    delete notebook1

    ------------------------------------
    -- append data to a note
    set notebook1 to create notebook "AppleScriptNotebook1"
    set note1 to create note with text "foo" notebook notebook1

    tell note1 to append html "<strong>bar</strong>"
    tell note1 to append text "baz"

    -- cleanup
    delete notebook1

    ------------------------------------
    -- execute a query and manipulate every note in the result
    -- for details on the query syntax, see our Search Grammar overview
    -- at http://dev.evernote.com/documentation/cloud/chapters/search_grammar.php
    set notebook1 to create notebook "AppleScriptNotebook1"
    set note1 to create note with text "An apple is a fruit" notebook notebook1
    set note2 to create note with text "An Apple is a computer" notebook notebook1
    set note3 to create note with text "An orange is a fruit" notebook notebook1
    set note4 to create note with text "An Amiga is a computer" notebook notebook1

    set tag1 to make tag with properties {name:"AppleScriptTag1"}

    set matches to find notes "notebook:AppleScriptNotebook1 apple"

    assign tag1 to matches

    -- cleanup
    delete tag1
    delete notebook1

    ------------------------------------
    -- do something with the application's selected notes
    set notebook1 to create notebook "AppleScriptNotebook1"
    set note1 to create note with text "Note 1" notebook notebook1
    set note2 to create note with text "Note 2" notebook notebook1
    open collection window with query string "notebook:AppleScriptNotebook1"

    set tag1 to make tag with properties {name:"AppleScriptTag1"}

    delay (3) -- be sure the new window has opened

    -- normally, a script would be activated to take action when the user had
    -- made a selection, rather than this artificial demonstration

    set noteList to selection

    assign tag1 to noteList

    -- note: the application's 'selection' property returns the same result as
    -- the selection of window 1, if any

    -- cleanup
    close window 1
    delete tag1
    delete notebook1

    ------------------------------------
    -- please refer to the Evernote application's scripting dictionary
    -- for complete details on the Evernote AppleScript interface

    end tell