2009-09-17
■[Safari][AppleScript]Safariで開いているページのタイトル・URL・選択部分をクリップボードにコピーする
Safariで調べものをして、たくさんの参考ページを開いているとします。このタイトルとURL一覧をメールで送ったり、エディタに書き出したりするのに、ひとつひとつ手でコピペすることはありません。こんなスクリプト実行するだけで、クリップボードにコピーできています。
tell application "Safari" set my_result to "" repeat with i from 1 to (count every document) repeat with ii from 1 to (count every tab of window i) set my_selection to do JavaScript "unescape(getSelection()).replace(/\\s+$/, '');" in tab ii of window i if (my_selection is not "") and (my_selection does not end with return) then set my_selection to "“" & my_selection & "”" & return end if set my_result to my_result & "『" & name of tab ii of window i & "』" & return & URL of tab ii of window i & return & my_selection & return end repeat end repeat set the clipboard to my_result end tell
(追記:2009-09-17T01:01:16+0900)ウインドウ内に選択テキストがあれば、それもクリップボードに取り込むようにしました。
(追記:2011-10-08T12:18:21+0900)選択部分を引用符で囲むようにしました。
ぺろんとMailにペーストしたところ:
使ってみればわかりますが、とても便利です。アプケーションとしてDocに入れておいてもいいし、スクリプトメニューに入れておいてもいいかもしれません。
カレントタブだけを対象にしたい場合
すべてのウィンドウのすべてのタブじゃなくて、いま開いている最前面のタブだけを対象にしたいときは、こちらを使うといいかもしれません。
tell application "Safari" set my_result to "" if exists window 1 then tell window 1 tell current tab set my_selection to do JavaScript "unescape(getSelection()).replace(/\\s+$/, '');" if (my_selection is not "") and (my_selection does not end with return) then set my_selection to "“" & my_selection & "”" & return end if set my_result to my_result & "『" & name & "』" & return & URL & return & my_selection & return end tell end tell set the clipboard to my_result end if end tell
まあ、これだけだったらプラグインがあるような気がしますけれど...^^
トラックバック - http://d.hatena.ne.jp/seuzo/20090917/1253114030
リンク元
- 34 http://www.seuzo.jp/st/index.html
- 25 http://dtpwiki.jp/planet/
- 17 http://www.seuzo.jp/st/scripts_other/index.html
- 12 http://www.google.com/reader/view/
- 9 http://reader.livedoor.com/reader/
- 8 http://b.hatena.ne.jp/hira_fumi/CS4/
- 8 http://www.advance-1st.co.jp/cybozu/ag.cgi?page=AGIndex
- 6 http://www.advance-1st.co.jp/cybozu/ag.cgi?
- 6 http://www.google.co.jp/search?client=safari&rls=en&q=applescript+開いている+window+name&ie=UTF-8&oe=UTF-8&redir_esc=&ei=6muTS5_3McmekQWii-iZDQ
- 6 http://www.google.co.jp/search?client=safari&rls=en&q=safari+url+一覧&ie=UTF-8&oe=UTF-8&redir_esc=&ei=RjfDS9maFY2TkAX31MzBBQ



