I'd like to be able to turn my screen off from command line.
I know there's the keyboard shortcut ctrl+⇧+⏏ but I want to do it from a shell script or remotely.
Is there a way?
I'd like to be able to turn my screen off from command line.
I know there's the keyboard shortcut ctrl+⇧+⏏ but I want to do it from a shell script or remotely.
Is there a way?
In OS X 10.9, you can simply do pmset displaysleepnow
. This will immediately turn off your display without changing any of your settings or putting the entire host to sleep.
Escalated privileges don't seem to be required (at least with recent versions of OS X), but if you get a message about inadequate privileges, you could do sudo pmset displaysleepnow
.
Based on feedback from commenters, this is not available in OS X 10.8 or earlier.
While I haven't been able to find a command that will sleep the display natively, there is an app you download that will do it. There are two options from here.
Install the app to the Applications folder and from Terminal or SSH run open /Applications/Sleep\ Display.app
Right click on the app from the Downloads folder and click "Show Package Contents". Navigate to Contents/MacOS
and copy the sleepdisplay file.
Navigate to /usr/bin
(you can use the ⇧+⌘+G shortcut and type the directory in) and paste the binary file. You will now be able to type the command sleepdisplay
into Terminal or through SSH and the display will immediately go to sleep.
Hope this helps!
The following script will do the job (in Leopard and later), but it must be run with sudo
:
The premise is that pmset
can set a time until display sleep, but the problem is that a value of 0 turns the feature off, rather than setting the delay to zero, and a value of 1 is a full one minute delay. The magic here is that a value of 2^31 seems to be stored as negative zero, which magically functions as "turn the display off immediately".
In Tiger and earlier, a different magic number was needed, because a different bit-sized variable was used internally to store the delay, in minutes, until the display turns off.
This mimics the behavior of control-shift-eject, and can be used on MacBook Airs without an eject key.
#!/bin/bash
original_setting=`/usr/bin/pmset -g | /usr/bin/awk '/displaysleep/ {print $2}'`
magic_number=2147483648
/usr/bin/pmset -a displaysleep $magic_number; sleep 1; /usr/bin/pmset -a displaysleep $original_setting
Here's a simple shell script that will do it.
#!/usr/bin/env bash
open /System/Library/Frameworks/ScreenSaver.framework/Versions/A/Resources/ScreenSaverEngine.app
This will run/start whatever settings you have enabled for Screen Saver on that computer.
on macOS Sierra
pmset displaysleepnow
caffeinate -u -t 1
pmset -g powerstate IODisplayWrangler | tail -1 | cut -c29
results <4 are a sleeping displaya small node HTTP server to set screen status of your Mac remotely: https://github.com/ycardon/switch-api
"echo $[$(pmset -g powerstate IODisplayWrangler | tail -1 | cut -c29) - 4]"
(using this to true/false "is screen on?").
Run the following command to execute a short AppleScript that puts the display to sleep:
osascript -e 'tell application "System Events" to sleep'
The command name suggests that it might put the entire system to sleep under some circumstances, but I could not make that happen in limited testing: I tested it with a shell script I started shortly before executing this command, and that was running for a few minutes until I "awoke" the system. It had continued to print output during the entire time.
Really hope this answer isn't too off the beaten track. My favourite way to sleep, restart, shut down - and most importantly lock - a Mac is using Alfred (the app launcher).
It doesn't require any scripts, knowledge of scripts or use of terminal, which is brilliant.
However, if you specifically WANT to use terminal, this probably isn't the solution for you.
You could use the pmset
command to change the value for displaysleep, something like
pmset -a displaysleep 1
(requires root)
Take a look at the open source github project maclock
By default, it just launches the screensaver, but you can put the display to sleep with:
maclock --display
Under the covers, it is a bash script that is essentially just calling:
pmset displaysleepnow
Well, this is an old one, but it seems there aren't really good answers for this question anywhere.
I've gotten an AppleScript to work, though it requires a third-party, unmaintained, and un-registrable app—Extra Suites. It can be downloaded on the developer's old website.
# Gets the current state of the upper left hot corner, then sets it to sleep display.
tell application "System Events"
tell expose preferences
set givenActivty to get the activity of the top left screen
set the properties of the top left screen corner to {activity:sleep display}
end tell
end tell
# Uses Mouse to Activate upper left hot corner. [Moving directly to {0, 0} does not work
tell application "Extra Suites"
ES move mouse {1, 1}
ES move mouse {0, 0}
end tell
# Restores state of upper left hot corner.
tell application "System Events"
tell expose preferences
delay 1
set the activity of the top left screen corner to givenActivty
end tell
end tell
# Gets rid of Extra Suites nag window.
tell application "Extra Suites"
quit
end tell
I've also incorporated it into an Alfred Workflow.
I did it with sudo shutdown -s now
.
Note that you will need administrator privilege to do this though.
To sleep the display :
pmset displaysleepnow
To check status :
pmset -g powerstate IODisplayWrangler|tail -1|grep -q USEABLE # exit 0 (success) if screen is on
Using Alfred (free at the AppStore) is a great way to put the display to sleep by just writing: "sleep". Doesn't get simpler than that.
Of course Alfred does much more than that, you can also restart, logout, empty trash, lock, shutdown and many more things.
No need extra app, just use tell application "Finder" to sleep
Here you go -
sudo su
pmset displaysleepnow
The app Launchbar works equally well as Alfred. In my case, CMD spacebar then S [Sleep] - simple and fast