Is it possible to write an AppleScript to put the display to sleep (which locks the display if the computer is set to lock on sleep)? You can do this from the keyboard by entering ⌃⇧⏏ (shift+control+eject); this leaves all the programs, etc., running, and just turns off the screen.
Share
Edit 2015-08-23: This is possible (from the shell) as of OS X 10.9! Go see user3064009’s answer for the update 🙂
There’s no good way to do this; there’s a SuperUser question about this same thing. Depending on why you want this, however, there’s one workaround I know: activate the screen saver. (This is what they suggest over on SuperUser). You can do this in AppleScript with
launch application id "com.apple.ScreenSaver.Engine", or from the command line by running the application/System/Library/Frameworks/ScreenSaver.framework/Resources/ScreenSaverEngine.app/Contents/MacOS/ScreenSaverEngine. I don’t know whether or not this is technically documented anywhere, but it’s worked for several iterations of the OS now. It may not do exactly what you want—your screen saver may, for instance, be colorful, which isn’t helpful if you want a black screen—but it will be the same in that it does lock the screen if you have that set up.For a nice catalog of other workarounds, check out this MacScripter thread: it documents that
key code EJECT, as there’s no such key code.pmsetto tell the display to go to sleep in one minute, but then you have to wait.