use AppleScript version "2.4" -- Yosemite (10.10) or later
use scripting additions
set secondsPerWeek to 604800
try
set currentUnixTime to ((do shell script "date '+%s'") as number)
set bootUnixTime to ((do shell script "sysctl -n kern.boottime | awk -F 'sec = |, usec' '{ print $2; exit }'") as number)
set timeSinceLastBoot to (currentUnixTime - bootUnixTime as number)
end try
set DialogText to "Per company policy your device must be rebooted weekly.
Please save your work and restart."
set dialogTitle to "Restart in Progress"
set PostponeText to "You can delay your reboot for 2 hours, up to a maximum of 4 hours total."
set TimesUpText to "Your time is up. You must reboot the machine now."
#if timeSinceLastBoot > secondsPerWeek then
try
display dialog DialogText buttons {"Postpone", "Reboot"} cancel button 1 default button 2 with title dialogTitle with icon file (("Macintosh HD:Users:me:Pictures:") & "myco.icns")
tell application "System Events" to restart
on error
repeat 2 times
display dialog PostponeText buttons {"Postpone" } with title dialogTitle with icon file (("Macintosh HD:Users:me:Pictures:") & "myco.icns")
delay 15
end repeat
display dialog TimesUpText buttons {"Restart"} with title dialogTitle with icon file (("Macintosh HD:Users:me:Pictures:") & "myco.icns") giving up after 20 with button #{"Restart"}
tell application "System Events" to restart
end try
#end if
Full disclosure: I’m totally new to AppleScript, and have cobbled this together by searches and asking for help elsewhere.
The Goal: an AppleScript that checks to see if system uptime is >1w and then (if uptime >1w) force the user to reboot the machine within 4 hours. Give them the opportunity to reboot now, or delay twice for a maximum total of 4 hours, and if they decide to postpone all the way, give them 3 mins before the machine reboots.
Right now, if a user decides to Postpone, they’re stuck in a Postpone loop. I tried copying the logic from the first part of the “try” block into the error block, but when I clicked “Reboot” it just exited.
I’m trying to fix it so that the user can choose to postpone or reboot at any point.
The other thing is I’m not sure how to get the icon file include in the compiled script. I’m just pointing to it on the filesystem, at present.
Thank you for your help!