Mac shut downs after User logs off

Et Al:
In System Preferences/Parental Controls/DaughterProfile/Time Limits/Bedtime/ I have her set for
School nights: 22:30 to 07:00 and
Weekend: 00:00 to 07:00

Energy Saver/Schedule is set to Shut Down Weekdays at 22:05.

That takes care of the Mac for Sunday night through Friday morning. However, the Energy Saver does not allow a separate set of times for Weekends. I want to use an applescript to run when she logs off that will shut down the Mac about 5 or 10 minutes after. How does Applescript, if it can, be run when she logs off? I know you can do startup items attached to a User’s log in but is there a way for the log off? In addition, I saw a few ways to have Applescript shut it down so that’s no problem, I think.

I already wrote the simple part of the script to check the day of the week .

– shuts down iMac after Daughter has logged off if it is Saturday or Sunday morning

set varDateToday to current date
set varDayOfWeek to weekday of varDateToday

if varDayOfWeek is Saturday or varDayOfWeek is Sunday then
– (the shutdown part of the script goes here)
end if

BG

P.S. Or should this question be more properly in a different Mac forum than this one?

Model: iMac (27-inch, Mid 2011)
AppleScript: ??
Browser: Safari Version 8.0 (10600.1.25.1)
Operating System: Mac OS X (10.1.x)

Unfortunately this isn’t possible as Applescript is confined to a user’s account/workspace and logging out makes that go poof. However, it might be possible with a shell script run as root.

Other than the above it’s relatively easy to shutdown or logout at set times. This script compiled as a

app and run from a user’s startup items would (or should) do it.

Refer to the AppleScript Language Guide for info on Idle Handlers and Date objects.


property wdt : 79500 -- 10:05 PM, for weekdays
property wet : 86340 -- 1 minute till midnight, for weekends

on idle
	set currDate to (current date)
	set ds to day of currDate
	
	if ds < 6 then -- weekday
		set tt to wdt
	else if ds > 5 then -- weekend
		set tt to wet
	end if
	
	set ct to (time of currDate) in seconds
	
	if ct < tt then
		return tt - (time of (current date)) -- in seconds
	else
		display dialog "The computer will shutdown now!" giving up after 10
		ignoring application responses -- for shutdown
			tell application "loginwindow" to «event aevtshut» -- shutdown
			-- tell application "loginwindow" to «event aevtrlgo» -- logout
		end ignoring
	end if
end idle

You could simplify this massively by moving the script to a launch daemon or agent

A daemon has elevated privileges but struggles with user contexts, whereas a launch agent works fine in a user context but has no elevated privileges

More about all of this here https://developer.apple.com/library/mac/documentation/MacOSX/Conceptual/BPSystemStartup/Chapters/CreatingLaunchdJobs.html

The best bit? You can have this run at particular times / dates, avoiding the complexity:

StartCalendarInterval

Minute
45
Hour
13
Day
7

Launch Daemons are powerful, and you can, via a small .sh, run applescripts with them. If you build your Xcode as as an agent rather than app… Well, you start to see the power…