Skip to content

Instantly share code, notes, and snippets.

@jasonkeene
Last active December 12, 2024 17:10
Show Gist options
  • Save jasonkeene/8c782ce83044ff94a35d61e4279b25b9 to your computer and use it in GitHub Desktop.
Save jasonkeene/8c782ce83044ff94a35d61e4279b25b9 to your computer and use it in GitHub Desktop.
Late Night Shutdown Prompt

Prompt yourself every 5 minutes after 7 PM if you really want to still be using your computer or not. This helps to avoid late night device use. You can dismiss the dialog every 5 minutes. If you don't dismiss it within 15 seconds your computer will shutdown.

Installation

  1. Add the shutdown.applescript to your local bin folder ~/.local/bin/shutdown.applescript. You may have to create this directory first with mkdir -p ~/.local/bin. Make the script executable with chmod +x ~/.local/bin/shutdown.applescript.
  2. Allow your user to use sudo to invoke shutdown without prompting for a password by adding a sudoers file:
echo "$USER ALL=NOPASSWD: /sbin/shutdown" | sudo tee /etc/sudoers.d/shutdown > /dev/null
  1. Install the crontab via crontab -e.
# Run every 5 mintes from 7 pm until 4 am
*/5 19-23,0-3 * * * ~/.local/bin/shutdown.applescript
#!/usr/bin/osascript
try
set userResponse to display dialog ¬
"Do you want to shut down your Mac?" ¬
buttons {"Cancel", "Shutdown"} default button "Cancel" ¬
giving up after 15
on error errMsg number errNum
if errNum = -128 then
-- User canceled the dialog
return
else
-- Some other error occurred, re-raise it
error errMsg number errNum
end if
end try
if gave up of userResponse then
-- User didn't respond within 15 seconds, proceed to shut down
do shell script "sudo /sbin/shutdown -h now"
else if button returned of userResponse is "Shutdown" then
-- User clicked Shutdown
do shell script "sudo /sbin/shutdown -h now"
end if
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment