citation - Evernoteに実装するアンビリバボーな機能 #2


アンビリーバボ？アンビリバボー？

[http://rashita.net/blog/?p=16722](http://rashita.net/blog/?p=16722)

こちらの2つ目、「拡張型ノートリンク」と書かれた機能をAppleScriptで実装しました。


## 使い方（基礎編）

Evernoteのノート上で引用したい箇所を選択状態に。

![citation screenshot 1](https://qiita-image-store.s3.amazonaws.com/0/101438/afdff6c3-40c0-47a4-eb72-10e89f7efc0e.png "スクリーンショット 2015-12-17 20.19.40.png")

スクリプトメニューから`citation`を実行すると

![citation screenshot 2](https://qiita-image-store.s3.amazonaws.com/0/101438/a4ff9c39-9578-71fb-76b6-efd3e04975e1.png "スクリーンショット 2015-12-17 20.21.02.png")

「Copied」と通知。

![citation screenshot 3](https://qiita-image-store.s3.amazonaws.com/0/101438/53b0c1d2-94f4-ea11-9d55-a8df77836842.png "スクリーンショット 2015-12-17 20.21.42.png")

好きなところでペーストすれば、それっぽい見た目のノートリンク。

![citation screenshot 4](https://qiita-image-store.s3.amazonaws.com/0/101438/6669d35b-5eec-f260-383b-67a703297066.png "スクリーンショット 2015-12-17 20.23.48.png")

## 使い方（応用編）

スクリプトオブジェクトを使って他のアプリケーションにも拡張できます。

例えばSafariの場合。ページ上で引用部分を選択して`citation`を実行。

![citation screenshot 5](https://qiita-image-store.s3.amazonaws.com/0/101438/f3e669e5-becb-e0b3-19f7-eff9b2121561.png "スクリーンショット 2015-12-17 20.27.38.png")

Evernoteにペーストで引用文を含んだリンクに。画像は反映されません。

![citation screenshot 6](https://qiita-image-store.s3.amazonaws.com/0/101438/8e1dd729-59a3-fe59-e420-6c7ab84332cd.png "スクリーンショット 2015-12-17 20.28.18.png")

今のところEvernoteとSafariの2種類のみです。ソースコードをいじって他のアプリケーションを追加してみてください。

リンクの色を変えるなり、アイコンを付けるなりしないと見分けがつきませんが、それはまた別の機会に。


## (2015-12-18 追記) 使い方（発展編）

HTMLを理解できる方にオススメ。

ソースコード冒頭にある`property citationTemplate : ...`の部分で、貼り付けるリンクの見た目を決めています。

このテンプレート、見ればわかるようにHTMLで記述されているのです。

* 元のままでは無駄な情報が多い
* 文字やリンクの大きさや色、順番が気にくわない
* 引用は`blockquote`じゃないと気持ち悪い

なんて方はHTMLをいじって自分好みの見た目に変更しましょう。

その際に注意すべきこととして **特殊文字の取り扱い** があります。

`property citationTemplate : ...`の部分ではHTML全体を文字列として`citationTemplate`という変数に入れるため、ダブルクオーテーション(")で囲っています。

HTML内でダブルクオーテーション(")を使うと、そこが文字列の終わりと判断されて「残りの文字列はなんだ？」ってエラーが出てしまいます。

そこで、エスケープのためにバックスラッシュ(\\)を前に置いて、`\"`と記述してください。こうすればエラーは出ないはず。

他にもつまずくことがあるかもしれませんが、Googleに頼りながら試してみてください。


## 動作環境

* Mac OS X: 10.11
* Evernote: ver 6.3 (452832 Direct)


## ソースコード

<!-- [citation.scpt を スクリプトエディタ.app で開く](applescript://com.apple.scripteditor?script=%28%2A%0A%2A%20%5B%5BClipping%5D%5D%3A%20%E5%BC%95%E7%94%A8%E5%86%85%E5%AE%B9%20-%20getClipping%28%29%0A%2A%20%5B%5BLink%5D%5D%3A%20%E3%83%AA%E3%83%B3%E3%82%AFURL%20-%20getLink%28%29%0A%2A%20%5B%5BTitle%5D%5D%3A%20%E3%83%AA%E3%83%B3%E3%82%AF%E3%82%BF%E3%82%A4%E3%83%88%E3%83%AB%20-%20getTitle%28%29%0A%2A%20%5B%5BAuthor%5D%5D%3A%20%E4%BD%9C%E8%80%85%20-%20getAuthor%28%29%0A%2A%20%5B%5BPublisher%5D%5D%3A%20%E5%87%BA%E7%89%88%E7%A4%BE%E3%80%81%E7%99%BA%E8%A1%8C%E5%85%83%20-%20getPublisher%28%29%0A%2A%20%5B%5BDate%5D%5D%3A%20%E6%97%A5%E4%BB%98%20-%20getDate%28%29%0A%2A%29%0Dproperty%20citationTemplate%20%3A%20%22%3Cbr%3E%0A%3Cdiv%20class%3D%27citation%27%20style%3D%27padding%3A%200px%2030px%3B%27%3E%0A%09%3Cdiv%20class%3D%27clipping%27%20style%3D%27line-height%3A18px%3B%20padding%3A%208px%2015px%3B%20border-left%3A2px%20solid%20%23dedede%3B%20color%3A%20%23919191%3B%20font-family%3A%20Helvetica%2C%20sans-serif%3B%20font-style%3A%20italic%3B%20font-size%3A%2014px%3B%27%3E%5B%5BClipping%5D%5D%3C%2Fdiv%3E%0A%09%3Cdiv%20style%3D%27padding-left%3A%2017px%3B%27%3E%0A%09%09%3Cdiv%20class%3D%27title%27%20style%3D%27color%3A%20%23000000%3B%20font-family%3A%20Helvetica%2C%20sans-serif%3B%20font-size%3A%2014px%3B%20margin-top%3A%2019px%3B%27%3E%0A%09%09%09%3Ca%20style%3D%27text-decoration%3A%20none%3B%20color%3A%20rgba%2845%2C190%2C96%2C1.0%29%3B%27%20href%3D%27%5B%5BLink%5D%5D%27%3E%0A%09%09%09%09%26quot%3B%5B%5BTitle%5D%5D%26quot%3B%0A%09%09%09%3C%2Fa%3E%0A%09%09%3C%2Fdiv%3E%0A%09%09%3Cdiv%20style%3D%27color%3A%20%23919191%3B%20font-size%3A%2012px%3B%20margin-top%3A%204px%3B%20font-family%3A%20%27CaeciliaLTStd%27%2C%20serif%3B%20font-style%3A%20italic%3B%27%3E%0A%09%09%09%3Cspan%20class%3D%27author%27%3E%5B%5BAuthor%5D%5D%3C%2Fspan%3E%0A%09%09%09%3Cspan%20class%3D%27publisher%27%3E%5B%5BPublisher%5D%5D.%3C%2Fspan%3E%0A%09%09%09%3Cspan%20class%3D%27date%27%3E%5B%5BDate%5D%5D%3C%2Fspan%3E%0A%09%09%3C%2Fdiv%3E%0A%09%3C%2Fdiv%3E%0A%3C%2Fdiv%3E%0A%3Cbr%3E%22%0D%0Don%20run%0D%09%0D%09--%E3%82%A2%E3%83%97%E3%83%AA%E3%82%B1%E3%83%BC%E3%82%B7%E3%83%A7%E3%83%B3%E5%88%A4%E5%88%A5%0D%09if%20application%20%22Evernote%22%20is%20running%20and%20application%20%22Evernote%22%20is%20frontmost%20then%0D%09%09set%20curApp%20to%20make%20Evernote%0D%09else%20if%20application%20%22Safari%22%20is%20running%20and%20application%20%22Safari%22%20is%20frontmost%20then%0D%09%09set%20curApp%20to%20make%20Safari%0D%09else%0D%09%09--set%20curApp%20to%20make%20some_application%0D%09%09error%0D%09end%20if%0D%09%0D%09--citation%E3%81%AEHTML%E4%BD%9C%E6%88%90%0D%09copy%20citationTemplate%20to%20citation%0D%09repeat%20with%20replacement%20in%20%7B%C2%AC%0D%09%09%7B%22%5B%5BClipping%5D%5D%22%2C%20getClipping%28%29%20of%20curApp%7D%2C%20%C2%AC%0D%09%09%7B%22%5B%5BLink%5D%5D%22%2C%20getLink%28%29%20of%20curApp%7D%2C%20%C2%AC%0D%09%09%7B%22%5B%5BTitle%5D%5D%22%2C%20getTitle%28%29%20of%20curApp%7D%2C%20%C2%AC%0D%09%09%7B%22%5B%5BAuthor%5D%5D%22%2C%20getAuthor%28%29%20of%20curApp%7D%2C%20%C2%AC%0D%09%09%7B%22%5B%5BPublisher%5D%5D%22%2C%20getPublisher%28%29%20of%20curApp%7D%2C%20%C2%AC%0D%09%09%7B%22%5B%5BDate%5D%5D%22%2C%20getDate%28%29%20of%20curApp%7D%20%C2%AC%0D%09%09%09%7D%0D%09%09set%20%7Bf%2C%20r%7D%20to%20replacement%0D%09%09set%20r%20to%20my%20escapeHTMLSpecialChars%28r%29%0D%09%09set%20r%20to%20my%20replace%28r%2C%20%7Breturn%2C%20linefeed%7D%2C%20%22%3Cbr%3E%22%29%0D%09%09set%20citation%20to%20my%20replace%28citation%2C%20f%2C%20r%29%0D%09end%20repeat%0D%09%0D%09--HTML%E3%82%92%E3%82%B3%E3%83%94%E3%83%BC%0D%09my%20copyHTML%28citation%29%0D%09display%20notification%20getTitle%28%29%20of%20curApp%20with%20title%20%22Copied%22%0Dend%20run%0D%0Dscript%20some_application%0D%09%0D%09on%20getClipping%28%29%0D%09%09repeat%20until%20application%20id%20%28my%20appID%29%20is%20frontmost%0D%09%09%09activate%20application%20id%20%28my%20appID%29%0D%09%09end%20repeat%0D%09%09set%20the%20clipboard%20to%20%22%22%0D%09%09set%20tmpClipboard%20to%20the%20clipboard%20as%20record%0D%09%09tell%20application%20%22System%20Events%22%0D%09%09%09keystroke%20%22c%22%20using%20%7Bcommand%20down%7D%0D%09%09%09set%20timestamp%20to%20current%20date%0D%09%09%09repeat%20while%20%28the%20clipboard%20as%20record%29%20%3D%20tmpClipboard%0D%09%09%09%09if%20%28current%20date%29%20%3E%20timestamp%20%2B%2010%20then%0D%09%09%09%09%09error%0D%09%09%09%09end%20if%0D%09%09%09%09delay%201%0D%09%09%09end%20repeat%0D%09%09end%20tell%0D%09%09return%20the%20clipboard%20as%20text%0D%09end%20getClipping%0D%09%0D%09on%20getLink%28%29%0D%09%09return%20%22%22%0D%09end%20getLink%0D%09%0D%09on%20getTitle%28%29%0D%09%09return%20%22%22%0D%09end%20getTitle%0D%09%0D%09on%20getAuthor%28%29%0D%09%09return%20%22%22%0D%09end%20getAuthor%0D%09%0D%09on%20getPublisher%28%29%0D%09%09return%20%22%22%0D%09end%20getPublisher%0D%09%0D%09on%20getDate%28%29%0D%09%09return%20%22%22%0D%09end%20getDate%0D%09%0D%09on%20make%0D%09%09set%20a_class%20to%20me%0D%09%09script%20Instance%0D%09%09%09property%20parent%20%3A%20a_class%0D%09%09%09property%20appID%20%3A%20run%20script%20%22tell%20application%20%5C%22System%20Events%5C%22%20to%20return%20bundle%20identifier%20of%20process%201%20whose%20frontmost%20%3D%20true%20and%20visible%20%3D%20true%22%0D%09%09end%20script%0D%09end%20make%0Dend%20script%0D%0Dscript%20Evernote%0D%09property%20parent%20%3A%20some_application%0D%09%0D%09--on%20getClipping%28%29%0D%09--%09continue%20getClipping%28%29%0D%09--end%20getClipping%0D%09%0D%09on%20getLink%28%29%0D%09%09tell%20application%20%22Evernote%22%0D%09%09%09return%20note%20link%20of%20%28note%20id%20%28my%20noteID%29%29%20of%20notebook%201%0D%09%09end%20tell%0D%09end%20getLink%0D%09%0D%09on%20getTitle%28%29%0D%09%09tell%20application%20%22Evernote%22%0D%09%09%09return%20title%20of%20%28note%20id%20%28my%20noteID%29%29%20of%20notebook%201%0D%09%09end%20tell%0D%09end%20getTitle%0D%09%0D%09on%20getAuthor%28%29%0D%09%09tell%20application%20%22Evernote%22%0D%09%09%09set%20noteAuthor%20to%20author%20of%20%28note%20id%20%28my%20noteID%29%29%20of%20notebook%201%0D%09%09end%20tell%0D%09%09if%20noteAuthor%20%3D%20missing%20value%20then%0D%09%09%09set%20noteAuthor%20to%20%22%22%0D%09%09end%20if%0D%09%09return%20noteAuthor%0D%09end%20getAuthor%0D%09%0D%09on%20getPublisher%28%29%0D%09%09tell%20application%20%22Evernote%22%0D%09%09%09return%20name%20of%20notebook%20of%20%28note%20id%20%28my%20noteID%29%29%20of%20notebook%201%0D%09%09end%20tell%0D%09end%20getPublisher%0D%09%0D%09on%20getDate%28%29%0D%09%09tell%20application%20%22Evernote%22%0D%09%09%09tell%20creation%20date%20of%20%28note%20id%20%28my%20noteID%29%29%20of%20notebook%201%20as%20date%0D%09%09%09%09return%20%22%22%20%26%20year%20%26%20%22%E5%B9%B4%22%20%26%20%28month%20of%20it%20as%20number%29%20%26%20%22%E6%9C%88%22%20%26%20day%20%26%20%22%E6%97%A5%22%0D%09%09%09end%20tell%0D%09%09end%20tell%0D%09end%20getDate%0D%09%0D%09on%20make%0D%09%09set%20self%20to%20continue%20make%0D%09%09script%20EvernoteInstance%0D%09%09%09property%20parent%20%3A%20self%0D%09%09%09property%20noteID%20%3A%20run%20script%20%22tell%20application%20%5C%22Evernote%5C%22%20to%20return%20local%20id%20of%20item%201%20of%20%28selection%20as%20list%29%22%0D%09%09end%20script%0D%09end%20make%0Dend%20script%0D%0Dscript%20Safari%0D%09property%20parent%20%3A%20some_application%0D%09%0D%09on%20getClipping%28%29%0D%09%09tell%20application%20%22Safari%22%0D%09%09%09set%20clipping%20to%20do%20JavaScript%20%22document.getSelection%28%29.toString%28%29%3B%22%20in%20current%20tab%20of%20window%20id%20%28my%20windowID%29%0D%09%09end%20tell%0D%09%09if%20clipping%20%3D%20%22%22%20then%0D%09%09%09try%0D%09%09%09%09set%20clipping%20to%20my%20extractText%28my%20metas%2C%20linefeed%20%26%20%22og%3Adescription%2C%22%2C%20linefeed%29%0D%09%09%09on%20error%20number%20-2700%0D%09%09%09end%20try%0D%09%09end%20if%0D%09%09return%20clipping%0D%09end%20getClipping%0D%09%0D%09on%20getLink%28%29%0D%09%09try%0D%09%09%09return%20my%20extractText%28my%20metas%2C%20linefeed%20%26%20%22og%3Aurl%2C%22%2C%20linefeed%29%0D%09%09on%20error%20number%20-2700%0D%09%09end%20try%0D%09%09tell%20application%20%22Safari%22%0D%09%09%09return%20URL%20of%20current%20tab%20of%20window%20id%20%28my%20windowID%29%0D%09%09end%20tell%0D%09end%20getLink%0D%09%0D%09on%20getTitle%28%29%0D%09%09try%0D%09%09%09return%20my%20extractText%28my%20metas%2C%20linefeed%20%26%20%22og%3Atitle%2C%22%2C%20linefeed%29%0D%09%09on%20error%20number%20-2700%0D%09%09end%20try%0D%09%09tell%20application%20%22Safari%22%0D%09%09%09return%20name%20of%20current%20tab%20of%20window%20id%20%28my%20windowID%29%0D%09%09end%20tell%0D%09end%20getTitle%0D%09%0D%09on%20getAuthor%28%29%0D%09%09try%0D%09%09%09return%20my%20extractText%28my%20metas%2C%20linefeed%20%26%20%22twitter%3Acreator%2C%22%2C%20linefeed%29%0D%09%09on%20error%20number%20-2700%0D%09%09end%20try%0D%09%09return%20%22%22%0D%09end%20getAuthor%0D%09%0D%09on%20getPublisher%28%29%0D%09%09try%0D%09%09%09return%20my%20extractText%28my%20metas%2C%20linefeed%20%26%20%22og%3Asite_name%2C%22%2C%20linefeed%29%0D%09%09on%20error%20number%20-2700%0D%09%09end%20try%0D%09%09return%20%22%22%0D%09end%20getPublisher%0D%09%0D%09on%20getDate%28%29%0D%09%09try%0D%09%09%09set%20ISODate%20to%20my%20extractText%28my%20metas%2C%20linefeed%20%26%20%22article%3Apublished_time%2C%22%2C%20linefeed%29%0D%09%09%09tell%20my%20ISODateToDate%28ISODate%29%0D%09%09%09%09return%20%22%22%20%26%20year%20%26%20%22%E5%B9%B4%22%20%26%20%28month%20of%20it%20as%20number%29%20%26%20%22%E6%9C%88%22%20%26%20day%20%26%20%22%E6%97%A5%22%0D%09%09%09end%20tell%0D%09%09on%20error%20number%20-2700%0D%09%09end%20try%0D%09%09return%20%22%22%0D%09end%20getDate%0D%09%0D%09on%20make%0D%09%09set%20self%20to%20continue%20make%0D%09%09script%20SafariInstance%0D%09%09%09property%20parent%20%3A%20self%0D%09%09%09property%20windowID%20%3A%20run%20script%20%22tell%20application%20%5C%22Safari%5C%22%20to%20return%20id%20of%20window%201%20whose%20current%20tab%20%E2%89%A0%20missing%20value%22%0D%09%09%09property%20metas%20%3A%20run%20script%20%22tell%20application%20%5C%22Safari%5C%22%20to%20return%20do%20JavaScript%20%5C%22var%20ary%3D%5B%5D%3B%20var%20elements%3Ddocument.getElementsByTagName%28%27meta%27%29%3B%20for%28i%3D0%3B%20i%3Celements.length%3B%20i%2B%2B%29%20%7Bcontent%3Delements%5Bi%5D.getAttribute%28%27content%27%29%3B%20if%28elements%5Bi%5D.getAttribute%28%27property%27%29%29%20%7Bary.push%28elements%5Bi%5D.getAttribute%28%27property%27%29%20%2B%20%27%2C%27%20%2B%20elements%5Bi%5D.getAttribute%28%27content%27%29%29%3B%7D%20else%20if%28elements%5Bi%5D.getAttribute%28%27name%27%29%29%20%7Bary.push%28elements%5Bi%5D.getAttribute%28%27name%27%29%20%2B%20%27%2C%27%20%2B%20elements%5Bi%5D.getAttribute%28%27content%27%29%29%3B%7D%7D%20%27%5C%5C%5C%5Cn%27%20%2B%20ary.join%28%27%5C%5C%5C%5Cn%27%29%20%2B%20%27%5C%5C%5C%5Cn%27%3B%5C%22%20in%20current%20tab%20of%20window%20id%20%22%20%26%20windowID%0D%09%09end%20script%0D%09end%20make%0Dend%20script%0D%0Don%20escapeHTMLSpecialChars%28args%29%0D%09if%20class%20of%20args%20%3D%20list%20then%0D%09%09repeat%20with%20i%20from%201%20to%20count%20args%0D%09%09%09set%20item%20i%20of%20args%20to%20my%20escapeHTMLSpecialChars%28item%20i%20of%20args%29%0D%09%09end%20repeat%0D%09else%0D%09%09set%20args%20to%20my%20replace%28args%2C%20%22%26%22%2C%20%22%26amp%3B%22%29%0D%09%09set%20args%20to%20my%20replace%28args%2C%20%22%3C%22%2C%20%22%26lt%3B%22%29%0D%09%09set%20args%20to%20my%20replace%28args%2C%20%22%3E%22%2C%20%22%26gt%3B%22%29%0D%09%09set%20args%20to%20my%20replace%28args%2C%20quote%2C%20%22%26quot%3B%22%29%0D%09%09set%20args%20to%20my%20replace%28args%2C%20%22%27%22%2C%20%22%26%23039%3B%22%29%0D%09end%20if%0D%09return%20args%0Dend%20escapeHTMLSpecialChars%0D%0Don%20replace%28str%20as%20text%2C%20find%2C%20replace%20as%20text%29%0D%09set%20oldDel%20to%20AppleScript%27s%20text%20item%20delimiters%0D%09set%20AppleScript%27s%20text%20item%20delimiters%20to%20find%0D%09set%20replaceList%20to%20text%20items%20of%20str%0D%09set%20AppleScript%27s%20text%20item%20delimiters%20to%20replace%0D%09set%20str%20to%20replaceList%20as%20string%0D%09set%20AppleScript%27s%20text%20item%20delimiters%20to%20oldDel%0D%09return%20str%0Dend%20replace%0D%0Don%20copyHTML%28HTML%20as%20text%29%0D%09set%20the%20clipboard%20to%20%7B%C2%ABclass%20utf8%C2%BB%3AHTML%2C%20string%3AHTML%2C%20Unicode%20text%3AHTML%2C%20%C2%ABclass%20HTML%C2%BB%3Amy%20htmlToRawData%28HTML%29%7D%0Dend%20copyHTML%0D%0Don%20extractText%28theText%2C%20beginText%2C%20endText%29%0D%09--beginText%2C%20endText%E4%BE%8B%3A%20%7Btext%3A%22a%22%2C%20forwardSearch%3Afalse%2C%20containText%3Atrue%7D%0D%09set%20beginText%20to%20%28beginText%20as%20record%29%20%26%20%7BforwardSearch%3Atrue%2C%20containText%3Afalse%7D%0D%09set%20endText%20to%20%28endText%20as%20record%29%20%26%20%7BforwardSearch%3Atrue%2C%20containText%3Afalse%7D%0D%09%0D%09if%20theText%20does%20not%20contain%20text%20of%20beginText%20or%20theText%20does%20not%20contain%20text%20of%20endText%20then%20error%20%22%E5%BC%95%E6%95%B0%E3%81%AE%E6%96%87%E5%AD%97%E5%88%97%E3%81%8C%E5%90%AB%E3%81%BE%E3%82%8C%E3%81%BE%E3%81%9B%E3%82%93%22%0D%09%0D%09set%20scraps%20to%20my%20split%28theText%2C%20text%20of%20beginText%29%0D%09if%20forwardSearch%20of%20beginText%20then%0D%09%09set%20theText%20to%20my%20join%28rest%20of%20scraps%2C%20text%20of%20beginText%29%0D%09else%0D%09%09set%20%7BtheText%7D%20to%20reverse%20of%20scraps%0D%09end%20if%0D%09set%20scraps%20to%20my%20split%28theText%2C%20text%20of%20endText%29%0D%09if%20forwardSearch%20of%20endText%20then%0D%09%09set%20%7BtheText%7D%20to%20scraps%0D%09else%0D%09%09set%20theText%20to%20my%20join%28reverse%20of%20rest%20of%20reverse%20of%20scraps%2C%20text%20of%20endText%29%0D%09end%20if%0D%09if%20containText%20of%20beginText%20then%0D%09%09set%20theText%20to%20%22%22%20%26%20text%20of%20beginText%20%26%20theText%0D%09end%20if%0D%09if%20containText%20of%20endText%20then%0D%09%09set%20theText%20to%20%22%22%20%26%20theText%20%26%20text%20of%20endText%0D%09end%20if%0D%09return%20theText%0Dend%20extractText%0D%0Don%20ISODateToDate%28ISODate%29%0D%09if%20class%20of%20ISODate%20%3D%20%C2%ABclass%20isot%C2%BB%20then%0D%09%09set%20aDate%20to%20ISODate%20as%20date%0D%09else%0D%09%09set%20aDate%20to%20%28do%20shell%20script%20%22echo%20%22%20%26%20quoted%20form%20of%20ISODate%20as%20%C2%ABclass%20isot%C2%BB%29%20as%20date%0D%09%09if%20ISODate%20ends%20with%20%22Z%22%20then%0D%09%09%09set%20aDate%20to%20my%20shiftDateFromGMT%28aDate%29%0D%09%09end%20if%0D%09end%20if%0D%09return%20aDate%0Dend%20ISODateToDate%0D%0Don%20htmlToRawData%28HTML%20as%20text%29%0D%09return%20run%20script%20%22%C2%ABdata%20HTML%22%20%26%20%28do%20shell%20script%20%22echo%20%22%20%26%20quoted%20form%20of%20HTML%20%26%20%22%20%7C%20hexdump%20-v%20-e%20%27%2F1%20%5C%22%2502X%5C%22%27%22%29%20%26%20%22%C2%BB%22%0Dend%20htmlToRawData%0D%0Don%20split%28str%20as%20text%2C%20delim%29%0D%09set%20oldDel%20to%20AppleScript%27s%20text%20item%20delimiters%0D%09set%20AppleScript%27s%20text%20item%20delimiters%20to%20delim%0D%09set%20aList%20to%20text%20items%20of%20str%0D%09set%20AppleScript%27s%20text%20item%20delimiters%20to%20oldDel%0D%09return%20aList%0Dend%20split%0D%0Don%20join%28textList%2C%20term%20as%20text%29%0D%09set%20oldDel%20to%20AppleScript%27s%20text%20item%20delimiters%0D%09set%20AppleScript%27s%20text%20item%20delimiters%20to%20term%0D%09set%20aText%20to%20textList%20as%20string%0D%09set%20AppleScript%27s%20text%20item%20delimiters%20to%20oldDel%0D%09return%20aText%0Dend%20join%0D%0Don%20shiftDateFromGMT%28aDate%29%0D%09global%20timeToGMT%0D%09try%0D%09%09timeToGMT%0D%09on%20error%20number%20-2753%0D%09%09set%20timeToGMT%20to%20time%20to%20GMT%0D%09end%20try%0D%09return%20aDate%20%2B%20timeToGMT%0Dend%20shiftDateFromGMT%0D) -->


```applescript:citation.scpt
(*
* [[Clipping]]: 引用内容 - getClipping()
* [[Link]]: リンクURL - getLink()
* [[Title]]: リンクタイトル - getTitle()
* [[Author]]: 作者 - getAuthor()
* [[Publisher]]: 出版社、発行元 - getPublisher()
* [[Date]]: 日付 - getDate()
*)
property citationTemplate : "<br>
<div class='citation' style='padding: 0px 30px;'>
	<div class='clipping' style='line-height:18px; padding: 8px 15px; border-left:2px solid #dedede; color: #919191; font-family: Helvetica, sans-serif; font-style: italic; font-size: 14px;'>[[Clipping]]</div>
	<div style='padding-left: 17px;'>
		<div class='title' style='color: #000000; font-family: Helvetica, sans-serif; font-size: 14px; margin-top: 19px;'>
			<a style='text-decoration: none; color: rgba(45,190,96,1.0);' href='[[Link]]'>
				&quot;[[Title]]&quot;
			</a>
		</div>
		<div style='color: #919191; font-size: 12px; margin-top: 4px; font-family: 'CaeciliaLTStd', serif; font-style: italic;'>
			<span class='author'>[[Author]]</span>
			<span class='publisher'>[[Publisher]].</span>
			<span class='date'>[[Date]]</span>
		</div>
	</div>
</div>
<br>"

on run
	
	--アプリケーション判別
	if application "Evernote" is running and application "Evernote" is frontmost then
		set curApp to make Evernote
	else if application "Safari" is running and application "Safari" is frontmost then
		set curApp to make Safari
	else
		--set curApp to make some_application
		error
	end if
	
	--citationのHTML作成
	copy citationTemplate to citation
	repeat with replacement in {¬
		{"[[Clipping]]", getClipping() of curApp}, ¬
		{"[[Link]]", getLink() of curApp}, ¬
		{"[[Title]]", getTitle() of curApp}, ¬
		{"[[Author]]", getAuthor() of curApp}, ¬
		{"[[Publisher]]", getPublisher() of curApp}, ¬
		{"[[Date]]", getDate() of curApp} ¬
			}
		set {f, r} to replacement
		set r to my escapeHTMLSpecialChars(r)
		set r to my replace(r, {return, linefeed}, "<br>")
		set citation to my replace(citation, f, r)
	end repeat
	
	--HTMLをコピー
	my copyHTML(citation)
	display notification getTitle() of curApp with title "Copied"
end run

script some_application
	
	on getClipping()
		repeat until application id (my appID) is frontmost
			activate application id (my appID)
		end repeat
		set the clipboard to ""
		set tmpClipboard to the clipboard as record
		tell application "System Events"
			keystroke "c" using {command down}
			set timestamp to current date
			repeat while (the clipboard as record) = tmpClipboard
				if (current date) > timestamp + 10 then
					error
				end if
				delay 1
			end repeat
		end tell
		return the clipboard as text
	end getClipping
	
	on getLink()
		return ""
	end getLink
	
	on getTitle()
		return ""
	end getTitle
	
	on getAuthor()
		return ""
	end getAuthor
	
	on getPublisher()
		return ""
	end getPublisher
	
	on getDate()
		return ""
	end getDate
	
	on make
		set a_class to me
		script Instance
			property parent : a_class
			property appID : run script "tell application \"System Events\" to return bundle identifier of process 1 whose frontmost = true and visible = true"
		end script
	end make
end script

script Evernote
	property parent : some_application
	
	--on getClipping()
	--	continue getClipping()
	--end getClipping
	
	on getLink()
		tell application "Evernote"
			return note link of (note id (my noteID)) of notebook 1
		end tell
	end getLink
	
	on getTitle()
		tell application "Evernote"
			return title of (note id (my noteID)) of notebook 1
		end tell
	end getTitle
	
	on getAuthor()
		tell application "Evernote"
			set noteAuthor to author of (note id (my noteID)) of notebook 1
		end tell
		if noteAuthor = missing value then
			set noteAuthor to ""
		end if
		return noteAuthor
	end getAuthor
	
	on getPublisher()
		tell application "Evernote"
			return name of notebook of (note id (my noteID)) of notebook 1
		end tell
	end getPublisher
	
	on getDate()
		tell application "Evernote"
			tell creation date of (note id (my noteID)) of notebook 1 as date
				return "" & year & "年" & (month of it as number) & "月" & day & "日"
			end tell
		end tell
	end getDate
	
	on make
		set self to continue make
		script EvernoteInstance
			property parent : self
			property noteID : run script "tell application \"Evernote\" to return local id of item 1 of (selection as list)"
		end script
	end make
end script

script Safari
	property parent : some_application
	
	on getClipping()
		tell application "Safari"
			set clipping to do JavaScript "document.getSelection().toString();" in current tab of window id (my windowID)
		end tell
		if clipping = "" then
			try
				set clipping to my extractText(my metas, linefeed & "og:description,", linefeed)
			on error number -2700
			end try
		end if
		return clipping
	end getClipping
	
	on getLink()
		try
			return my extractText(my metas, linefeed & "og:url,", linefeed)
		on error number -2700
		end try
		tell application "Safari"
			return URL of current tab of window id (my windowID)
		end tell
	end getLink
	
	on getTitle()
		try
			return my extractText(my metas, linefeed & "og:title,", linefeed)
		on error number -2700
		end try
		tell application "Safari"
			return name of current tab of window id (my windowID)
		end tell
	end getTitle
	
	on getAuthor()
		try
			return my extractText(my metas, linefeed & "twitter:creator,", linefeed)
		on error number -2700
		end try
		return ""
	end getAuthor
	
	on getPublisher()
		try
			return my extractText(my metas, linefeed & "og:site_name,", linefeed)
		on error number -2700
		end try
		return ""
	end getPublisher
	
	on getDate()
		try
			set ISODate to my extractText(my metas, linefeed & "article:published_time,", linefeed)
			tell my ISODateToDate(ISODate)
				return "" & year & "年" & (month of it as number) & "月" & day & "日"
			end tell
		on error number -2700
		end try
		return ""
	end getDate
	
	on make
		set self to continue make
		script SafariInstance
			property parent : self
			property windowID : run script "tell application \"Safari\" to return id of window 1 whose current tab ≠ missing value"
			property metas : run script "tell application \"Safari\" to return do JavaScript \"var ary=[]; var elements=document.getElementsByTagName('meta'); for(i=0; i<elements.length; i++) {content=elements[i].getAttribute('content'); if(elements[i].getAttribute('property')) {ary.push(elements[i].getAttribute('property') + ',' + elements[i].getAttribute('content'));} else if(elements[i].getAttribute('name')) {ary.push(elements[i].getAttribute('name') + ',' + elements[i].getAttribute('content'));}} '\\\\n' + ary.join('\\\\n') + '\\\\n';\" in current tab of window id " & windowID
		end script
	end make
end script

on escapeHTMLSpecialChars(args)
	if class of args = list then
		repeat with i from 1 to count args
			set item i of args to my escapeHTMLSpecialChars(item i of args)
		end repeat
	else
		set args to my replace(args, "&", "&amp;")
		set args to my replace(args, "<", "&lt;")
		set args to my replace(args, ">", "&gt;")
		set args to my replace(args, quote, "&quot;")
		set args to my replace(args, "'", "&#039;")
	end if
	return args
end escapeHTMLSpecialChars

on replace(str as text, find, replace as text)
	set oldDel to AppleScript's text item delimiters
	set AppleScript's text item delimiters to find
	set replaceList to text items of str
	set AppleScript's text item delimiters to replace
	set str to replaceList as string
	set AppleScript's text item delimiters to oldDel
	return str
end replace

on copyHTML(HTML as text)
	set the clipboard to {«class utf8»:HTML, string:HTML, Unicode text:HTML, «class HTML»:my htmlToRawData(HTML)}
end copyHTML

on extractText(theText, beginText, endText)
	--beginText, endText例: {text:"a", forwardSearch:false, containText:true}
	set beginText to (beginText as record) & {forwardSearch:true, containText:false}
	set endText to (endText as record) & {forwardSearch:true, containText:false}
	
	if theText does not contain text of beginText or theText does not contain text of endText then error "引数の文字列が含まれません"
	
	set scraps to my split(theText, text of beginText)
	if forwardSearch of beginText then
		set theText to my join(rest of scraps, text of beginText)
	else
		set {theText} to reverse of scraps
	end if
	set scraps to my split(theText, text of endText)
	if forwardSearch of endText then
		set {theText} to scraps
	else
		set theText to my join(reverse of rest of reverse of scraps, text of endText)
	end if
	if containText of beginText then
		set theText to "" & text of beginText & theText
	end if
	if containText of endText then
		set theText to "" & theText & text of endText
	end if
	return theText
end extractText

on ISODateToDate(ISODate)
	if class of ISODate = «class isot» then
		set aDate to ISODate as date
	else
		set aDate to (do shell script "echo " & quoted form of ISODate as «class isot») as date
		if ISODate ends with "Z" then
			set aDate to my shiftDateFromGMT(aDate)
		end if
	end if
	return aDate
end ISODateToDate

on htmlToRawData(HTML as text)
	return run script "«data HTML" & (do shell script "echo " & quoted form of HTML & " | hexdump -v -e '/1 \"%02X\"'") & "»"
end htmlToRawData

on split(str as text, delim)
	set oldDel to AppleScript's text item delimiters
	set AppleScript's text item delimiters to delim
	set aList to text items of str
	set AppleScript's text item delimiters to oldDel
	return aList
end split

on join(textList, term as text)
	set oldDel to AppleScript's text item delimiters
	set AppleScript's text item delimiters to term
	set aText to textList as string
	set AppleScript's text item delimiters to oldDel
	return aText
end join

on shiftDateFromGMT(aDate)
	global timeToGMT
	try
		timeToGMT
	on error number -2753
		set timeToGMT to time to GMT
	end try
	return aDate + timeToGMT
end shiftDateFromGMT
```



## 普段AppleScriptを使わない方へ

### メニューバーにスクリプトメニューを表示する方法

このスクリプトはスクリプトメニューから実行することを想定しています。スクリプトエディタから実行してもエラーになってしまうので、次に示す設定をお願いします。

* スクリプトエディタ.app (/Applications/Utilities/Script Editor.app) 起動
* メニューバー
* 「スクリプトエディタ」
* 「環境設定…」
* 「一般」
* 「スクリプトメニュー」
* 「メニューバーにスクリプトメニューを表示」 にチェック

メニューバーにスクリプトメニューのアイコンが表示されればOK。

### GUIスクリプティングを有効に

スクリプト中に「GUIスクリプティング」と呼ばれる機能を使っています。スクリプトメニューから実行する前に以下の設定が必要です。

* システム環境設定.app (/Applications/System Preferences.app) 起動
* 「セキュリティとプライバシー」
* 「プライバシー」
*  鍵マーク 「変更するにはカギをクリックします。」をクリック
*  パスワード入力
* 「ロックを解除」
* 「アクセシビリティ」
*  「+」
* /System/Library/CoreServices/SystemUIServer.app を選択
* 「開く」

### (2015-12-18 追記) スクリプトファイルの保存方法

スクリプトファイルの保存場所を書いていませんでした。スクリプトメニューからAppleScriptを実行するには特定の場所にファイルがなければいけません。

下の方法でスクリプトファイルを保存してください。

<!-- * このページ中部の「ソースコード」にある「citation.scpt を スクリプトエディタ.app で開く」をクリック
* 「開発元を識別できないスクリプト」というウインドウが表示された場合は「新規スクリプト」をクリック -->
* スクリプトエディタ.app (/Applications/Utilities/Script Editor.app) 起動
* ファイル選択ダイアログが表示された場合は「新規書類」
* このページ中部の「ソースコード」にある「citation.scpt」全体をスクリプトエディタ.appにコピペ
* ⌘+Kでコンパイル
* ⌘+Sで保存
* 名前を「citation.scpt」に
* 保存場所を「/Users/（ユーザ名）/Library/Scripts」に
* ファイルフォーマットは「スクリプト」のままで
* 「保存」

保存が完了しました。スクリプトエディタ.appを終了してください。

スクリプトの実行方法は冒頭の「使い方（基礎編）・（応用編）」に書いたとおり。きちんと動くか確認してみてください。


## 更新履歴

* 2015-12-16: Evernoteのノート上で引用型ノートリンクをコピーする機能実装
* 2015-12-17: Safariで引用型リンクをコピーする機能追加