エンジニアのソフトウェア的愛情

または私は如何にして心配するのを止めてプログラムを・愛する・ようになったか

CotEditorのテキストからEvernoteのノートを作る

こちらの記事でEvernote for MacEvernoteMac OS X用のクライアントソフト)はAppleScriptで操作できるということを知りました。


おやくそくのように。CotEditorからEvernoteへ、に挑戦。

情報収集

まず、AppleScriptそのものについてはこちら。


CotEditorについてはこちら。

また、CotEditorをインストールすると下記の場所にAppleScriptについてのドキュメントが格納されます。

/Applications/CotEditor_1.0.1/Script/AppleScript/AboutAppleScript-jp.rtf


Evernoteについてはこちら。

ノートを作る方法として4種類紹介されています。引用。

tell application "Evernote"
    (* 中略 *)

    -- 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/about/media/img/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

    (* 中略 *)
end tell

実装

ここまでくると、あとはたいしたことなくて。10行で終了。
テキストが選択状態なら選択されたテキストから、そうでなければテキスト全体からノートを作成します。1行目がノートのタイトルになります。
AppleScriptを書いたことはほとんどないのでいろいろ間違ってる可能性はありますが、一応動いています。

tell application "CotEditor"
    set note_text to contents of selection of front document
    if note_text is "" then
        set note_text to contents of front document
    end if
end tell
tell application "Evernote"
    create note title paragraph 1 of note_text with text note_text
end tell

CotEditorは自作したスクリプトを登録することができます。登録の仕方は以前のエントリを参考にしてみてください。