Skip to content

Instantly share code, notes, and snippets.

@Coro365
Created November 5, 2018 14:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Coro365/1def4815063da8d17e9a47385651d6d1 to your computer and use it in GitHub Desktop.
Save Coro365/1def4815063da8d17e9a47385651d6d1 to your computer and use it in GitHub Desktop.
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