あらゆるブラウザで表示中の全ページを、指定したブラウザで一気に開き直すAppleScript

昨日のSafari_to_Other.scptの続き。やはり、コピー元がSafariに限定されては、今ひとつ使い勝手が悪い。理想は、あらゆるブラウザ間で、自由に開き直せるようにしたい。

しかし、AppleScriptに対応していないブラウザがあったり、対応していてもURLを取得する方法がそれぞれ違っていたりと、一筋縄では行けない...。Firefoxに至っては、GUIスクリプティングを利用しても、タブのエレメント情報さえ取得できない状態であった。(UIElementInspector.appで確認した限り)

そこで、最もAppleScript対応状況が悪そうなFirefoxから全URLを取得する方法を考えてみた。そうすれば、その他のブラウザでも処理できる共通の方法となるはず。

環境

ショートカットキーの連続技でURLを取得する

Firefoxではタブのエレメント情報が取得できないので、キー操作でURLのコピーとタブの移動を繰り返すしかないと考えた。

  • command-L
  • command-C
  • command-option-→

そうすると問題なのが、上記操作をいつまで繰り返すか...。タブの数量を取得したいところだが、タブのエレメント情報が取得できない状況ではその方法が思い付かない。
窮余の策で、操作を繰り返していて同じURLを取得したら、そこで止めることにした。だから、一つのウィンドウで同じURLのタブを複数開いていると、全てのタブを巡回せずに途中で終わってしまう。ちょっと問題ありそうだけど、他に方法が思い付かないので、この仕様でやってみる。


(*
このスクリプトは、アクティブなブラウザで表示中の全URLを取得します。 取得したいwebブラウザをアクティブにして、get_URLs.scptを実行します。 取得したURLは、open_URLs.scptにて一気に開くことが出来ます。 このスクリプトを動かすために必要なもの AppleScriptライブラリ 依存するAppleScriptライブラリを以下のページからダウンロードしておく必要があります。 http://github.com/zarigani/AppleScript-bebe-s-Library/tree/master
そして、以下のファイルをユーザースクリプトフォルダ(~/Library/Scripts)にインストールしてください。 _lib.scpt _gui.scpt *)
property GUI : load script file ((path to scripts folder as text) & "_gui.scpt")
property LIB : load script file ((path to scripts folder as text) & "_lib.scpt")

property NEXT_TABS : {{"Safari", "control-tab"}, {"Shiira", "command-shift-}"}, {"Firefox", "command-option-→"}, {"Opera", "command-option-→"}}

GUI's init_with_interval(0.18)
set app_name to GUI's frontmost_app()
set next_tab to LIB's look_up_with_default(NEXT_TABS, app_name, "command-option-→")

activate
app_name & "で表示中のURLを取得します。" -- & return & return & tab_names
display alert result buttons {"キャンセル", "全ウィンドウの全URL", "このウィンドウの全URL"} cancel button 1 default button 3 giving up after 30
if result's button returned = "このウィンドウの全URL" then
set url_list to top_window_URLs(app_name, next_tab)
else if result's button returned = "全ウィンドウの全URL" then
set url_list to all_window_urls(app_name, next_tab)
else
error number -128
end if

set the clipboard to "'" & LIB's join(url_list, "' '") & "'"
set url_num to (count LIB's split(the clipboard as text, "' '")) as text
LIB's message(app_name, url_num & " URLを取得できました。" & return & LIB's join(url_list, return))



on all_window_urls(app_name, next_tab)
set url_list to {}
tell application "System Events"
tell process app_name
repeat with a_windw in every window
GUI's app_shortcut(app_name, "command-`")
set url_list's end to my top_window_URLs(app_name, next_tab)
end repeat
end tell
end tell
set url_list to LIB's reject_if(url_list, {{}})
end all_window_urls

on top_window_URLs(app_name, next_tab)
set url_list to {}
repeat
set the clipboard to ""
GUI's shortcut(app_name, "command-L")
GUI's shortcut(app_name, "command-C")
if (the clipboard as text) is in url_list then exit repeat
set url_list's end to the clipboard as text
GUI's shortcut(app_name, next_tab)
end repeat
set url_list to LIB's reject_if(url_list, {""})
end top_window_URLs

取得したURLを一気に開く

以上で、URLがクリップボードに転送される。あとは、クリップボードのURLを利用して「open -a アプリケーション名 URLリスト」コマンドを実行すれば良いはず。


(*
このスクリプトは、get_URLs.scptで取得したURLを使って、指定したブラウザで開きます。 利用したいwebブラウザをアクティブにして、このスクリプトを実行します。 ブラウザの設定によっては、タブでなく、ウィンドウが複数開いてしまう可能性もあります。 このスクリプトを動かすために必要なもの AppleScriptライブラリ 依存するAppleScriptライブラリを以下のページからダウンロードしておく必要があります。 http://github.com/zarigani/AppleScript-bebe-s-Library/tree/master
そして、以下のファイルをユーザースクリプトフォルダ(~/Library/Scripts)にインストールしてください。 _lib.scpt _gui.scpt *)
property GUI : load script file ((path to scripts folder as text) & "_gui.scpt")
property LIB : load script file ((path to scripts folder as text) & "_lib.scpt")

GUI's init()
--実行時に最前面のアプリケーションを保存する
set app_name to GUI's frontmost_process()

set url_options to the clipboard as text
if url_options ≠ "''" then
GUI's shortcut(app_name, "command-N")
delay 1
do shell script ("open -a " & app_name & space & url_options)
end if

ついでに

上記の方法では、キー操作を自動的に繰り返しているだけなので、タブの枚数が多いと結構時間がかかる。(タブが40枚あったら、30秒くらいかかってしまうんじゃないだろうか。)もっと高速に操作したいが、途中のinit_with_interval(0.18)をより小さくしてしまうと、キー操作のレスポンスが返る前に次のキー操作に入ってしまう為なのか、このスクリプトが正常に動作しなくなってしまう...。
Operaについては、AppleScriptに対応していて、URLを一気に取得できるので、Opera_to_Other.scptも作ってみた。こちらは、タブが何枚あろうと、瞬時にURL取得を完了して、指定ブラウザでタブを開き始める。


(*
このスクリプトは、現在Operaで開いている全ページを、指定したブラウザで一気に開きます。 利用したいwebブラウザをアクティブにして、Opera_to_Other.scptを実行します。 全てのページを一つの新規ウィンドウを開いて表示します。 最前面のOperaウィンドウのみに限定することができます。(デフォルト) このスクリプトを動かすために必要なもの AppleScriptライブラリ 依存するAppleScriptライブラリを以下のページからダウンロードしておく必要があります。 http://github.com/zarigani/AppleScript-bebe-s-Library/tree/master
そして、以下のファイルをユーザースクリプトフォルダ(~/Library/Scripts)にインストールしてください。 _lib.scpt _gui.scpt *)
property GUI : load script file ((path to scripts folder as text) & "_gui.scpt")
property LIB : load script file ((path to scripts folder as text) & "_lib.scpt")

--実行時に最前面のアプリケーションを保存する
set app_name to GUI's frontmost_process()

--Operaで開いているページのリストを取得する
tell application "Opera"
activate
delay 0.5
set tab_names to "" & LIB's join(my top_window_names(), return & "")
"Operaで表示中のページを、" & app_name & "で開きます。" & return & return & tab_names
display alert result buttons {"キャンセル", "全ウィンドウの全タブを開く", "このウィンドウの全タブを開く"} cancel button 1 default button 3 giving up after 30
if result's button returned = "このウィンドウの全タブを開く" then
set window_tab_urls to {my top_window_URLs()}
else if result's button returned = "全ウィンドウの全タブを開く" then
set window_tab_urls to my all_window_urls()
else
error number -128
end if
end tell

--空のリストとTop Siteを除外する
repeat with tab_urls in window_tab_urls
if tab_urls's number > 0 then
set tab_urls's contents to LIB's reject_if(tab_urls, "topsites://")
end if
end repeat
--さらに、Top Siteを除外した結果、空になったリストを除外する
set window_tab_urls to LIB's reject_if(window_tab_urls, {})

--シングルクォートしたスペース区切りのURLテキストを生成する
set url_options to "'" & LIB's join(window_tab_urls, "' '") & "'"
--中身が存在すれば新規ウィンドウを開いて表示する
if url_options ≠ "''" then
tell application app_name to activate
delay 0.5
GUI's shortcut("", "command-N")
delay 0.5
do shell script ("open -a " & app_name & space & url_options)
end if



on all_window_urls()
set url_list to {}
tell application "System Events"
tell process "Opera"
repeat with a_windw in every window
GUI's app_shortcut("Opera", "command-`")
set url_list's end to my top_window_URLs()
end repeat
end tell
end tell
set url_list to LIB's reject_if(url_list, {{}})
end all_window_urls

on top_window_URLs()
each_document("URL")
end top_window_URLs

on top_window_names()
each_document("name")
end top_window_names

on each_document(option)
tell application "Opera"
set doc_num to count window 1's documents
set a_list to {}
repeat with i from 1 to doc_num
set a_list's end to run script ("document " & i & "'s " & option)
end repeat
end tell
a_list's reverse
end each_document

ダウンロードと使い方

  • 実行方法は、以下のどれか(利用するブラウザがアクティブな状態になっている必要がある)
    • スクリプトメニュー(~/Library/Scripts/)から利用する
    • Quicksilver等でショートカットを割り当てる
    • スクリプトエディタから実行する時は、ブラウザをアクティブにした状態をキープしつつ、コマンドキーを押しながら実行ボタンをクリックする

これで思う存分、最新のブラウザを気軽に試すことができるようになった!