Skip to content

Instantly share code, notes, and snippets.

@Coro365

Coro365/screen.sh

Created Nov 5, 2018
Embed
What would you like to do?
This script will turn on / off the screen of the Mac or run the screen saver and return screen status
#!/bin/sh
# @(#) This script will turn on / off the screen of the Mac or run the screen saver.
# @(#) Argument:on, off, saver and display_status. Support macOS Mojave version.20181016
if [ -z "$1" ]; then
echo "Argument is missing!"
exit 1
elif [ "$1" == "on" ]; then
caffeinate -u -t 1
elif [ "$1" == "off" ]; then
pmset displaysleepnow
elif [ "$1" == "saver" ]; then
open /System/Library/CoreServices/ScreenSaverEngine.app
elif [ "$1" == "saver_status" ]; then
ps ax|grep [S]creenSaverEngine > /dev/null
if [ "$?" != "0" ] ; then
#echo "Screensaver is disabled."
echo "0"
else
#echo "Screensaver is enabled."
echo "1"
fi
elif [ "$1" == "display_status" ]; then
# 最後のuserの操作からの秒数を返す
IDLETIME=$((`ioreg -c IOHIDSystem | sed -e '/HIDIdleTime/ !{ d' -e 't' -e '}' -e 's/.* = //g' -e 'q'` / 1000000000))
#echo Idle Time is $IDLETIME
if [ "$IDLETIME" -gt "299" ]; then
#echo "User is away."
echo "0"
else
#echo "User is working."
echo "1"
fi
else
echo "Invalid argument!"
exit 1
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment