Cocoa Emacs => Evernote

簡単にメモを取るために考えてみました.

;; http://tezfm.blogspot.com/2009/09/do-applescript.html
(defun applescript-string-literal (str)
  "do-applescriptに渡す文字列リテラルを作成"
  (replace-regexp-in-string ;; convert " => \"
   "\\\"" "\\\\\""
   (replace-regexp-in-string ;; convert \ => \\
    "\\\\" "\\\\\\\\"
    str)))

(defun en-mac-create-note* (&optional notebook note tags)
  (let ((notebook
         (or notebook
             (read-string "Notebook: " "Emacs" nil "Untitled Notebook")))
        (note
         (or note (read-string "Note: " nil nil "Untitled Note")))
        ;; tag or list of tags
        (tags
         (cond
          ((null tags)
           (replace-regexp-in-string
            "[[:blank:]]" "\",\"" (read-string "Tags: ")))
          ((listp tags)
           (mapconcat 'identity tags "\",\""))
          (t
           tags)))
        (text (applescript-string-literal
               (if (and mark-active
                        (<= (region-beginning) (point))
                        (<= (point) (region-end)))
                   (buffer-substring (region-beginning)
                                     (region-end))
                 (buffer-string)))))

    (en-mac-do-applescript
     ;; AppleScript
     (format
      "tell application \"Evernote\"
       set theNotebook to \"%s\"
       if (not (notebook named theNotebook exists)) then
           make notebook with properties {name:theNotebook}
       end if
       set theNotebook to notebook theNotebook
       set theNote to create note title \"%s\" with text \"%s\" notebook theNotebook
       set theTags to {\"%s\"}
           repeat with x in theTags
               if (not (tag named x exists)) then
                   make tag with properties {name:x}
               end if
               set theTag to tag x
               assign theTag to theNote
           end repeat
       end tell"
      notebook note text tags)
     (format "Evernote: Added `%s' into `%s'" note notebook))
    ))

(defun en-mac-do-applescript (script msg)
  (or (condition-case err
          (do-applescript script)
        (error err))
      ;; growl.el + growlnotify
      ;; http://github.com/elim/emacs-growl/raw/master/growl.el
      (if (fboundp 'growl)
          (growl msg)
        (message msg))))

(defun en-mac-create-note ()
  "Clip text to the Evernote on your Mac."
  (interactive)
  (en-mac-create-note*
   "Emacs"
   (format "%s %s @%s"
           (buffer-name)
           (format-time-string "%Y/%m/%d %H:%M:%S")
           (system-name))
   (symbol-name major-mode))
  )

(global-set-key (kbd "C-c C-e") 'en-mac-create-note)

注意点:

  • Emacsのバージョンや文字コードによってはバックスラッシュとダブルクォートが上手く渡りません.Emacs 23.2, utf8ではうまくいきます.
  • ターミナル上のEmacsや,shell-modeなどでは動きません.
  • 上記コードのAppleScriptでtextをhtmlと書き換えると,htmlのソースを変換してクリップできます.

参考:

追記(2010/11/17):
さっきこんなのみつけました.
emacs-evernote-mode - Project Hosting on Google Code
サーバー上のEvernoteにアクセスしてノートを編集したりできるようです.このエントリーで紹介したelispはローカルのEvernoteクライアントにポストするだけなので,既存のノートをゴリゴリ編集したいような場合にはemacs-evernote-mode使ったほうがいいと思います.