AppleScriptによるiTunesのスリープタイマー
AppleScriptの勉強をかねて、iTunes11でのスリープタイマーアプリケーションを作成してみました。
おヒマな方、AppleScriptに詳しい方には、ぜひお試しいただいて改善点などアドバイスをいただけたら幸いです。
当方、プログラム経験(COBOL,Visual Basic等)はあるものの、AppleScriptは全くの素人です。どんな事でも結構ですので何とぞご指導ください。
なお、iTunes11には「AppleScriptでシャッフルとリピートの情報を取得、設定できない」というバグがあるそうです。こちらにその報告があります。
:iTunes 11 AppleScript bug « Doug's AppleScripts for iTunes
iTunes10.7では問題なく、iTunes11にて発生したバグであることは当方でも確認いたしました。
また、その対応策を考えられた方がいらっしゃいましたので、そちらからサンプルサブルーチンを拝借いたしました。それがこちらに公開されています。
:How to set iTunes 11 in shuffle or repeat mode via applescript ...
このサブルーチンはiTunes11専用です。また、下記ソースコードでは日本語環境用に少々カスタマイズしています。
以下がソースコードです。(先頭のデフォルト値は、各々の好みで変更してください)
set myShuffleList to {"曲別", "アルバム別", "グループ別"}
set myTimerList to {"00:05:00", "00:15:00", "00:30:00", "01:00:00", "01:30:00", "02:00:00"}
set myDelayTime to 10
set myDefaultShuffle to "アルバム別"
set myDefaultPlayList to "お気に入りのラジオ番組"
set myDefaultTrack to "Jazz Wyoming"
set myDefaultTimer to "01:00:00"
try
-- iTunesが起動されているか?if application "iTunes" is not running then
display alert "iTunesが起動されていません:" message "起動しますか?" as warning buttons {"No", "Yes"} default button "No" cancel button "No"
if result is {"No"} then
error number -128
else
tell application "iTunes" to run
end if
end if
tell application "iTunes"
-- Windowを表示
activateset visible of front window to true
-- プレイリストを選択set mySelectPlaylist to (choose from list (name of every playlist as list) with prompt "プレイリストを選択して下さい:" & return & "[キャンセル]で、再生を中止します。" default items myDefaultPlayList)
if mySelectPlaylist is false then
error number -128
else
set myPlaylist to playlist (mySelectPlaylist as string)
revealmyPlaylistend if
-- トラックを選択set mySelectTrack to (choose from list (name of every track of myPlaylist as list) with prompt "トラックを選択して下さい:" & return & "[キャンセル]で、全曲を再生します。" default items myDefaultTrack)
if mySelectTrack is false then
-- プレイリストを再生set myTrack to myPlaylist
set myKind to ""
-- シャッフル再生?set mySelectShuffle to (choose from list myShuffleList with prompt "シャッフル再生をしますか:" & return & "[キャンセル]で、先頭から再生します。" default items myDefaultShuffle)
if mySelectShuffle is false then
set myShuffle to "オフ"
else
set myShuffle to (mySelectShuffle as string)
end if
my setShuffleType(myShuffle)
else
-- トラックを再生set myTrack to track (mySelectTrack as string) of myPlaylist
set myKind to kind of myTrack
revealmyTrackend if
-- タイマーを選択:プレイリスト再生 or ラジオ再生if (myKind is "") or (myKind is "インターネットオーディオストリーム") then
set myTimer to (choose from list myTimerList with prompt "再生時間を選択して下さい:" & return & "[キャンセル]で、再生を中止します。" default items myDefaultTimer)
if myTimer is false then error number -128
else
set myTimer to my retSecondsToTimer(((duration of myTrack) as integer) + 1)
end if
-- 再生開始play myTrack with once
revealcurrent track
-- 再生時間の監視set mySeconds to my retTimerToSeconds(myTimer as string)
repeat (mySeconds div myDelayTime + 1) times
if player state is not playing then exit repeat
delaymyDelayTimeend repeat
repeat with myVolume from 100 to 0 by -10
set sound volume to myVolume
delay 1
end repeat
stopmy setShuffleType("オフ")
set sound volume to 100
set visible of front window to false
end tell
-- エラー出口on error wCaptionnumberwErrNum
if wErrNum = -128 then
-- "Cancelled!!"else
error wCaptionnumberwErrNum
end if
end try
-- HH:MM:SSを秒に変換
on retTimerToSeconds(hhmmss)
set hh to (text from character 1 to character 2 of hhmmss) as integer
set mm to (text from character 4 to character 5 of hhmmss) as integer
set ss to (text from character 7 to character 8 of hhmmss) as integer
return (hh * hours + mm * minutes + ss)
end retTimerToSeconds
-- シャッフル状態の取得
on getShuffleType() -- the return value is a string: Off/By Songs/By Albums/By Groupings
tell application "System Events"
tell process "iTunes"
set menuItems to menu items of menu bar 1's menu bar item "制御"'s menu 1's menu item "シャッフル"'s menu 1
set onOffItemName to name of item 1 of menuItems
end tell
end tell
-- is shuffle offignoring case
if onOffItemName contains "シャッフルをオン" then return "オフ"
end ignoring
-- shuffle is on so find how we are shufflingset currentChoice to "Unknown"
tell application "System Events"
tell process "iTunes"
repeat with i from 2 to count of menuItems
set anItem to item i of menuItems
try
set theResult to value of attribute "AXMenuItemMarkChar" of anItem
if theResult is not "" then
set currentChoice to name of anItem
exit repeat
end if
end try
end repeat
end tell
end tell
return currentChoice
end getShuffleType
-- シャッフルの設定
on setShuffleType(shuffleType) -- shuffleType is a string: Off/By Songs/By Albums/By Groupings
set currentValue to my getShuffleType()
script subs
on toggleShuffleOnOff(OnOff)
tell application "System Events" to perform action "AXPress" of (first menu item of process "iTunes"'s menu bar 1's menu bar item "制御"'s menu 1's menu item "シャッフル"'s menu 1 whose name ends with "シャッフルを" & OnOff)
end toggleShuffleOnOff
on pressBySongs()
tell application "System Events" to perform action "AXPress" of (first menu item of process "iTunes"'s menu bar 1's menu bar item "制御"'s menu 1's menu item "シャッフル"'s menu 1 whose name ends with "曲別")
end pressBySongs
on pressByAlbums()
tell application "System Events" to perform action "AXPress" of (first menu item of process "iTunes"'s menu bar 1's menu bar item "制御"'s menu 1's menu item "シャッフル"'s menu 1 whose name ends with "アルバム別")
end pressByAlbums
on pressByGroupings()
tell application "System Events" to perform action "AXPress" of (first menu item of process "iTunes"'s menu bar 1's menu bar item "制御"'s menu 1's menu item "シャッフル"'s menu 1 whose name ends with "グループ別")
end pressByGroupings
end script
ignoring case
if shuffleType contains "オフ" then -- we have to make sure it's off
if currentValue does not contain "オフ" then subs's toggleShuffleOnOff("オフ")
else
-- make sure it's onif currentValue contains "オフ" then subs's toggleShuffleOnOff("オン")
-- select the shuffle menu item for the typeif shuffleType contains "曲別" and currentValue does not contain "曲別" then
subs's pressBySongs()else if shuffleType contains "アルバム別" and currentValue does not contain "アルバム別" then
subs's pressByAlbums()else if shuffleType contains "グループ別" and currentValue does not contain "グループ別" then
subs's pressByGroupings()end if
end if
end ignoring
end setShuffleType
なお、リピート再生については、実用的でないことから対応しておりません。
MacBook Pro (15-inch Early 2008), OS X Mountain Lion, Core2Duo 2.4GHz , 4G RAM