SafariでURLとTitleと選択している文字列を引用するためのAppleScriptを作ってみようと思います。
まずURLとTitleは
set t to name of document 1 set u to URL of document 1
で取得できました。
選択文字列はselectionで取得できるかと思ったら、どうやら無理のようです。で、考えられる方法は二つ。
- keystrokeかkey codeでcommand+cを送出してコピー
- JavaScriptでselectionを取得
ですね。今回はJavaScriptで取得してみます。
set theSelection to do JavaScript "unescape(escape(getSelection()));" in document 1
選択箇所がない場合はタイトルをリンク付きで取得。選択箇所がある場合はblockquoteで取得するようにしました。
tell application "Safari"
set theTitle to name of document 1
set theURL to URL of document 1
set theSelection to do JavaScript "unescape(escape(getSelection()));" in document 1
if theSelection is "" then
set theText to "<a href='" & theURL & "'>" & theTitle & "</a>"
else
set theText to "<blockquote cite='" & theURL & "' title='" & theTitle & "'>" & return & "<p>" & theSelection & "<cite><a href='" & theURL & "'>" & theTitle & "</a></cite></p>" & return & "</blockquote>" & return
end if
set the clipboard to theText as Unicode text
end tell