Webブラウザ(Safari)を便利に使うためのアップルスクリプト



Safariで表示されているページをFireFoxで表示する

選択した住所をYahoo地図で検索する

Yahoo地図で表示している地図をGoogleマップで表示する(その逆も)

選択した語句をexciteの英和・和英辞典で調べる

Safariで表示しているページをexciteやGoogleで翻訳する

Safariで選択した語句をgoogleで翻訳する

自動リンクになってないURLやttp://、wwwなどで始まってるURLを選択して開く

全角のURLを選択して開く

Safariのウインドウを整頓する



★ Safariで現在表示されているページをFireFoxで表示するアップルスクリプト

いわゆる「たすけてキツネ」です。

--tasuketekitune
tell application "Safari"
	set curl to URL of document 1
end tell
tell application "Firefox"
	OpenURL curl
	activate
end tell


★ 選択した住所をYahoo地図で検索するアップルスクリプト

スクリプトを使用するにあたっては、「システム環境設定」の「ユニバーサルアクセス」の「補助装置にアクセスできるようにする」のチェックをオンにしてください。

--選択した住所をYahoo地図で検索
set cb to the clipboard as record
tell application "System Events"
	tell application process "Safari"
		tell application "Safari" to activate
		delay 0.5
		keystroke "c" using command down
		delay 0.5
		set address to the clipboard
	end tell
end tell
set the clipboard to cb
if address contains return then set address to do shell script "echo " & quoted form of address & " | sed s/" & return & "//g"
set escaddress to do shell script "echo " & quoted form of address & "| iconv -f UTF-8-MAC -t EUC-JP | perl -pe '~s/([^?w ])/¥"%¥".unpack(¥"H2¥", $1)/eg;'"
set escaddress to characters 1 thru -4 of escaddress as Unicode text
tell application "Safari"
	open location "http://search.map.yahoo.co.jp/search?p=" & escaddress & "&pref=&skind="
end tell


★ Yahoo地図で表示している地図をGoogleマップで表示するアップルスクリプト(その逆も)

使い方はちょいと面倒ですが、スクロール地図の性質上しゃーないってことで。

Yahoo地図では地図右上の「この地図のURL」をクリックし、表示されたURLをクリックしてからスクリプトを実行します。

Googleマップでは地図右上の「リンク」をクリックしてからスクリプトを実行します。

--Yahoo地図⇔Googleマップ 2009年8月のYahoo地図リニューアルに対応
set convurl to ""

tell application "Safari"
	set clp to do JavaScript "var slct = escape(window.getSelection()); unescape(slct);" in document 1
	
	if clp contains "map.yahoo" then
		set AppleScript's text item delimiters to "&lat="
		set clp to text item 2 of clp
		
		set AppleScript's text item delimiters to "&lon="
		set ido to text item 1 of clp
		set clp to text item 2 of clp
		set AppleScript's text item delimiters to "&z="
		set keido to text item 1 of clp
		set clp to text item 2 of clp
		set AppleScript's text item delimiters to "&mode="
		set syuku to (text item 1 of clp) as number
		set syuku to syuku - 1
		
		set convurl to "http://maps.google.co.jp/maps?ie=UTF8&ll=" & ido & "," & keido & "&spn=0.0,0.0&z=" & syuku
		set AppleScript's text item delimiters to ""
		
	end if
	
	if clp contains "maps.google" then
		set AppleScript's text item delimiters to "&ll="
		set clp to text item 2 of clp
		set AppleScript's text item delimiters to ","
		set ido to text item 1 of clp
		set clp to text items 2 thru 3 of clp as Unicode text
		set AppleScript's text item delimiters to "&spn="
		set keido to text item 1 of clp
		set clp to text item 2 of clp
		set AppleScript's text item delimiters to "&z="
		set syuku to (word 1 of text item 2 of clp) as number
		set syuku to syuku + 1
		
		set convurl to "http://map.yahoo.co.jp/pl?type=scroll&lat=" & ido & "&lon=" & keido & "&z=" & syuku & "&mode=map&pointer=on&datum=wgs"
		set AppleScript's text item delimiters to ""
	end if
	
	if convurl is "" then
		display dialog "Yahoo地図では地図右上の「この地図のURL」をクリックし、表示されたURLをクリックしてからスクリプトを実行します。" & return & "Googleマップでは地図右上の「リンク」をクリックしてからスクリプトを実行します。" giving up after 10
		return
	end if
	
	open location convurl
end tell


★ 選択した語句をexciteの英和・和英辞典で調べるアップルスクリプト

スクリプトを使用するにあたっては、「システム環境設定」の「ユニバーサルアクセス」の「補助装置にアクセスできるようにする」のチェックをオンにしてください。

--選択した単語をexciteの英和・和英辞典で調べる
set cb to the clipboard as record
tell application "System Events"
	tell application process "Safari"
		tell application "Safari" to activate
		delay 0.5
		keystroke "c" using command down
		delay 0.5
		set surl to the clipboard
	end tell
end tell
set the clipboard to cb
if surl contains return then set surl to do shell script "echo " & quoted form of surl & " | sed s/" & return & "//g"
set surl to "http://www.excite.co.jp/dictionary/japanese_english/?submit=+検+索+&match=beginswith&search=" & surl
tell application "Safari"
	open location surl
end tell


★ Safariで表示しているページをexciteやGoogleで翻訳するアップルスクリプト

tell application "Safari"
	set currenturl to URL of document 1
	set sites to choose from list {"自動→日 google", "英→日 excite", "英→日 infoseek", "英→日 google", "英→日 excite & infoseek & google", "中→日 excite", "中→日 infoseek", "中→日 google", "韓→日 excite", "韓→日 infoseek", "韓→日 google", "仏→日 google", "西→日 google", "独→日 google"} with prompt "翻訳サイトを選択" default items {"自動→日 google"}
	if sites is false then return
	
	if item 1 of sites is "自動→日 google" then open location "http://translate.google.com/translate?hl=ja&sl=auto&u=" & currenturl
	
	if item 1 of sites is "英→日 infoseek" or item 1 of sites is "英→日 excite & infoseek & google" then open location "http://translation.infoseek.co.jp/?ac=Web&lng=en&selector=0&submit=&dsp=0&web_translate_url=" & currenturl & "&outer="
	
	if item 1 of sites is "英→日 excite" or item 1 of sites is "英→日 excite & infoseek & google" then open location "http://www.excite.co.jp/world/english/web/?wb_url=" & currenturl & "&wb_lp=ENJA"
	
	if item 1 of sites is "英→日 google" or item 1 of sites is "英→日 excite & infoseek & google" then open location "http://translate.google.com/translate?hl=ja&sl=en&u=" & currenturl
	
	if item 1 of sites is "中→日 excite" then open location "http://www.excite.co.jp/world/chinese/web/?wb_url=" & currenturl & "&wb_lp=CHJA"
	
	if item 1 of sites is "中→日 infoseek" then open location "http://translation.infoseek.co.jp/?ac=Web&lng=zh&web_translate_url=" & currenturl
	
	if item 1 of sites is "中→日 google" then open location "http://translate.google.com/translate?hl=ja&sl=zh&u=" & currenturl
	
	if item 1 of sites is "韓→日 excite" then open location "http://www.excite.co.jp/world/korean/web/?wb_url=" & currenturl & "&wb_lp=KOJA&wb_dis=2"
	
	if item 1 of sites is "韓→日 infoseek" then open location "http://translation.infoseek.co.jp/?ac=Web&lng=ko&web_translate_url=" & currenturl
	
	if item 1 of sites is "韓→日 google" then open location "http://translate.google.com/translate?hl=ja&sl=ko&u=" & currenturl
	
	if item 1 of sites is "仏→日 google" then open location "http://translate.google.com/translate?hl=ja&sl=fr&u=" & currenturl
	
	if item 1 of sites is "西→日 google" then open location "http://translate.google.com/translate?hl=ja&sl=es&u=" & currenturl
	
	if item 1 of sites is "独→日 google" then open location "http://translate.google.com/translate?hl=ja&sl=de&u=" & currenturl
	
end tell


★ Safariで選択した語句をgoogleで翻訳するアップルスクリプト

tell application "Safari"
	set slct to do JavaScript "var slct = escape(window.getSelection()); unescape(slct);" in document 1
	
	if (source of document 1) contains "frameset" is true or slct is "" then
		set slct to do JavaScript "var slct = window.getSelection().toString(); if (! slct){if (frames.length){\n        for (var i = 0; i < frames.length; i ++ ){slct = frames[i].getSelection().toString(); if (slct) break;}}}" in document 1
	end if
	
	if slct is "" then
		set cb to the clipboard as record
		tell application "Safari" to activate
		delay 0.5
		tell application "System Events" to keystroke "c" using command down
		delay 0.5
		set slct to the clipboard
		set the clipboard to cb
	end if
	
	if slct is "" then return
	if slct contains return then set slct to do shell script "echo " & quoted form of slct & " | sed s/" & return & "//g"
	
	set sites to choose from list {"自動→日", "英→日", "中→日", "韓→日", "仏→日", "西→日", "独→日", "伊→日"} default items {"自動→日"} with prompt "" & return & "を翻訳"
	if sites is false then return
	
	if "自動→日" is in sites then open location "http://translate.google.com/?hl=ja#auto|ja|" & slct
	if "英→日" is in sites then open location "http://translate.google.com/?hl=ja#en|ja|" & slct
	
	if "中→日" is in sites then open location "http://translate.google.com/?hl=ja#zh-CN|ja|" & slct
	
	if "韓→日" is in sites then open location "http://translate.google.com/?hl=ja#ko|ja|" & slct
	
	if "仏→日" is in sites then open location "http://translate.google.com/?hl=ja#fr|ja|" & slct
	
	if "西→日" is in sites then open location "http://translate.google.com/?hl=ja#es|ja|" & slct
	
	if "独→日" is in sites then open location "http://translate.google.com/?hl=ja#de|ja|" & slct
	
end tell

★ 自動リンクになってないURLやttp://、wwwなどで始まってるURLを選択して開くアップルスクリプト
スクリプトを使用するにあたっては、「システム環境設定」の「ユニバーサルアクセス」の「補助装置にアクセスできるようにする」のチェックをオンにしてください。
--選択したURLを開く
set cb to the clipboard as record
tell application "System Events"
	tell application process "Safari"
		tell application "Safari" to activate
		delay 0.5
		keystroke "c" using command down
		delay 0.5
		set surl to the clipboard
	end tell
end tell
set the clipboard to cb
if surl contains return then set surl to do shell script "echo " & quoted form of surl & " | sed s/" & return & "//g"
if surl contains " " then set surl to do shell script "echo " & quoted form of surl & " | sed s/" & quoted form of " " & "//g"
if surl contains "://" then
	set AppleScript's text item delimiters to "://"
	set surl to "http://" & text item 2 of surl
else
	set surl to "http://" & surl
end if
tell application "Safari"
	open location surl
end tell

★ 全角のURLを選択して開くアップルスクリプト
全角半角が混在していても可。スクリプトを使用するにあたっては、「システム環境設定」の「ユニバーサルアクセス」の「補助装置にアクセスできるようにする」のチェックをオンにしてください。
set cb to the clipboard as record
tell application "System Events"
	tell application process "Safari"
		tell application "Safari" to activate
		delay 0.5
		keystroke "c" using command down
		delay 0.5
		set zenurl to the clipboard
	end tell
end tell
set the clipboard to cb

set zentext to zenurl
set zentable to " !?#$¥%&@.,:;()[]{}/\_|^`〜’”+−<=>*0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
set hantable to " !?#$\¥%&@.,:;()[]{}/\\_|^`~'\"+-<=>*0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
set hantext to ""
considering case
	repeat with x in zentext
		set y to 1
		set convtext to x as string
		repeat 96 times
			if convtext contains (item y of zentable) then
				set convtext to (item y of hantable) as string
				exit repeat
			end if
			set y to y + 1
		end repeat
		set hantext to hantext & convtext
	end repeat
end considering
set hanurl to hantext

tell application "Safari"
	open location hanurl
end tell

★ Safariのウインドウを整頓するアップルスクリプト
横縦のサイズは2行目の値(ここでは800, 740)、重なったウインドウのずれ幅は11行目の値(ここでは0)です。各自のモニタ環境や好みに合わせて任意に設定してください。
tell application "Safari"
	set {hida, ue, migi, shita} to {0, 22, 800, 740} --横縦のサイズ
	set mado to number of window
	repeat while mado > 0
		if miniaturizable of window mado is false then
			set mado to mado - 1
		else
			if name of window mado is "Downloads" then
				set bounds of window "Downloads" to {0, 600, 300, 750}
				set mado to mado - 1
				set {hida, ue, migi, shita} to {hida + 0, ue + 0, migi + 0, shita + 0} --重なったウインドウのずれ幅
			else
				set bounds of window mado to {hida, ue, migi, shita}
				set mado to mado - 1
				set {hida, ue, migi, shita} to {hida + 0, ue + 0, migi + 0, shita + 0}
			end if
		end if
	end repeat
end tell