Conditional logic issues

use AppleScript version "2.4" -- Yosemite (10.10) or later
use scripting additions

set secondsPerWeek to 604800

	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
	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!

Hi Aaron! It looks like the “postpone loop” is based on the logic in the “repeat 2 times” block. It is displaying a message that only offers to postpone, and then delaying for 15 seconds before displaying the same panel again.

It would probably be a good exercise for you, and help you in zeroing in on the right logic, to extract some of this functionality out into functions (handlers as they are called in AppleScript). For example, you could define something liek:

on promptToRebootOrPostone(DialogTitle, DialogText)
		set response to 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")
		return true
	on error
		return false
	end try
end promptToRebootOrPostone

on performReboot()
	display dialog "Would restart!"
	-- tell application "System Events" to restart
end performReboot

if promptToRebootOrPostone(DialogTitle, DialogText) then
end if

With this kind of modular approach you can see it affords little debugging tricks like stubbing out the “reboot” process to not actually reboot, but just let you know it would. I think reorganizing your code with handlers like this will be a good learning experience and also guide you towards understanding the logic better.

Oh and as for the question of how to bundle your icon with the script, I think the right approach is to save your script as an application bundle, which can include its own resources. That would probably be suitable for the long running time this script is likely to have, too.