Skip to content

Instantly share code, notes, and snippets.

@nissuk
Last active August 29, 2015 14:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nissuk/1bc41a4eaa611fed89de to your computer and use it in GitHub Desktop.
Save nissuk/1bc41a4eaa611fed89de to your computer and use it in GitHub Desktop.
AppleScript: iTunesの曲を指定時間ずつ再生(レーティング考慮)
-- iTunesの曲を指定時間ずつ再生(レーティング考慮)
-- see:
-- http://detail.chiebukuro.yahoo.co.jp/qa/question_detail/q1081882590 (base script)
-- https://discussions.apple.com/thread/2274705 (fade)
-- 標準の再生時間(秒)
property zapTime : 30
-- レーティングを考慮するか(trueの場合下記のratingToAdditionalTimeにもとづいて時間を追加)
property ratingAdditionalTimeEnable : true
-- 擬似クロスフェード再生をするか
property fadeEnable : true
-- レーティングから下記のとおり追加秒を決定します。
-- { ★0: 0秒(★3と同じ), ★1: -20秒, ★2: -10秒, ★3: 0秒, ★4: 30秒, ★5: 60秒 }
on ratingToAdditionalTime(rating)
if rating = 0 then
set star to 3
set adjustedRating to star * 20
else
set star to rating / 20
set adjustedRating to rating
end if
if star < 3 then
set additionalTime to (star - 3) * 10
else
set additionalTime to (star - 3) * 30
end if
return additionalTime
end ratingToAdditionalTime
-- 擬似クロスフェードしながら次の曲を再生します。
on playNextTrackWithFade()
tell application "iTunes"
-- ボリュームを取得
set currentVolume to the sound volume
-- フェードアウト(ボリュームを徐々に下げる)
repeat with i from currentVolume to 0 by -1
set the sound volume to i
delay 0.02
end repeat
-- 次の曲を再生
play (next track)
-- フェードイン(ボリュームを徐々に上げる)
repeat with i from 0 to currentVolume by 1
set the sound volume to i
delay 0.02
end repeat
end tell
end playNextTrackWithFade
-- メイン処理です。
on main()
tell application "iTunes"
try
play
on error
set myMsg to "曲の再生に失敗しました。"
display dialog myMsg buttons {"終了"} default button 1 with icon 0
return
end try
repeat
-- 再生時間を確保
-- (単純にdelay ZapTimeだと曲を切り替えたあともdelayが残ったり不便なので
-- 1秒ずつdelayしながら再生時間を確認)
set i to 0
repeat
if ratingAdditionalTimeEnable then
-- レーティングにもとづいて追加時間を取得
set additionalTime to my ratingToAdditionalTime(rating of current track)
set zapTotalTime to zapTime + additionalTime
else
set zapTotalTime to zapTime
end if
set i to player position
log i & zapTotalTime
-- 曲の再生時間が指定時間を超えた場合repeatを終了
if i ≥ zapTotalTime then
exit repeat
end if
delay 1
end repeat
-- 次の曲を再生
if fadeEnable then
my playNextTrackWithFade()
else
play (next track)
end if
end repeat
end tell
end main
my main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment