I had a similar problem with the iMac computer labs that I manage. I searched and searched but didn't find anything so made my own solution. A launch daemon is used to run a script that continuously runs while the computer is on. This is what it does -
Checks to see if the external speakers are selected for output
If so, it turns the volume to zero
Checks to see if system has been idle for whatever amount of time
Shuts down the computer if idle for whatever amount of time
When headphones are plugged in, the volume is not altered at all.
The site says I don't have permission to add attachments so here is the code for com.idle.restart.plist and idle.sh. If you want to recreate the files, use TextEdit. Make sure that it is set to plain text mode, copy the text below and use the correct extensions. It will try to put .txt by default but that will not work. I know it isn't a perfect solution but it does the job pretty well.
Quote: com.idle.restart.plist
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "
http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>Label</key>
<string>com.idle.restart</string>
<key>OnDemand</key>
<false/>
<key>Program</key>
<string>/Library/Management/idle.sh</string>
<key>ProgramArguments</key>
<array>
<string>/Library/Management/idle.sh</string>
</array>
<key>ServiceDescription</key>
<string>Restarts the computer after it has been idle for 20 minutes</string>
</dict>
</plist>
Quote: idle.sh
#!/bin/sh
#infinite loop while system is on
#turn off speakers if enabled
while [ 1 ]
do
OUTPUT=`audiodevice output`
if [ "$OUTPUT" = "Internal Speakers" ] ; then
osascript -e "set Volume 0"
fi
#idle reboot
idleTime=`ioreg -c IOHIDSystem | awk '/HIDIdleTime/ {print int($NF/1000000000); exit}'`
#If the idle time is more than x minutes (x*60 seconds) then restart the machine
if [ "$idleTime" -ge 1800 ]; then
INPUT=$( osascript \
-e 'tell application "System Events"' \
-e 'activate' \
-e 'set dialog_result to display dialog "Computer is idle and will restart soon" with title "Idle Computer" with icon caution buttons {"Continue Working"} default button 1 giving up after (60)' \
-e 'end tell' \
-e 'tell application "Terminal"' \
-e 'activate' \
-e 'end tell' \
-e 'get button returned of dialog_Result'
)
if [[ $INPUT != "Continue Working" ]]; then
shutdown -h now
fi
fi
# the longer the sleep time, the less cpu this will use. 3 seconds uses about 1% it seems
sleep 3
done
- Download "audiodevice" command line app from this site: http://whoshacks.blogspot.com/2009/01/change-audio-devices-via-shell-script.html
- Copy that command line app to a PATH directory (ie /bin/). Use terminal with root access.
- Place the com.idle.restart.plist file in /Library/LaunchDaemons.
- Make sure permissions for com.idle.restart are -rw-r--r-- (should be the same as the other permissions in that folder.) Also that the owner is root and group is wheel.
- Place idle.sh in /Library/Management
If you don't know how to do the file operations above, google it. Most of this you can easily do with the command line. If you don't want certain parts of the script to be active, just comment out the line(s) with a # symbol.
Hope it is useful to someone.
Edited by skier354 - 12/20/12 at 11:56am