Skip to content

Instantly share code, notes, and snippets.

@tuna-wtl
Last active August 29, 2015 14:08
Show Gist options
  • Save tuna-wtl/632ca818938547d7e14a to your computer and use it in GitHub Desktop.
Save tuna-wtl/632ca818938547d7e14a to your computer and use it in GitHub Desktop.
iTunes のプレイリストに登録されたラジオステーションを一定時間で切り替えながら鳴らす(AppleScript)

使い方

まずラジオステーションを登録したプレイリストを一つ作ってください。
受信できないステーションはスキップします。その場合数秒の時間が空きます。
複数のステーションを登録しておくと良いでしょう。
そのリストの名前を【1】プレイリスト名へ。ダブルクオートの囲みは消さずに。

続く【2】【3】にそれぞれの(最大)音量を。
例えば両方「50」と数値を書き換えたら、システム/iTunes それぞれ
ボリュームスライダの針を中央にしたのと同じことになります。
実際には iTunes 側で最大(100)にし、あとはシステム側で設定すれば
いいのですが。

初期設定はそれだけです。

スクリプトのまま実行しても、アプリケーションとして保存してからでもよし。
起動(実行)直後に、ローテーション時間を聞かれます。「分」で指定・入力してください。

「Set」ボタンで iTunes が起動し、ラジオが流れ始めます。

実行を途中で止めたい場合は、スクリプトを先に(強制)終了させてください。
先に iTunes を止めても、何度も起動しなおしてしまいます (^▽^;)

登録されたステーションの数だけ、実行時間が伸びます。(入力した分数 x ステーション数)
手始めに、短めの時間から試すと良いでしょう…

ある程度「AppleScript エディタ」に慣れた方向けでもあります。
念のため YOUR OWN RISK で。


テスト環境

Mac OS X 10.6( Snow Leopard ) / iTunes 10

Mac OS X 10.9( Marverics ) / iTunes 12

一応、両方で動いております…


動機

好きな音楽はもっぱら外出の時で、仕事とその作業中は環境音楽となれるラジオを
流すのが常なのです。

そしてそのラジオも同じでは飽きるだろうと思い、60分〜あたりをイメージし
切り替えながら流したいと思いました。


改良したいこと

  1. ロジック全般 (・・;) すでにスパゲティ…

  2. シャッフルしたい。

  3. 実行中、フロートウインドウでステータスを見せておきたい(どうやるんだ…)


謝辞

https://gist.github.com/nissuk/1bc41a4eaa611fed89de

http://d.hatena.ne.jp/zariganitosh/20121219/startup_volume_controller

上記サイト様にお世話になりました。お礼申し上げます。

特にフェードイン/アウトの処理は参考になり楽しかったので
機能として取り込ませていただきました。
また、処理メイン部分をすっきりさせたかったので関数に切り出しました。

property playlistName : "your_playlist_name" --【1】プレイリスト名
property systemVolMax : 20 --【2】システム音量(0~100)
property itunesVolMax : 85 --【3】iTunes音量(0~100)
property rotationInterval : missing value
property inputedVal : missing value
property intervalTime : missing value
property ITNS_VOL_MIN : 0
property INTVL_DFLT_UNITS : 60
property INTVL_TIME_ZERO : 0
property BUTTON_TEXT_CANCEL : "Cancel"
property BUTTON_TEXT_SET : "Set"
set ans to (display dialog ¬
"How many minutes?" buttons {BUTTON_TEXT_CANCEL, BUTTON_TEXT_SET} ¬
default button 2 with icon 1 default answer "")
set btnLabel to button returned of ans
if btnLabel is BUTTON_TEXT_SET then
set inputedVal to text returned of ans
if inputedVal is not "" then
try
set intervalTime to (round inputedVal)
do shell script "osascript -e " & quoted form of ¬
"set volume output volume " & systemVolMax
my playRadioStation()
on error
displayCaution()
return
end try
else
displayCaution()
return
end if
else
return
end if
on playRadioStation()
tell application "iTunes"
set sound volume to itunesVolMax
set radioStations to tracks of playlist named playlistName
repeat with station in radioStations
set currentVol to the sound volume
set intervalTime to (inputedVal * INTVL_DFLT_UNITS)
my fadeVolume("out", 2, currentVol, ITNS_VOL_MIN)
repeat 1 times -- # fake loop
try
play station
on error
set intervalTime to INTVL_TIME_ZERO
exit repeat -- # simulated 'continue'
end try
end repeat
my fadeVolume("in", 1, ITNS_VOL_MIN, currentVol)
delay intervalTime
end repeat
end tell
end playRadioStation
-- ----------------------------------------
-- fadeVolume()
-- type(string): 'in'/'out', step(integer):0~100,
-- strtVal(integer):0~100, endVal(integer):0~100
on fadeVolume(type, step, strtVal, endVal)
set fadeStep to 1 -- default value
if type is "in" then
set fadeStep to step
else if type is "out" then
set fadeStep to -step
else
error
return
end if
tell application "iTunes"
repeat with i from strtVal to endVal by fadeStep
set the sound volume to i
delay 0.02
end repeat
end tell
end fadeVolume
on displayCaution()
set msg to "Please input ASCII numeric value."
display alert msg as critical
end displayCaution
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment