Script to do full Shutdown then restart

Some times I have a need to do a full power shutdown and then have the computer restart due to some hardware not initializing properly with a regular restart.

Here is a script I wrote that I keep in the Script Menu…

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

property userName : "Supervisor" -- an account that has admin privileges
property pwd : "**mypassword**"

on run
	restartMac()
end run

on restartMac()
	local wakeTime, h, m, s, tid
	set wakeTime to current date
	set tid to text item delimiters
	set text item delimiters to ":"
	set t to (time of wakeTime) + 20
	set wakeTime to short date string of wakeTime
	set h to t div 3600
	set m to t mod 3600
	set s to m mod 60
	set m to m div 60
	set wakeTime to wakeTime & " " & ({h, m, s} as text)
	set text item delimiters to tid
	try
		do shell script "pmset schedule wakeorpoweron \"" & wakeTime & "\"" user name userName password pwd with administrator privileges -- wake the mac
	on error errMsg
		display alert errMsg
		return false
	end try
	say "Shutting down Mac!"
	try
		tell application "loginwindow" to «event aevtshut»
	end try
	quit
end restartMac

It uses ‘pmset’ to set a power event to power on 20 seconds after it shutdown.

Initially, I turned your script into an application that sat on the desktop, ended up with an endless loop of shutdowns/restarts. Then I read your post more carefully, added the Script Editor to the menu bar, and the script went into the script folder. It now shuts down, but no matter how long I wait, there is no restart. I’m on Monterey 12.7.6 on a 2020 M1 MacBook Pro. Any ideas? And I did add my account and password to the script.

When it tries to shutdown, how many seconds till it actually shuts down?
The script creates a 20 second delay before it restarts. If it takes longer than that to actually shutdown because of many open apps that delay the shutdown, the restart power event will have executed before the Mac was actually off.
If this is the case then you would have to increase the time-delay. Maybe to 30 seconds

Also is your account an admin account?

The shutdown happens almost immediately (I keep no applications open). Soon as the Mac speaks “Shutting down Mac!”, screen goes dark and it’s done. Tried increasing the delay to 30 seconds, no luck. Also tried decreasing it to 10 seconds, still nothing. My account (the only account) is the admin account.

Seems like one of those “nice to have” scripts, but possibly, there’s something on my system that will not allow it to work. Thanks for trying.

Have you tried resetting the pram?

Looked up resetting the pram and found this from Apple: The steps to reset NVRAM (or PRAM) don’t apply to Mac computers with Apple silicon, and aren’t needed on those computers.

But that started me thinking, what if the issue is an Apple Silicon/Intel thing. Tried your script on my 2014 Intel Mac Mini (also running Monterey 12.7.6) and it runs perfectly. I even played with extending the times until restart, and the restart happened pretty close to the settings I entered (as far as I could unscientifically time it by just counting). So it does seem the issue is an Apple Silicon/Intel thing.

Unfortunately I don’t have an M1 or better system to test it on.

Not to worry, it was a fun exerciser, and I learned something at the same time.

On your M1 Mac try this script and post the resulting output of this script

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

property userName : "Supervisor" -- an account that has admin privileges
property pwd : "**PASSWORD**"

on run
	local wakeTime, h, m, s, tid
	set wakeTime to current date
	set tid to text item delimiters
	set text item delimiters to ":"
	set t to (time of wakeTime) + 3600
	set wakeTime to short date string of wakeTime
	set h to t div 3600
	set m to t mod 3600
	set s to m mod 60
	set m to m div 60
	set wakeTime to wakeTime & " " & ({h, m, s} as text)
	set text item delimiters to tid
	try
		do shell script "pmset schedule wakeorpoweron \"" & wakeTime & "\"" user name userName password pwd with administrator privileges -- wake the mac
	on error errMsg
		display alert errMsg
		return false
	end try
	do shell script "pmset -g sched" user name userName password pwd with administrator privileges -- wake the mac
end run

I assumed I was to just run the script (in Script Debugger) rather than from the Script Editor menu, which is what I did. Results posted below.

Scheduled power events:
[0] wake at 08/11/24 04:06:21 by ‘com.apple.alarm.user-visible-Weekly Usage Report’
[1] wakeorpoweron at 08/10/24 18:34:33 by ‘pmset’

OK, so this proves that the power event is actually getting saved.
Try changing the time to 60 seconds like so

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

property userName : "Supervisor" -- an account that has admin privileges
property pwd : "**mypassword**"

on run
	local pnames
	tell application "System Events"
		set pnames to name of application processes whose background only is false
	end tell
	repeat with aProc in pnames
		set aProc to contents of aProc
		if aProc is not in {"Script Editor", "Finder", "loginwindow"} then
			if application aProc is running then
				tell application aProc to quit
				repeat with i from 100 to 0 by -1
					if application aProc is not running then exit repeat
					delay 0.3
				end repeat
				if i = 0 then -- timed out
					say aProc & " refused to Quit!"
					return
				end if
			end if
		end if
	end repeat
	restartMac()
end run

on restartMac()
	local wakeTime, h, m, s, tid
	set wakeTime to current date
	set tid to text item delimiters
	set text item delimiters to ":"
	set t to (time of wakeTime) + 60
	set wakeTime to short date string of wakeTime
	set h to t div 3600
	set m to t mod 3600
	set s to m mod 60
	set m to m div 60
	set wakeTime to wakeTime & " " & ({h, m, s} as text)
	set text item delimiters to tid
	try
		do shell script "pmset schedule wakeorpoweron \"" & wakeTime & "\"" user name userName password pwd with administrator privileges -- wake the mac
	on error errMsg
		display alert errMsg
		return false
	end try
	say "Shutting down Mac!"
	try
		tell application "loginwindow" to «event aevtshut»
	end try
	quit
end restartMac

This newer version will try quiting all open apps first before setting the power event

Added the new script to the Script Editor menu and ran. Shutdown happened, but no restart.

Found a very interesting web site that has much information on pmset here: pmset - Manipulate power management settings on macOS

This web site belongs to a software company that makes a product called Power Manager. While I don’t think I’m all that interested in the product, the information they provided has allowed me to do the following:
pmset -g sched - which lists all schedules
sudo pmset schedule cancelall - which deletes all schedules

I’m sure that none of this is new to you, but I thought you might find the web site that I posted above of interest.

Problem Solved! Went on the Apple Support Community and posted the question as to why no pmset commands were working in Monterey, at least on the M1 MacBook Pro.

Response: Using the Terminal to Schedule Sleep and Wake Times is performed from macOS 13 Ventura and above.

For Monterey: Schedule your Mac notebook to turn on or off - Apple Support (CA)

Apple States: On your Mac, choose Apple menu > System Preferences, click Battery , then click Schedule. Select the options you want to use.

For Ventura: Schedule your Mac to turn on or off in Terminal - Apple Support (CA)

Apple States: In the Terminal app on your Mac, enter a pmset command. See the pmset man page for the list of commands you can use. For example, try any of the following:
◦ pmset -g sched: See the current schedule.
◦ sudo pmset repeat wake M 8:00:00: Schedule your Mac to wake at 8:00 a.m. every Monday.
◦ sudo pmset repeat cancel: Cancel the current schedule.
Press Return.

Can’t believe I totally missed this as many times as I went up on Apple support pages chasing this issue. I’ll be upgrading the MacBook Pro to Ventura tonight. I’ll let you know if your original script now works.

What will remain a mystery is why in Monterey it works on my Intel 2014 Mac mini, but not on the M1 MacBook Pro. I think I can live with that mystery.

Update: Well, that didn’t go quite as planned. Tried updating the MacBook Pro to Ventura, but Apple seems to make things very difficult. First I ended up with the internal SSD being wiped clean. The only option then was to do an internet recovery. The issue with that is that Apple does not allow you to select the OS, so I was forced to go with Sonoma (which I have to admit, is not as bad as I thought). In any event, I’m back up and running, so I’m going to just leave well enough alone.

You should make a Ventura install disk on a USB thumb drive

Went on the “Mr. Macintosh” web site to download the “InstallAssistant.pkg” files, and I now have a USB install disk for the latest versions of Monterey, Ventura and Sonoma. A little too late to help me with my issue of a couple of nights ago, but at least I’m now prepared for future issues.