TextExpanderがいつのまにかApp Storeに登場しているのを知って、改めて使い方を考えてみようと思い始めています。ということで、手軽にできて便利な使い方の紹介です。
1: 材料となるスニペットを登録する
まずは以下のスクリプトを script-SafariSelection という省略形で登録します。
set rText to getSafariSelection()
return rText
on getSafariSelection()
set aProcess to existOfProcessByBundleID("com.apple.safari")
if aProcess is equal to false then
return ""
else
tell application "Safari"
if (count of document) is greater than 0 then
set selectionCommand to "window.unescape(escape(window.getSelection()));"
set aSelection to (do JavaScript selectionCommand in document 1) as string
end if
if (count of aSelection) is greater than 0 then
return aSelection
else
return ""
end if
end tell
end if
end getSafariSelection
on existOfProcessByBundleID(bundleID)
tell application "System Events"
set aProc to every process whose bundle identifier is equal to bundleID
if aProc = {} then return false
set aProc to contents of first item of aProc
return aProc
end tell
end existOfProcessByBundleID
そしてこの写真の矢印で示した部分を「プレーンテキスト」から「アップルスクリプト」に変更します。このコードは AppleScript で Safari で選択しているテキストを取得するコードになります。
また同様に以下の2つのスニペットも登録します。
このスニペットを script-SafariTitle という省略形で登録。アップルスクリプトに変更。
set rText to getSafariTitle()
return rText
on getSafariTitle()
set aProcess to existOfProcessByBundleID("com.apple.safari")
if aProcess is equal to false then
return ""
else
tell application "Safari"
if (count of document) is greater than 0 then
set titleCommand to "window.document.title;"
set aTitle to (do JavaScript titleCommand in document 1) as string
end if
if (count of aTitle) is greater than 0 then
return aTitle
else
return ""
end if
end tell
end if
end getSafariTitle
on existOfProcessByBundleID(bundleID)
tell application "System Events"
set aProc to every process whose bundle identifier is equal to bundleID
if aProc = {} then return false
set aProc to contents of first item of aProc
return aProc
end tell
end existOfProcessByBundleID
このスニペットを script-SafariURL という省略形で登録。アップルスクリプトに変更。
set rText to getSafariURL()
return rText
on getSafariURL()
set aProcess to existOfProcessByBundleID("com.apple.safari")
if aProcess is equal to false then
return ""
else
tell application "Safari"
if (count of document) is greater than 0 then
set urlCommand to "window.location.href;"
set aURL to (do JavaScript urlCommand in document 1) as string
end if
if (count of aURL) is greater than 0 then
return aURL
else
return ""
end if
end tell
end if
end getSafariURL
on existOfProcessByBundleID(bundleID)
tell application "System Events"
set aProc to every process whose bundle identifier is equal to bundleID
if aProc = {} then return false
set aProc to contents of first item of aProc
return aProc
end tell
end existOfProcessByBundleID
これで下準備は完了です。
2: 次に実際に使用するスニペットを登録します。
以下のようにスニペットを登録します。
<figure>
<blockquote>
<p>
%snippet:script-SafariSelection%
</p>
</blockquote>
<figcaption>
<a href="%snippet:script-SafariURL%">%snippet:script-SafariTitle%</a> より
</figcaption>
</figure>
このとき写真のポイントと書いてあるところをみると %snippet:script-SafariSelection% と登録してあるのがわかると思います。これは script-SafariSelection という省略形のスニペットを展開して挿入するという意味で 手順1 で登録したスニペットを呼び出しています。
このスニペットを使うとSafariで選択した範囲の文章を引用文として、開いているページのタイトル、そして URL を自動で挿入することができるようになります。またコードを <a href="%snippet:script-SafariURL%">%snippet:script-SafariTitle%</a> のように変更すれば、もっと単純な a タグによるリンクをつくることができます。ブログを書いている人には便利かもしれないですね♪
まとめ
というわけで AppleScript と TextExpander の組み合わせ、いかがだったでしょうか? 知ってるよ。。。って方、ごめんなさい。これからもっともっと使いこなして文字を打つ回数を減らしていけたらいいなぁと。たかともでした。
TextExpander ってやっぱりすごいなぁ〜。
追記
最初に公開したときとコードが変更しました。Safariが起動していない場合の処理を追記したので、コードが長くなっていますが、機能は同じものになります。

