Skip to content

Instantly share code, notes, and snippets.

@fbender
Last active March 5, 2024 09:51
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save fbender/090dbbd742ad6a140c44 to your computer and use it in GitHub Desktop.
Save fbender/090dbbd742ad6a140c44 to your computer and use it in GitHub Desktop.
AppleScript to put your Mac to sleep after X minutes, fading out the system volume shortly before sleeping. Can be turned into an app via the "Export" feature of the AppleScript Editor.
-- written by Florian Bender, 2015
-- based on work by Michael Wyszomierski <https://wysz.com/wyszdom/2009/06/simple-sleep-timer-with-applescript/>
-- license: Public Domain / CC0 <http://creativecommons.org/publicdomain/zero/1.0/>
tell application "System Events"
set volumeSettings to get volume settings
set volumeValue to (output volume of volumeSettings)
set fadeTime to 10 -- time in seconds for fade change
set fadeChange to (volumeValue div fadeTime)
-- display alert "Volume: " & volumeValue & ", Delta: " & fadeChange
display dialog "Sleep time (min):" default answer "25"
delay ((text returned of the result) * 60 - 30)
display notification "Going to sleep in 30 sec" with title "Sleep Timer"
delay (30 - fadeTime)
repeat with X from 1 to fadeTime
set volumeValue to (volumeValue - fadeChange)
set volume output volume volumeValue
delay 1
end repeat
sleep -- alternatively: do shell script "pmset sleepnow"
end tell
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment