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を表示


activate

set 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)


revealmyPlaylist

end 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


revealmyTrack

end 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


delaymyDelayTime

end repeat


repeat with myVolume from 100 to 0 by -10

set sound volume to myVolume

delay 1

end repeat


stop

my 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 off

ignoring case

if onOffItemName contains "シャッフルをオン" then return "オフ"

end ignoring



-- 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 "" 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 on

if currentValue contains "オフ" then subs's toggleShuffleOnOff("オン")



-- select the shuffle menu item for the type

if 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

投稿日 2013/05/02 19:47

返信
返信: 47

2013/05/03 07:59 Pajerow への返信

エラー出口を修正しました。


-- エラー出口

on error wCaptionnumberwErrNum

tell my applicasion 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

先頭の「tellmyapplicasiontoactivate」は、ダイアログがiTunesウィンドウの後ろに隠れてしまうことに対する処置です。

こんなんでいいんでしょうか?

2013/05/03 08:24 Pajerow への返信

ああ〜うっかりしてました。スリープタイマーなので、アラートダイアログじゃ意味ないです。眠ってしまってからダイアログが出ても気がつかない...。失礼しました。ここはエラーが起きたら単純に return とするのが良いです。エラーログをテキストで書き出しても良いのですが、そこまでは必要ないでしょう。

2013/05/04 22:48 Pajerow への返信

機能追加をしてみました。

  1. ジャンル、アーティスト、アルバムで絞り込みができるようにしました。
  2. ブラウザウィンドウの表示が、再生中の曲に追従するようにしました。
    プレイリストウィンドウをグリッド表示にしておくとなかなか面白いです。


だいぶん弄りましたので、再度、全体を掲載します。

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 "Jazz"

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 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

set myPlaylist to playlist (mySelectPlaylist as string)

set trks to every track of myPlaylist as list



-- ジャンルを選択

set myGetGenre to {}

set myGenre to ""

repeat with trk in trks

set gnr to genre of trk

if gnr is not in myGetGenre then set myGetGenre to myGetGenre & {gnr}

end repeat

if myGetGenre is not {} and myGetGenre is not {""} then

set mySelectGenre to (choose from list myGetGenre with prompt "ジャンルを選択して下さい:" & return & "[キャンセル]で、すべてを選択します。" default items myDefaultGenre)

else

set mySelectGenre to false

end if

if mySelectGenre is false then

else

set myGenre to mySelectGenre as string

set trks to every track of myPlaylist whose genre is myGenre

end if



-- アーティストを選択

set myGetArtist to {}

set myArtist to ""

repeat with trk in trks

set art to artist of trk

if art is not in myGetArtist then set myGetArtist to myGetArtist & {art}

end repeat

if myGetArtist is not {} and myGetArtist is not {""} then

set mySelectArtist to (choose from list myGetArtist with prompt "アーティストを選択して下さい:" & return & "[キャンセル]で、すべてを選択します。" default items myDefaultArtist)

else

set mySelectArtist to false

end if

if mySelectArtist is false then

if myGenre is "" then

else

set trks to every track of myPlaylist whose genre is myGenre

end if

else

set myArtist to mySelectArtist as string

if myGenre is "" then

set trks to every track of myPlaylist whose artist is myArtist

else

set trks to every track of myPlaylist whose genre is myGenre and artist is myArtist

end if

end if



-- アルバムを選択

set myGetAlbum to {}

set myAlbum to ""

repeat with trk in trks

set alb to album of trk

if alb is not in myGetAlbum then set myGetAlbum to myGetAlbum & {alb}

end repeat

if myGetAlbum is not {} and myGetAlbum is not {""} then

set mySelectAlbum to (choose from list myGetAlbum with prompt "アルバムを選択して下さい:" & return & "[キャンセル]で、すべてを選択します。" default items myDefaultAlbum)

else

set mySelectAlbum to false

end if

if mySelectAlbum is false then

if myArtist is "" then

if myGenre is "" then

else

set trks to every track of myPlaylist whose genre is myGenre

end if

else

if myGenre is "" then

set trks to every track of myPlaylist whose artist is myArtist

else

set trks to every track of myPlaylist whose genre is myGenre and artist is myArtist

end if

end if

else

set myAlbum to mySelectAlbum as string

if myArtist is "" then

if myGenre is "" then

set trks to every track of myPlaylist whose album is mymyalbum

else

set trks to every track of myPlaylist whose genre is myGenre and album is myAlbum

end if

else

if myGenre is "" then

set trks to every track of myPlaylist whose artist is myArtist and album is myAlbum

else

set trks to every track of myPlaylist whose genre is myGenre and artist is myArtist and album is myAlbum

end if

end if

end if



-- トラックを選択

set myGetTrack to {}

set myTrack to ""

repeat with trk in trks

set tk to name of trk

if tk is not in myGetTrack then set myGetTrack to myGetTrack & {tk}

end repeat

if myGetTrack is not {} and myGetTrack is not {""} then

set mySelecttrack to (choose from list myGetTrack with prompt "トラックを選択して下さい:" & return & "[キャンセル]で、すべてを選択します。" default items myDefaultTrack)

else

set mySelecttrack to false

end if

if mySelecttrack is false then

if myAlbum is "" then

if myArtist is "" then

if myGenre is "" then

else

set trks to every track of myPlaylist whose genre is myGenre

end if

else

if myGenre is "" then

set trks to every track of myPlaylist whose artist is myArtist

else

set trks to every track of myPlaylist whose genre is myGenre and artist is myArtist

end if

end if

else

if myArtist is "" then

if myGenre is "" then

set trks to every track of myPlaylist whose album is myAlbum

else

set trks to every track of myPlaylist whose genre is myGenre and album is myAlbum

end if

else

if myGenre is "" then

set trks to every track of myPlaylist whose artist is myArtist and album is myAlbum

else

set trks to every track of myPlaylist whose genre is myGenre and artist is myArtist and album is myAlbum

end if

end if

end if

else

set myTrack to mySelecttrack as string

if myAlbum is "" then

if myArtist is "" then

if myGenre is "" then

set trks to every track of myPlaylist whose name is myTrack

else

set trks to every track of myPlaylist whose genre is myGenre and name is myTrack

end if

else

if myGenre is "" then

set trks to every track of myPlaylist whose artist is myArtist and name is myTrack

else

set trks to every track of myPlaylist whose genre is myGenre and artist is myArtist and name is myTrack

end if

end if

else

if myArtist is "" then

if myGenre is "" then

set trks to every track of myPlaylist whose album is myAlbum and name is myTrack

else

set trks to every track of myPlaylist whose genre is myGenre and album is myAlbum and name is myTrack

end if

else

if myGenre is "" then

set trks to every track of myPlaylist whose artist is myArtist and album is myAlbum and name is myTrack

else

set trks to every track of myPlaylist whose genre is myGenre and artist is myArtist and album is myAlbum and name is myTrack

end if

end if

end if

end if



-- display dialog "Playlist" & tab & ":" & name of myPlaylist & return & "Genre" & tab & ":" & myGenre & return & "Artist" & tab & ":" & myArtist & return & "Album" & tab & ":" & myAlbum & return & "Track" & tab & ":" & myTrack buttons {"OK"} default button {"OK"}



-- 専用プレイリストを作成

set allPlaylist to name of every playlist as list

if myPlaylistName is not in allPlaylist then

set pList to makenewplaylistwith properties {name:myPlaylistName}

else

set pList to playlist (myPlaylistName)

end if

delete every track of pList

repeat with trk in trks


duplicatetrktopList

end repeat


set trkNum to count track of pList

if trkNum = 0 then

error number -128

end if

set myKind to ""

set mySeconds to 0

if trkNum = 1 then

set myKind to kind of item 1 of trks

if myKind is not "インターネットオーディオストリーム" then set mySeconds to ((duration of item 1 of trks) as integer) + 1

end if



-- シャッフル再生

if trkNum > 1 then

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)

end if



-- タイマー選択:複数再生 or ラジオ再生

if (trkNum > 1) 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

end if

else

set myTimer to my retSecondsToTimer(mySeconds)

end if



-- 再生開始

play pList with once

set myOldTrack to current track


revealmyOldTrack



-- 再生時間の監視

set mySeconds to my retTimerToSeconds(myTimer as string)

repeat (mySeconds div myDelayTime + 1) times

if player state is not playing then exit repeat

if current track is not myOldTrack then

set myOldTrack to current track


revealmyOldTrack

end if


delaymyDelayTime

end repeat



-- フェードアウトして停止

set myVolume to sound volume

if player state is playing then

repeat while sound volume > 0

set sound volume to sound volume - 1

delay 0.1

end repeat

end if


stop

my setShuffleType("オフ")

set sound volume to myVolume

set visible of front window to false


end tell



-- エラー出口

on error wCaptionnumberwErrNum

tell my applicasion 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 "" 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


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's toggleShuffleOnOff("オン")



-- select the shuffle menu item for the type

if 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 setShuffleType


2013/05/05 02:31 Hiro__S への返信

Hiro.Sさん、こんにちは。

お休み(ですか?)のところ、早速のご指導ありがとうございました。


typoの2件早速修正いたしました。


「if文」のすごいネストになってしまっているので、全てのパターンを確認しきれていませんでした。(汗) このあたりをもう少し改善できないか、今頭をひねっている所です。(^^;

2013/05/05 16:40 Pajerow への返信

ロジックを簡略化してみました。「if文」がかなり減りスッキリしました。

サブルーチンに変更はありません。


ただし、曲数の多いプレイリストを選択すると大きな負荷がかかるようになってしまいました。(3,000曲ほどのプレイリストを処理してみると、途中でiTunesが異常停止してしまいました)

そのため、曲数の多いプレイリストは選択できないように制限をかけました。(とりあえず500曲を越えるプレイリストを選択一覧から除外するようにしています。マシン能力にもよりますが、これぐらいが限界かと...)

この辺りを、諸氏においてご確認いただけたら幸いです。


set myPlaylistName to "for Sleep Timer"

set myTrackCountLimit to 500

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 "Jazz"

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 allPlaylist to name of every playlist as list

if myPlaylistName is not in allPlaylist then

set pList to makenewplaylistwith properties {name:myPlaylistName}

else

set pList to playlist (myPlaylistName)

end if

revealpList

delete every track of pList



-- プレイリストを選択

set myGetPlaylist to {}

set ListName to name of every playlist

repeat with i in ListName

set l to i as string

if ((count tracks of playlist (l)) > myTrackCountLimit) or (l is myPlaylistName) or (l is "ブック") then

else

set myGetPlaylist to myGetPlaylist & {l}

end if

end repeat

set mySelectPlaylist to choose from list myGetPlaylist with prompt "プレイリストを選択して下さい:" & return & "[キャンセル(esc)]で、再生を中止します。" & return & return & "(トラック数が" & myTrackCountLimit & "曲を越えるプレイリストは表示されません)" default items myDefaultPlayList

if mySelectPlaylist is false then error number -128

set myPlaylist to playlist (mySelectPlaylist as string)

set trks to every track of myPlaylist as list

repeat with trk in trks -- ここに凄い負荷がかかる!!

duplicate trk to pList

end repeat



-- ジャンルを選択

set myGetGenre to {}

repeat with trk in trks

set gnr to genre of trk

if gnr is not in myGetGenre then set myGetGenre to myGetGenre & {gnr}

end repeat

if myGetGenre is not {} and myGetGenre is not {""} then

set mySelectGenre to (choose from list myGetGenre with prompt "ジャンルを選択して下さい:" & return & "[キャンセル(esc)]で、すべてを選択します。" default items myDefaultGenre)

else

set mySelectGenre to false

end if

if mySelectGenre is not false then

delete (every track of pList whose genre is not mySelectGenre as string)

set trks to every track of pList

end if



-- アーティストを選択

set myGetArtist to {}

repeat with trk in trks

set art to artist of trk

if art is not in myGetArtist then set myGetArtist to myGetArtist & {art}

end repeat

if myGetArtist is not {} and myGetArtist is not {""} then

set mySelectArtist to (choose from list myGetArtist with prompt "アーティストを選択して下さい:" & return & "[キャンセル(esc)]で、すべてを選択します。" default items myDefaultArtist)

else

set mySelectArtist to false

end if

if mySelectArtist is not false then

delete (every track of pList whose artist is not mySelectArtist as string)

set trks to every track of pList

end if



-- アルバムを選択

set myGetAlbum to {}

repeat with trk in trks

set alb to album of trk

if alb is not in myGetAlbum then set myGetAlbum to myGetAlbum & {alb}

end repeat

if myGetAlbum is not {} and myGetAlbum is not {""} then

set mySelectAlbum to (choose from list myGetAlbum with prompt "アルバムを選択して下さい:" & return & "[キャンセル(esc)]で、すべてを選択します。" default items myDefaultAlbum)

else

set mySelectAlbum to false

end if

if mySelectAlbum is not false then

delete (every track of pList whose album is not mySelectAlbum as string)

set trks to every track of pList

end if



-- トラックを選択

set myGetTrack to {}

repeat with trk in trks

set tk to name of trk

if tk is not in myGetTrack then set myGetTrack to myGetTrack & {tk}

end repeat

if myGetTrack is not {} and myGetTrack is not {""} then

set mySelectTrack to (choose from list myGetTrack with prompt "トラックを選択して下さい:" & return & "[キャンセル(esc)]で、すべてを選択します。" default items myDefaultTrack)

else

set mySelectTrack to false

end if

if mySelectTrack is not false then

delete (every track of pList whose name is not mySelectTrack as string)

set trks to every track of pList

end if



-- 曲数の判定

set trkNum to count track of pList

if trkNum = 0 then error number -128

set mySeconds to 0

if trkNum = 1 then

if kind of item 1 of trks is not "インターネットオーディオストリーム" then set mySeconds to ((duration of item 1 of trks) as integer) + 1

end if



-- シャッフル再生:複数曲の再生

if trkNum > 1 then

set mySelectShuffle to (choose from list myShuffleList with prompt "シャッフル再生をしますか:" & return & "[キャンセル(esc)]で、先頭から再生します。" default items myDefaultShuffle)

if mySelectShuffle is false then

set myShuffle to "オフ"

else

set myShuffle to (mySelectShuffle as string)

end if

my setShuffleType(myShuffle)

end if



-- タイマー選択:複数曲の再生 or ラジオの再生

if mySeconds > 0 then

set myTimer to my retSecondsToTimer(mySeconds)

else

set myTimer to (choose from list myTimerList with prompt "再生時間を選択して下さい:" & return & "[キャンセル(esc)]で、再生を中止します。" default items myDefaultTimer)

if myTimer is false then error number -128

end if



-- 再生開始

play pList with once

set myOldTrack to current track

revealmyOldTrack



-- 再生時間の監視

set mySeconds to my retTimerToSeconds(myTimer as string)

repeat (mySeconds div myDelayTime + 1) 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

revealmyNewTrack

end if

delaymyDelayTime

end repeat



-- フェードアウトして停止

set myVolume to sound volume

if player state is playing then

repeat while sound volume > 0

set sound volume to sound volume - 1

delay 0.1

end repeat

end if

stop

my setShuffleType("オフ")

set sound volume to myVolume

set visible of front window to false


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 "" 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


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's toggleShuffleOnOff("オン")



-- select the shuffle menu item for the type

if 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 setShuffleType


ちなみに、下記の私の環境では、500曲のプレイリストを処理するのに約43秒かかりました。

2013/05/05 21:13 Pajerow への返信

ちなみに、下記の私の環境では、500曲のプレイリストを処理するのに約43秒かかりました。


私の環境では 412 曲 で 9 秒でした。この辺は環境依存かもしれませんね。

・Intel Core 2 Duo 2.4 GHz、メモリ 4 GB

・Mac OS X 10.6.8


ただし、これとは別に実験用のコードを書いて 1000 曲程度で repeat 文を使うと処理内容によってはハングする場合がありました。(ハングしたりしなかったり結構不安定)


AppleScript の repeat 文が (他のスクリプト言語と比較して) 遅いのは確かなのですが、この場合は iTunes が遅いのかも知れません。よくわかりませんけど...。


いずれにせよ、このスクリプトの目的は「スリープタイマー」なので、「500 曲」とか「2 時間」とかはオーバースペックのような気もします。500 曲だと翌日のスリープタイマーになっちゃいます。(笑)


なので、リミットは「50 ~ 100 曲」程度で良いのではないでしょうか?それなら安定して動くと思います。ただそうすると、リミット越えのプレイリストが表示されなくなるので、その辺は一工夫する必要があるかなと。

2013/05/05 21:54 Hiro__S への返信

ちなみに、下記の私の環境では、500曲のプレイリストを処理するのに約43秒かかりました。


私の環境では 412 曲 で 9 秒でした。この辺は環境依存かもしれませんね。

・Intel Core 2 Duo 2.4 GHz、メモリ 4 GB

・Mac OS X 10.6.8

うっ、ずいぶん違いますね。CPUとメモリは、私も同じですが...

iTunesライブラリのあるHDDなどの環境の差でしょうか?

私の場合は、iTunesライブラリはMBPの内蔵HDD上にあります。


ただし、これとは別に実験用のコードを書いて 1000 曲程度で repeat 文を使うと処理内容によってはハングする場合がありました。(ハングしたりしなかったり結構不安定)

これは、うちでも発生しました。(ハングしたりしなかったり)


いずれにせよ、このスクリプトの目的は「スリープタイマー」なので、「500 曲」とか「2 時間」とかはオーバースペックのような気もします。500 曲だと翌日のスリープタイマーになっちゃいます。(笑)


なので、リミットは「50 ~ 100 曲」程度で良いのではないでしょうか?それなら安定して動くと思います。ただそうすると、リミット越えのプレイリストが表示されなくなるので、その辺は一工夫する必要があるかなと。

そうですね。


例えば、プレイリストでなく「ミュージック」全体から絞り込みをしたいときなど、今の仕様では数千曲をプレイリストにコピーしようとしてしまうので、そういうことは現実的ではありません。

あらかじめ絞り込んだプレイリストを用意しておく必要がありますね。


まぁ、あくまでも「スリープタイマー」だということで...これで良しとします。

2013/05/05 22:32 Pajerow への返信

うっ、ずいぶん違いますね。CPUとメモリは、私も同じですが...

iTunesライブラリのあるHDDなどの環境の差でしょうか?

私の場合は、iTunesライブラリはMBPの内蔵HDD上にあります。


私の場合もライブラリは内蔵 HDD (別パーティション) です。しかもおそ〜いやつ。それか iTunes の登録曲数でしょうか?全部で 940 曲しか入ってません。1000 曲程度を目安にしてそれを超えたものは外付け HDD に二軍落ちとしてます。で、聞きたくなったら 再度 iTunes に登録。あとは OS の違い?謎ですね。


まぁ、あくまでも「スリープタイマー」だということで...これで良しとします。


私もそう思います。リミットは各自好みの値にすれば良いだけですからね。とても楽しいスクリプトになりましたね。

2013/05/06 04:30 Hiro__S への返信

Hiro.S さんによる書き込み:


それか iTunes の登録曲数でしょうか?全部で 940 曲しか入ってません。1000 曲程度を目安にしてそれを超えたものは外付け HDD に二軍落ちとしてます。で、聞きたくなったら 再度 iTunes に登録。あとは OS の違い?謎ですね。

登録曲数は私の場合、3,500曲ぐらい入ってます。これは大きな違いですね。

OSの違いについて、こちらでも同じライブラリを10.6.8でテストしてみましたがやっぱり遅いです。


それと前のコメントにありました

ただし、これとは別に実験用のコードを書いて 1000 曲程度で repeat 文を使うと処理内容によってはハングする場合がありました。(ハングしたりしなかったり結構不安定)

についてですが、

スクリプトのまま実行すると確かに不安定ですが、アプリケーションとして実行するとハングしないような気がするのですが... また実行速度も若干速く感じますが、そんな事ってあるんでしょうか?

2013/05/06 05:48 Pajerow への返信

登録曲数でしょうかね?まあこの辺の追求はやめときましょう。深みにはまりそう。


スクリプトのまま実行すると確かに不安定ですが、アプリケーションとして実行するとハングしないような気がするのですが... また実行速度も若干速く感じますが、そんな事ってあるんでしょうか?


AppleScript Editor からの実行の場合は、「イベントログの履歴」があるので、そのせいで遅くなるような気もしますが...。正直言って私も分かりません。


スクリプト形式/スクリプトバンドル形式とアプリケーション形式の速度に関しては、多くの場合、前者が速いと思います。これは立ち上がりの時間の問題。ただ、本件に関してはアプリケーション。難しいですね...。


いずれにせよ、AppleScript は大量のデータ処理はあまり得意ではないので、処理件数を減らすというのが無難なところかと思います。本件の場合は myTrackCountLimit の値を小さくする...で良いかと思います。

2013/05/08 19:47 Pajerow への返信

さらに手を加えてみました。

もうそろそろ弄るところは思いつかなくなってきましたので、再アップしておきます。


変更点

  1. コードの簡略化と変数名の統一。
  2. 曲データに「リンク切れ」があったときの異常終了を無視する。
  3. プレイリスト、ジャンル、アーティスト、アルバムの「複数選択」を可能とする。
  4. プレイリストに「ブック」が含まれていた時それを除外する。
  5. プレイリストの時間がタイマー指定時間より短い時はタイマー指定時間まで「リピート再生」する。


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 "Jazz"

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 ((count tracks of playlist (myStr)) > myTrackCountLimit) or (myStr is myPlaylistName) or (myStr is "ブック") 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

repeat with myItem in mySelect

set mytracks to every track of playlist (myItem as string)

repeat with mytrack in mytracks

duplicatemytracktomyPlayTrack

end repeat

end repeat

set mytracks to every track of myPlayTrack

end try



-- ブックを除外

if "ブック" is in myAllPlaylist then

set myBooks to name of every track of playlist ("ブック") as list

repeat with myBook in myBooks

delete (every track of myPlayTrack whose name is myBook as string)

end repeat

set mytracks to every track of myPlayTrack

end if



-- ジャンルを選択:複数選択も可

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

if myList is not {} and myList is not {""} and number of myList > 1 then

set mySelect to choose from list myList with prompt "ジャンルを選択して下さい:" & 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

if myList is not {} and myList is not {""} and number of myList > 1 then

set mySelect to choose from list myList with prompt "アーティストを選択して下さい:" & 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

if myList is not {} and myList is not {""} and number of myList > 1 then

set mySelect to choose from list myList with prompt "アルバムを選択して下さい:" & 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

if myList is not {} and myList is not {""} and number of myList > 1 then

set mySelect to choose from list myList with prompt "トラックを選択して下さい:" & 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



-- 曲数の判定:複数曲の場合、シャッフル再生

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 "シャッフル再生をしますか:" & return & "[キャンセル(esc)]で先頭から再生します。" default items myDefaultShuffle

if mySelect is false then

my setShuffleType("オフ")

else

my setShuffleType(mySelect as string)

end if

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 & "[キャンセル(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

end if



-- 再生開始

playmyPlayTrack with once

set myOldTrack to current track

revealmyOldTrack



-- 再生時間の監視

repeat (mySeconds div myDelayTime + 1) 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

revealmyNewTrack

end if

  delaymyDelayTime

end repeat



-- フェードアウトして停止

set myVolume to sound volume

if player state is playing then

repeat while sound volume > 0

set sound volume to sound volume - 1

delay 0.1

end repeat

end if

stop

my setShuffleType("オフ")

my setRepeatType("")

set sound volume to myVolume

set visible of front window to false


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 "" 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


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's toggleShuffleOnOff("オン")



-- select the shuffle menu item for the type

if 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 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 "" 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


2013/05/09 03:01 Pajerow への返信

あれ? myTrackCountLimit の値を大きくしたんですか?このままだと、私の環境ではハングします。AppleScript Editor 起動直後にこのスクリプトを実行すると 100% ハング...。


アプリケーション形式だとちゃんと動きますが、プレイリストの作成が極端に遅くなります。リミットは 50 でも良いぐらいです。1 曲 4 分で計 200 分。曲数が多いと設定だけで 10 分ぐらいかかっちゃいます。(笑)


それと、ライブラリ選択 → 複数選択...とすると、myTrackCountLimit が決壊。曲数分のプレイリストを作ってしまいます。

このスレッドはシステム、またはAppleコミュニティチームによってロックされました。 問題解決の参考になる情報であれば、どの投稿にでも投票いただけます。またコミュニティで他の回答を検索することもできます。

AppleScriptによるiTunesのスリープタイマー

Apple サポートコミュニティへようこそ
Apple ユーザ同士でお使いの製品について助け合うフォーラムです。Apple Account を使ってご参加ください。