Posts Tagged applescript

Mac lab auto-logout fix

Quick fix to a small Mac lab management headache.  It’s very useful to set the “Log out users after XX minutes of inactivity” setting in Workgroup Manager; however the built-in automatic logout dialog is often “too polite”.  If the user has left something open like Firefox, OpenOffice or even just TextEdit, the auto-logout will get canceled, leaving the computer logged in with no-one at the helm until some random person sits down on the previous user’s account!  BAD!

Found this post on MacOSXHints.com: http://www.macosxhints.com/dlfiles/idle_time_check.txt that showed a scriptable way to get the system idle time from a script.  Unfortunately the perl code in the example didn’t work for me, but since my perl-fu isn’t so hot I changed it a little bit.

This is a combination of shell script to check the idle time and run whatever you want to run, and AppleScript to display a nice little “final warning” dialog.  Default timeout of 30mins, overridable via either a simple setting in the script, or it’ll automatically check whatever setting you put in Workgroup Manager for the inactivity timeout and work off of that.

#!/bin/bash
# Detect Mac OS Idle and force-autologout
# Regular auto-logout is too "polite" and won't quit if user accidentally left
# any apps open.  This is more authoritive.
# Carl Chan 100707

####### Options #######
# Timeouts all in seconds
# Max idle time before forcing logout
maxidle_default=1800
# Final warning dialog box timeout
warning_timeout=10

function do_on_maxidle() {
	give_final_warning 2>&1 >>/dev/null
	if [ $? -eq 1 ]; then
		exit 0
	else
		killall -9 loginwindow
	fi
}
#######################

function give_final_warning(){
	osascript <<EOF
		tell application "System Events"
			display dialog "Final logout warning: This workstation has been idle for more than $(($maxidle/60)) minutes. Click cancel to stop auto-logout." buttons "Cancel" giving up after $warning_timeout with icon 0
		end tell
EOF
}

# Override script default if AutoLogOutDelay is set from Directory Server
if [ -e "/Library/Managed\ Preferences/.GlobalPreferences" ]; then
	maxidle_managed=$(defaults read /Library/Managed\ Preferences/.GlobalPreferences | grep "AutoLogOutDelay" | sed "s/[^0-9]//g")
fi

if [ -z "$maxidle_managed" ]; then
	maxidle="$maxidle_default"
else
	maxidle="$maxidle_managed"
fi

# Really cool way for finding idle time
# Modified from http://www.macosxhints.com/dlfiles/idle_time_check.txt
# perl expression didn't work and i don't know perl well enough to fix it
# so stuck with slightly clunkier method, but should be ok since this script
# isn't running continuously in the background
idletime=$(($(ioreg -c IOHIDSystem | grep -m1 HIDIdleTime | sed 's/[^0-9]//g')/1000000000))

# Check idle time and force logout if idletime > maxidle
if [ "$idletime" -gt "$maxidle" ]; then
	do_on_maxidle
fi

exit 0
AutoLogout.sh (1kb)

Here’s a little package with the whole thing wrapped in a pkg, includes a User Daemon to run it every minute.

ProudlyGeeky-AutoLogout (12kb)

Hope that comes in handy for someone else =)

, , , ,

No Comments