ダウンロード:Google_Site_Search_2.1.scpt.zip (8KB)
<使い方>
- 解凍してできあがった Google_Site_Search_2.scpt をホームフォルダ内のライブラリ/Scripts またはその中のサブフォルダ内に入れます。Safari または Firefox が最前面にあるときにしか動作しませんので、Scripts フォルダ内に Applications フォルダを作っている方は、その中の Safari または Firefox フォルダに入れておいてもいいかもしれません。両方のブラウザで動作させたいなら、片方のフォルダにはエイリアスを入れておくといいでしょう。
- UI Element Scripting 環境が必要になりますので、『This script needs UI Element Scripting.』というダイアログが出てしまった場合には『システム環境設定 → ユニバーサルアクセスペイン』で『補助装置を使用可能にする』チェックボックスをチェックしてください。
- Safari または Firefox でサイト内検索をしたいページを開いた状態で、スクリプトメニューから実行します。たとえばブログの場合、エントリページからスタートしても OK です。
- サイト内検索の対象にする URI がダイアログで示されますので、必要に応じて編集(URI を削る)してください。
- 2番目のダイアログに検索語を入れます。複数の検索ワードを使う場合は、スペース区切りです。ただ、複数の検索語をクオーテイションマークでくくっておこなう「フレーズ検索」には対応していません。複数語の「and 検索」になってしまいます。
Firefox の場合、AppleScript から URI を直接渡すと、日本語が文字化けしてしまいます。そのため、UI Element Scripting で Systen Events を操作して『URI をクリップボードに格納 → 新規タブ → URI 入力窓にペースト → リターンキー』という動作をさせています。そのため、Safari で動作させるよりもじゃっかん、もたつくかもしれません。
こんなスクリプトになりました。
tell application "System Events"
if not (UI elements enabled) then
tell application "System Preferences"
activate
set current pane to pane "com.apple.preference.universalaccess"
set dialog_message to "Error: This script needs UI Element Scripting." & return & return & "Set it on Univesal Access pane."
display dialog dialog_message buttons "Cancel" default button 1 with icon caution
end tell
end if
set the browser_name to ""
set the frontmost_application_name to displayed name of (path to frontmost application)
if frontmost_application_name is "Safari" then
set the browser_name to "Safari"
else if frontmost_application_name is "Firefox" then
set the browser_name to "Firefox"
else
activate
display dialog "Error: Any Browser is not Frontmost." & return & return & "You must bring \"Safari\" or \"Firefox\" to frontmost Application." buttons {"Cancel"} default button 1 with icon caution
end if
end tell
--
if browser_name is "Safari" then
tell application "Safari"
set the url_for_search to the URL of document 1 as text
end tell
else
tell application "Firefox"
set the url_for_search to «class curl» of window 1
end tell
end if
--
my replaceText(url_for_search, "http://", "")
set the url_for_search to the result
set the number_of_search_words to number of items of url_for_search
repeat with i from 1 to number_of_search_words
if item -i of url_for_search is "/" then
set the minus_num to i
exit repeat
end if
end repeat
set the search_site to ""
repeat with i from 1 to (number_of_search_words - minus_num)
set search_site to search_site & item i of url_for_search
end repeat
display dialog "Edit URI" default answer search_site buttons {"Cancel", "OK"} default button 2
copy the result as list to {text_returned, button_pressed}
my replaceText(text_returned, "/", "%2F")
set search_site to the result
--
display dialog "Enter search word(s)" default answer "" buttons {"Cancel", "OK"} default button 2
copy the result as list to {text_returned, button_pressed}
set word_num to the number of words of text_returned
repeat with i from 1 to word_num
if i is 1 then
set search_word to word 1 of text_returned
else
set search_word to search_word & "%20" & word i of text_returned
end if
end repeat
--
set the_uri to "http://www.google.com/search?q=" & search_word & "+site%3A" & search_site
--
if browser_name is "Safari" then
tell application "Safari"
-- set the_uri to the_uri & "&btnG=Google+検索&lr="
open location the_uri
end tell
else
tell application "Firefox"
activate
set the_uri to the_uri & "&start=0&start=0&hl=ja&lr=lang_ja&ie=utf-8&oe=utf-8&client=firefox-a&rls=org.mozilla:ja-JP:official"
set the clipboard to the_uri
tell application "System Events"
delay 0.5
keystroke "t" using command down
delay 0.5
keystroke "v" using command down
keystroke return
end tell
end tell
end if
--
on replaceText(targetStr, pre, pro)
set CurDelim to AppleScript's text item delimiters
set AppleScript's text item delimiters to pre
set targetStr to text items of targetStr
set AppleScript's text item delimiters to pro
set targetStr to targetStr as string
set AppleScript's text item delimiters to CurDelim
return targetStr
end replaceText