(* インターネットラジオで再生中の曲名をWeb検索する iTunes用 AppleScript
*
* 動作要件
* ・Mac OS X 10.5 以上 (Mac OS X 10.5.8 で動作確認済み)
* ・iTunes 9 以上 (iTunes 9.1 で動作確認済み)
* ※iTunes 7 では動かないので注意!
*
* 保存
* ・AppleScript の場合: スクリプトバンドル形式か、スクリプト形式
*
* 使い方
* ・スクリプトを実行し、ダイアログに従う
*
*)
on run
-- dialog text
set msg1 to "Web 検索してもよろしいですか?"
set msg2 to "検索エンジンを選択:"
set ttl1 to "Radio Info Search"
try
-- iTunes is running?
if application "iTunes" is not running then return
-- get current stream title
tell application "iTunes"
set stream_title to current stream title
if stream_title is missing value then return
end tell
-- set search keyword
set str_input to display_dialog(msg1, stream_title, ttl1)
-- url encode
set myKeyU to url_encode(str_input, "UTF8-MAC", "UTF8")
-- list items
set x01 to "Google ja"
set y01 to "http://www.google.co.jp/search?hl=ja&q=" & myKeyU
set x02 to "Amazon JP - Google"
set y02 to "http://www.google.co.jp/search?hl=ja&q=" & myKeyU & "&as_sitesearch=www.amazon.co.jp"
set x03 to "YouTube"
set y03 to "http://www.youtube.com/results?search_query=" & myKeyU
-- list
set ary to {{x01, y01}, {x02, y02}, {x03, y03}}
-- choose from list and get num
set num to get_num(ary, msg2)
-- go!
open location (item 2 of item num of ary)
on error err_msg
return err_msg
end try
end run
-- subroutine: display_dialog()
on display_dialog(msg1, ans1, ttl1)
repeat
display dialog msg1 default answer ans1 with title ttl1
get text returned of result
if result is not "" then
return result
end if
end repeat
end display_dialog
-- subroutine: convert text encoding and url encode
on url_encode(str, conv_from, conv_to)
do shell script ("
printf '%s' " & quoted form of str & " |
iconv -c -f " & conv_from & " -t " & conv_to & " |
php -R 'print rawurlencode($argn);'
")
end url_encode
-- subroutine: return item number selected in list-dialog
on get_num(ary, msg)
set ary_new to {}
repeat with f in ary
copy (item 1 of f) to end of ary_new
end repeat
tell application "System Events"
activate
choose from list ary_new with prompt msg default items item 1 of ary_new
if result is false then
error number -128
else
set res to result as text
end if
end tell
set c to 1
repeat with i in ary_new
if contents of i is res then
return c
end if
set c to c + 1
end repeat
end get_num