Skip to content

Instantly share code, notes, and snippets.

@NSBum
Created January 11, 2018 11:46
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 NSBum/4eb1e4171a012f6f7853305c4e637c25 to your computer and use it in GitHub Desktop.
Save NSBum/4eb1e4171a012f6f7853305c4e637c25 to your computer and use it in GitHub Desktop.
Play and fade the current iTunes track
--
-- Created by: Alan Duncan
-- Created on: 2018-01-11
--
-- Copyright (c) 2018 OjisanSeiuchi
-- All Rights Reserved
--
use AppleScript version "2.4" -- Yosemite (10.10) or later
use scripting additions
property minimumVolume : 10
-- initial volume, set by user
global volume0, trackRepeatMode0
on run
set trackDuration to the text returned of (display dialog "Minutes to play track?" default answer "5")
tell application "iTunes"
set volume0 to sound volume
set trackRepeatMode0 to song repeat
-- probe to see that we have a track selected
try
set t to the current track
on error errMsg number errNum
if errNum is -1728 then
display dialog "Please select a track first"
else
display dialog "Unable to begin playback"
end if
return
end try
-- set track repeat 1
set song repeat to one
-- begin playing track
set player position to 0
play
-- calculate the delay between fades
set fadeDelay to (trackDuration * 60) / (volume0 - minimumVolume)
repeat with i from minimumVolume to volume0
delay fadeDelay
set sound volume to (sound volume) - 1
end repeat
-- stop playing
pause
set player position to 0
-- restore iTunes settings
set sound volume to volume0
set song repeat to trackRepeatMode0
quit me
end tell
end run
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment