set myTrackCountLimit to 1500
set myPlaylistName to "for Sleep Timer"
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 5
set myDefaultShuffle to "アルバム別"
set myDefaultPlayList to "インターネットラジオ"
set myDefaultGenre to "*"
set myDefaultArtist to "*"
set myDefaultAlbum 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 "Yes" cancel button "No"
--if result is {"No"} then error number -128
tell application "iTunes" to run
end if
tell application "iTunes"
-- Windowを表示
activate
set visible of front window to true
-- 専用プレイリストを作成
set myAllPlaylist to name of every playlist as list
if myPlaylistName is in myAllPlaylist then
set myPlayTrack to playlist (myPlaylistName)
delete every track of myPlayTrack
else
set myPlayTrack to makenewplaylistwith properties {name:myPlaylistName}
end if
revealmyPlayTrack
-- プレイリストを選択:複数選択も可
set myList to {}
repeat with myItem in myAllPlaylist
set myStr to myItem as string
if ((counttracks of playlist (myStr)) > myTrackCountLimit) or (myStr is myPlaylistName) then
else
set myList to myList & {myStr}
end if
end repeat
set mySelect to choose from list myList with prompt "プレイリストを選択して下さい:" & return & "(複数選択も可)" & return & "[キャンセル(esc)]で再生を中止します。" & return & return & "(トラック数が" & myTrackCountLimit & "曲を越えるプレイリストは表示されません)" default items myDefaultPlayList with multiple selections allowed
if mySelect is false then error number -128
-- 専用プレイリストに重複を除外してコピー:リンク切れがあるとここでエラーが発生するが無視する
try
set id_list to {}
repeat with i in mySelect
repeat with j in tracks of playlist (i as string)
if database ID of j is not in id_list then
set id_list to id_list & (database ID of j)
duplicatejtomyPlayTrack
end if
end repeat
end repeat
end try
-- ブックを除外
delete (every track of myPlayTrack whose kind ends with "ブック" or kind is "PDF 書類")
set mytracks to every track of myPlayTrack
-- ジャンルを選択:複数選択も可
set myList to {}
repeat with mytrack in mytracks
set myStr to genre of mytrack
if myStr is not in myList then set myList to myList & {myStr}
end repeat
set myList to my simple_sort(the myList)
set mySeconds to duration of myPlayTrack
if myList is not {} and myList is not {""} and number of myList > 1 then
set mySelect to choose from list myList with prompt "選択合計時間:" & my retSecondsToTimer(mySeconds) & return & return & "ジャンルを選択して下さい:" & return & "(複数選択も可)" & return & "[キャンセル(esc)]ですべてを選択します。" default items myDefaultGenre with multiple selections allowed
if mySelect is not false then
repeat with myItem in myList
if myItem is not in mySelect then
delete (every track of myPlayTrack whose genre is myItem as string)
end if
end repeat
set mytracks to every track of myPlayTrack
end if
end if
-- アーティストを選択:複数選択も可
set myList to {}
repeat with mytrack in mytracks
set myStr to artist of mytrack
if myStr is not in myList then set myList to myList & {myStr}
end repeat
set myList to my simple_sort(the myList)
set mySeconds to duration of myPlayTrack
if myList is not {} and myList is not {""} and number of myList > 1 then
set mySelect to choose from list myList with prompt "選択合計時間:" & my retSecondsToTimer(mySeconds) & return & return & "アーティストを選択して下さい:" & return & "(複数選択も可)" & return & "[キャンセル(esc)]ですべてを選択します。" default items myDefaultArtist with multiple selections allowed
if mySelect is not false then
repeat with myItem in myList
if myItem is not in mySelect then
delete (every track of myPlayTrack whose artist is myItem as string)
end if
end repeat
set mytracks to every track of myPlayTrack
end if
end if
-- アルバムを選択:複数選択も可
set myList to {}
repeat with mytrack in mytracks
set myStr to album of mytrack
if myStr is not in myList then set myList to myList & {myStr}
end repeat
set myList to my simple_sort(the myList)
set mySeconds to duration of myPlayTrack
if myList is not {} and myList is not {""} and number of myList > 1 then
set mySelect to choose from list myList with prompt "選択合計時間:" & my retSecondsToTimer(mySeconds) & return & return & "アルバムを選択して下さい:" & return & "(複数選択も可)" & return & "[キャンセル(esc)]ですべてを選択します。" default items myDefaultAlbum with multiple selections allowed
if mySelect is not false then
repeat with myItem in myList
if myItem is not in mySelect then
delete (every track of myPlayTrack whose album is myItem as string)
end if
end repeat
set mytracks to every track of myPlayTrack
end if
end if
-- トラックを選択:複数選択も可
set myList to {}
repeat with mytrack in mytracks
set myStr to name of mytrack
if myStr is not in myList then set myList to myList & {myStr}
end repeat
set myList to my simple_sort(the myList)
set mySeconds to duration of myPlayTrack
if myList is not {} and myList is not {""} and number of myList > 1 then
set mySelect to choose from list myList with prompt "選択合計時間:" & my retSecondsToTimer(mySeconds) & return & return & "トラックを選択して下さい:" & return & "(複数選択も可)" & return & "[キャンセル(esc)]ですべてを選択します。" default items myDefaultTrack with multiple selections allowed
if mySelect is not false then
repeat with myItem in myList
if myItem is not in mySelect then
delete (every track of myPlayTrack whose name is myItem as string)
end if
end repeat
set mytracks to every track of myPlayTrack
end if
end if
-- 曲数の判定:複数曲の場合、シャッフル再生
my setShuffleType("オフ")
my setRepeatType("切")
set myTrackCount to counttrack of myPlayTrack
if myTrackCount = 0 then error number -128
set mySeconds to duration of myPlayTrack
if myTrackCount > 1 then
set myList to myShuffleList
set mySelect to choose from list myList with prompt "選択合計時間:" & my retSecondsToTimer(mySeconds) & return & return & "シャッフル再生をしますか:" & return & "[キャンセル(esc)]で先頭から再生します。" default items myDefaultShuffle
if mySelect is not false then my setShuffleType(mySelect as string)
end if
-- タイマー選択
set myList to myTimerList
if mySeconds is 0 then
set mySelect to choose from list myList with prompt "再生時間を選択して下さい:" & return & "[キャンセル(esc)]で再生を中止します。" default items myDefaultTimer
if mySelect is false then error number -128
set mySeconds to my retTimerToSeconds(mySelect as string)
else
set myDuration to mySeconds
set mySelect to choose from list myList with prompt "選択合計時間:" & my retSecondsToTimer(mySeconds) & return & return & "再生時間を選択して下さい:" & return & "指定時間リピート再生します。" & return & "[キャンセル(esc)]で全体を1回のみ再生します。" default items myDefaultTimer
if mySelect is not false then
set mySeconds to my retTimerToSeconds(mySelect as string)
if mySeconds > myDuration then my setRepeatType("すべて")
end if
end if
-- 再生開始
playmyPlayTrack with once
set myOldTrack to current track
revealmyOldTrack
-- 再生時間の監視
(*set myRepeat to mySeconds div myDelayTime
if mySeconds mod myDelayTime > 0 then set myRepeat to myRepeat + 1
repeat myRepeat times
if player state is not playing then exit repeat
set myNewTrack to current track
if myNewTrack is not myOldTrack then
set myOldTrack to myNewTrack
reveal myNewTrack
end if
delay myDelayTime
end repeat*)
set sTime to my (current date)
set nTime to sTime
set eTime to sTime + mySeconds
repeat while nTime < eTime
if player state is not playing then exit repeat
set myNewTrack to current track
if myNewTrack is not myOldTrack then
set myOldTrack to myNewTrack
revealmyNewTrack
end if
delaymyDelayTime
set nTime to my (current date)
end repeat
-- フェードアウトして停止
set myVolume to sound volume
if player state is playing then
repeat with v from myVolume to 0 by -1
set sound volume to v
delay 0.1
end repeat
end if
stop
my setShuffleType("オフ")
my setRepeatType("切")
set sound volume to myVolume
set visible of front window to false
--quit
end tell
-- エラー出口
on error wCaptionnumberwErrNum
tell me to activate
if wErrNum = -128 then
-- "Cancelled !!"
display alert "処理は中止されました..." as warning
else
-- "Error Happend !!"
display alert wCaption & return & "Error No:" & wErrNum as warning
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
-- 秒をHH:MM:SSに変換
on retSecondsToTimer(ssNum)
set hhStr to retZeroPaddingText((ssNum div hours) as string, 2)
set mmStr to retZeroPaddingText((ssNum mod hours div minutes) as string, 2)
set ssStr to retZeroPaddingText((ssNum mod hours mod minutes) as string, 2)
return hhStr & ":" & mmStr & ":" & ssStr
end retSecondsToTimer
-- ゼロパディング
on retZeroPaddingText(aNum, aLen)
do shell script "printf '%0" & aLen & "d' " & aNum
end retZeroPaddingText
-- シャッフル状態の取得
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 off
if onOffItemName contains "オン" then return "オフ"
-- shuffle is on so find how we are shuffling
set 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 "") and (theResult is not missing value) 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()
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 contains "シャッフル")
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 contains "曲")
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 contains "アルバム")
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 contains "グループ")
end pressByGroupings
end script
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 on
if currentValue contains "オフ" then subs'stoggleShuffleOnOff()
-- select the shuffle menu item for the type
if shuffleType contains "曲" and currentValue does not contain "曲" then
subs'spressBySongs()
else if shuffleType contains "アルバム" and currentValue does not contain "アルバム" then
subs'spressByAlbums()
else if shuffleType contains "グループ" and currentValue does not contain "グループ" then
subs'spressByGroupings()
end if
end if
end setShuffleType
-- リピート状態の取得
on getRepeatType() -- the return value is a string: Off/All/One
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 currentChoice to "unknown"
repeat with anItem in menuItems
try
set theResult to value of attribute "AXMenuItemMarkChar" of anItem
if (theResult is not "") and (theResult is not missing value) then
set currentChoice to name of anItem
exit repeat
end if
end try
end repeat
end tell
end tell
return currentChoice
end getRepeatType
-- リピートの設定
on setRepeatType(repeatType) -- repeatType is a string: Off/All/One
set currentValue to my getRepeatType()
if currentValue is not repeatType then
tell application "System Events" to tell process "iTunes"'s menu bar 1's menu bar item "制御"'s menu 1's menu item "リピート"'s menu 1
if repeatType is "すべて" then
perform action "AXPress" of menu item "すべて"
else if repeatType is "1 項目" then
perform action "AXPress" of menu item "1 項目"
else
perform action "AXPress" of menu item "切"
end if
end tell
end if
end setRepeatType
-- リストの並べ替え
on simple_sort(my_list)
set the index_list to {}
set the sorted_list to {}
repeat (the number of items in my_list) times
set the low_item to "*"
repeat with i from 1 to (number of items in my_list)
if i is not in the index_list then
set this_item to item i of my_list as text
if the low_item is "*" then
set the low_item to this_item
set the low_item_index to i
else if this_item comes before the low_item then
set the low_item to this_item
set the low_item_index to i
end if
end if
end repeat
set the end of sorted_list to the low_item
set the end of the index_list to the low_item_index
end repeat
return the sorted_list
end simple_sort