Edit 2015-08-23: It is possible (from the shell) with OS X 10.9! Go see user3064009 update response :-)
There is no good way to do this; there SuperUser question about the same. However, depending on why you want this, there is one way that I know: activate the screen saver. (This is what they offer on SuperUser). You can do this in AppleScript using the launch application id "com.apple.ScreenSaver.Engine"
or from the command line by running the /System/Library/Frameworks/ScreenSaver.framework/Resources/ScreenSaverEngine.app/Contents/MacOS/ScreenSaverEngine
application. I don't know if this is technically documented anywhere, but now it works for several iterations of the OS. It may not do exactly what you want - your screen saver may, for example, be colorful, which is unsafe if you want a black screen, but it will be the same as if it locked the screen if you have this set up .
For a good catalog of other workarounds, check out this MacScripter thread : it documents that
- There is no way to write scripts.
- You cannot tell AppleScript
key code EJECT
as there is no such key code. - You can use
pmset
to show that the display goes into sleep mode in one minute, but then you need to wait. - There is an undocumented IOKit method; there is a mailing list explaining how.
source share