Take the 2-minute tour ×
Ask Different is a question and answer site for power users of Apple hardware and software. It's 100% free, no registration required.

I'm trying to find out whether there is any kind of equivalent command line one-liner which replicates the exact identical functionality of the 'Lock Screen' menu option available via the Keychain app in OS X 10.6

I'm aware of the all the 'usual' solutions (sleep/screensaver preferences to require a password on reactivation) but these are not suitable to me.

I'm also aware of this option:

/System/Library/CoreServices/Menu\ Extras/user.menu/Contents/Resources/CGSession -suspend

which is also not suitable for me.

The perfect functionality is the 'Lock Screen' option however I have not yet found an elegant way to call it.

At the moment, I am calling via Automator, which isn't elegant!

I have read much about this issue and my understanding is that no-one has yet found the 'holy grail' but I was wondering whether that might have changed.

share|improve this question
    
What do you call with Automator ? –  Matthieu Riegler Jan 26 '13 at 13:37
    
Sorry, I should have been more clear... I have recorded a 'Watch Me Do' in Automator (tied to a system keyboard shortcut) which, literally, moves the mouse to the menu option and selects it. It works, but it's slow and not at all elegant! –  user40433 Jan 26 '13 at 13:47
2  
What is wrong with setting the Security preferences to require a password when the screen saver is active and then using a hot corner to activate the screen saver? That is fast and elegant, but you said you didn't like that solution. –  Mark Jan 26 '13 at 13:57
3  
It's a pragmatic suggestion and one I've tried on and off over many years of usage but the nature of my work patterns means that this gets in the way >80% of the time. I need the ability to respond (physically) to engineering requests and emergencies and immediately lock the screen but equally have remote access to the box whilst it also maintains all of its existing connections and background tasks. –  user40433 Jan 26 '13 at 15:57
1  
Re: "where is the Lock Screen menu in Keychain" Open Keychain... preferences... show in menu bar! Re: "what does it do?" It locks the screen! Beyond that, I think I have explained my requirements (and my aversion to 'solutions' that some folk believe meet my requirements, but don't) quite thoroughly. –  user40433 Jan 30 '13 at 13:35
show 3 more comments

14 Answers 14

I use Alfred →

enter image description here

The universal hotkey allows me to activate it using a keyboard.

share|improve this answer
add comment

Set the preferences to lock the screen when the screen saver is active.

Security & Privacy Preference Pane

Then you can use this terminal command to start the screen saver.

/System/Library/Frameworks/ScreenSaver.framework/Resources/ScreenSaverEngine.app/Contents/MacOS/ScreenSaverEngine

As far as I can tell this is exactly what the Keychain menu item does.

share|improve this answer
    
Thanks you for your comment but this is what I've battled with for too long and am trying to find an alternative to. –  user40433 Jan 26 '13 at 16:01
1  
Just for clarity and completeness, I should that the above does indeed start the screensaver but it does not do what the 'Lock Screen' menu function offers. –  user40433 Jan 26 '13 at 16:06
1  
Then you need to be more specific as to what exactly you are looking for. –  Mark Jan 26 '13 at 16:17
    
Hmmm... not sure I can be any more specific or succinct then the opening paragraph in my original question! –  user40433 Jan 26 '13 at 16:23
1  
I had a chance to test on 10.6.8. On that OS the monitor is turned of and the session is locked. I found a programatic way to do this, but it is not built in. –  Mark Jan 29 '13 at 14:05
show 4 more comments

I was also looking for a solution for this. Today I just had some time to play around and found a way to programmatically actually call the functionality from the keychain menu plugin. This solution works perfectly as long as Apple doesn't change the relevant parts in the keychain menu plugin.

#import <objc/runtime.h>
#import <Foundation/Foundation.h>

int main () {
    NSBundle *bundle = [NSBundle bundleWithPath:@"/Applications/Utilities/Keychain Access.app/Contents/Resources/Keychain.menu"];

    Class principalClass = [bundle principalClass];

    id instance = [[principalClass alloc] init];

    [instance _lockScreenMenuHit:NULL];

    return 0;
}

Save the above code in a file called main.m. Compile the file in Terminal with:

clang -framework Foundation main.m -o lockscreen

Execute the program by typing:

./lockscreen
share|improve this answer
    
Could you elaborate on how to use this? Do I copy-paste this into an Automator workflow? Or make an Applescript out of it? As it stands now it's hard to do something with your answer. –  Bart Arondson Mar 10 at 1:09
    
This is a simple Objective-C Program. Copy it into a file called main.m. Then run 'clang -framework Foundation main.m -o lockscreen' to compile the program. './lockscreen' runs the program and locks your screen. –  jnk Mar 10 at 1:19
    
Tried it, works as described. The compilation generates one warning instance method '-_lockScreenMenuHit:' not found, but the lockscreen application works nonetheless. However, when doubleclicked it opens a Terminal window before it locks the screen. After unlocking you have to close the window manually. Is there a way to close that window automatically? –  Bart Arondson Mar 10 at 10:11
    
I'm aware of the warning. The program is more like a proof of concept than a well-thought-out program. it would be possible to make a full mac os application from this executable, that would prevent the terminal being opened. but on the other hand if you wanted to click on something to lock the screen, why not use the keychain menu plugin itself? i intended this program to be called by some other process (e.g. launchbar). that way no terminal is spawned. –  jnk Mar 10 at 10:44
add comment

I think the answer to your question is "No, there isn't a command line way to do this."

Because this is a menu bar item, it's not something that you can access easily using Keyboard Maestro or another similar tool.

However, if you are not averse to a solution using a 3rd party app, QuickLock will let you do this.

It's a free (donations accepted) app which will let you assign a keyboard shortcut to lock the screen. There's also a menu bar item which you can click to lock the screen.

The app will let you set a password (separate from your account password).

It does not require the use of password with screensaver, it's all completely separate.

You can see a video of it in action at http://www.youtube.com/watch?v=SBHwykPB19o

p.s. when the screen is locked, type your password to unlock it. I was confused because I expected to see a password field. There is none. You just type the password.

p.p.s. I've only used this for a few minutes, so there may be some other issues with it I'm not aware of, but it seems to fit the bill.

share|improve this answer
    
Thanks for your contribution... although not what I set out looking for (I love open source and DIY methods!) it does look very interesting and appears to offer the functionality that I need, albeit in a 3rd party app. I'll look into it more closely. Thanks! –  user40433 Jan 29 '13 at 22:30
    
I'd love to find a way to do it too, but I don't think there is one. –  TJ Luoma Jan 30 '13 at 15:07
add comment

The Mac is not designed to provide you with such an option. However, this article explains a workaround using QuickSilver. While I can't understand why you don't like the solution Apple provides natively, I do wish you luck on finding a solution.

share|improve this answer
add comment

@Bart Arondson answer goes into using some key codes to navigate the Keychain Lock Screen menu.

All the key codes can be bypassed using GUI Scripting to Actually hit the menu items directly.

Using key codes to navigate menus can be hit and miss. And as much as I do not like GUI scripting because it can also be hit and miss imho it is less hit and miss in this case.

This applescript code:

tell application "System Events" to tell process "SystemUIServer" to click (first menu item of menu 1 of ((click (first menu bar item whose description is "Keychain menu extra")) of menu bar 1) whose title is "Lock Screen")

Can be used in a Automator 'Run Applescript' Action in a Automator Service Workflow.

enter image description here

You then give the service a Keyboard shortcut. In the usual way in The Keyboard Shortcuts.

enter image description here

The Service:

enter image description here

share|improve this answer
add comment

I use BetterTouchTool for that purpose and assigned a keyboard shortcut to "Switch to login screen" (which is lock screen): doesn't fulfill the command line requirement but may be helpful to you anyway.

keyboard shortcut in BetterTouchTool to lock screen

share|improve this answer
    
Thanks for your help and suggestion. Along with QuickLock mentioned above by @TJLuoma BetterTouchTool (although not the method I set out to find) does appear to come very close to my functional objectives. I will look more closely at this... thanks. –  user40433 Jan 31 '13 at 20:48
add comment

I'm not quite sure I understand your issue here. If you require a password on screensaver, it will not stop processes from running or block you from remotely accessing your Mac. I have this option set (using ControlPlane to activate it at work and deactivate it at home) and I have never once had the system refuse to respond to a Remote Desktop, VNC, or SSH connection. In all cases where GUI access is required I just need to input the password; SSH uses key pair so no password is requested. If you don't want apps to sleep, then make sure the system power settings are such that it will not enter system sleep before you return. Just set the sleep timeout to a long delay. If you just don't want to be bothered to enter a password when you are sitting in front of the computer and working, then increase the screensaver delay to a longer time before it activates, or add a 1 min delay between screensaver and password prompt so that you have a chance to catch it before it locks you out.

I use ctrl-shift-eject to lock the screen. It's fast, simple, and it works. This is the built-in way to lock the screen. No one is likely to come up with an alternative simply because replicating the functions already baked in to the OS is a little too redundant for anyone to put a lot of time into pursuing.

As far as I can tell there is no "lock screen" option in Keychain in 10.8 anyway, so your preferred solution is not going to exist if you ever upgrade. Edit: Didn't notice this in Keychain preferences; yes, it is still there in 10.8.

If you are using Automator for this can't you just package the script as an app and assign a hotkey combo or leave it in the Dock for quick access?

share|improve this answer
add comment

Just like the question asker I was looking for a way to use the Keychains Lock Screen in a fast way.

Combining the knowledge from an old Mac OS X hint and some Applescripting I came up with the following:

First follow the steps from the linked Mac OS X hint to add a keyboard shortcut for Keychains Lock Screen functionality.

For me (on 10.8.4) this didn't solve the problem as I had to press ctrl+F8 to shift focus to the status bar, press down to open the Keychain menu bar, and then press ctrl+F9 to lock the screen. I've automated this process with the following Applescript:

# Press ^F8
tell application "System Events"
    key code 100 using control down
end tell

# Press "down"
tell application "System Events"
    key code 125
end tell

# Press ^F9
tell application "System Events"
    key code 101 using control down
end tell

Open Automator, create a new Service, add a Run Applescript module, copy-paste the above code and save the Automator Service with a name you desire. Now go to Keyboard Shortcuts in the System Preferences and add a keyboard shortcut for the freshly created Automator Service. I set mine to ctrl+cmd+L.

Now press the keyboard shortcut in any app and your screen will be locked instantly.

For those wondering why not just go with other methods, here's my opinion on them:

  • Fast user switching/lock screen
    With this method I get logged out of my corporate WPA2 network. This is not good as re-connecting sometimes takes ages.

  • Screensaver + password
    Replicates the functionality but this means that if my laptop is inactive for some time while I'm reading a paper next to it, the screen will be locked when I want to use it again. This is undesirable. Coupling this method with a hot-corner has the side effect that unexperienced Mac users might trigger the hot-corner by accident leading to screen lock. I also sometimes hit a hot-corner by accident which would stall my workflow considerably.

  • 3rd party apps
    These are all fine, but why run a 3rd part app if it's natively available in the OS?

share|improve this answer
add comment

Set the preferences to lock the screen when the screen saver is active.

Security & Privacy Preference Pane

Then set a "Hot Corner" to start the screen saver:

Screen Saver Preference Pane

Now just move the mouse to the upper Right corner to activate the screen saver and lock the screen.

share|improve this answer
1  
Thank you Mark. I'm very familiar with all this functionality but it's not the solution I am seeking. The Lock Screen functionality is perfect for me but the means of invocation is cluncky and it is the latter that I am trying to address. –  user40433 Jan 26 '13 at 16:03
add comment

I use the Screensaver password setting with a keystroke (Ctrl+Shift+Eject) on 10.6.8, and from what I can see, it does the same as the Lock Screen item from the Keychain Access task bar icon.

I know that this is similar to some of the other answers, but instead of the "hot corner", it uses a keystroke, which is more convenient for me.

This solution is described in more detail here, that's where I got it from: http://osxdaily.com/2011/01/17/lock-screen-mac/

Have been using this for several years now without problems.

share|improve this answer
1  
Thanks but, unfortunately this requires that the option to "require screensaver when returning from sleep or screensaver" option sis checked and, as mentioned earlier, this is not something I wish to have engaged. –  user40433 Jan 30 '13 at 13:32
add comment

Well, just combine two of the above answers. Run this command:

open -a /System/Library/Frameworks/ScreenSaver.framework//Versions/A/Resources/ScreenSaverEngine.app

After having set the Security & Privacy preferences to ask for a password immediately. Make an alias for your shell if you want to make the command easier to execute.

share|improve this answer
1  
Yes but, as mentioned above, I don't wish to have the Security & Privacy preferences to always require a password immediately. This is why the Lock Screen command functionality (specifically) is so cool - it provides the level of security I require on a completely ad hoc basis. –  user40433 Feb 1 '13 at 22:12
add comment

FWIW, I understand your situation and have been looking for a good solution to this myself. I ran across this thread because I've been searching for a somewhat faster method than what I eventually came up with. I came up with something that, although not exactly what I'd hoped, works and is an acceptable compromise. I'm using the same kind of GUI scripting commands as @markhunte shows (slightly different structure, but same idea) but instead of just placing it all in an Automator service workflow, I separated it into an .app bundle built with Platypus. I did this because in my travels and testing I've found that Automator services run very slowly under certain conditions. If your Mac is under any kind of stress, i.e. a disk scan running, A/V software doing something, Mail pulling down email, etc., the OS seems to push services to the back of the line. This has the affect of it taking several seconds before the workflow runs (sometimes) and on occasion even several seconds before the GUI commands in the service get run. Quite annoying if you;re trying to "quickly" lock your screen and it takes 10 seconds to do it!

Having a standalone "app" also has the advantage that you can use literally dozens of different 3rd party apps to run it, or just use Spotlight or an Automator service to launch the app. Any of them work. The fastest I've found is an old utility called Spark, that miraculously still works even in 10.8.4. Don't ask me how since it was last developed foe 10.4! Spark can be found here - Spark

In addition to all this, I combatted the issue of GUI commands sometimes running slow by bundling in cocoaDialog and calling it first when the app runs to show a small floating bubble in the middle of the screen indicating a Lock Screen is happening. Usually the lock happens right after, but if there is a delay, this at least gives some visual "feedback" that you pressed the right keys. I also made it self healing - if "Enable Access to Assistive Devices" is not on, it alerts you with a dialog and lets you enable it right away. It will also make sure the Keychain Access Menu is showing in the menu bar since this is a requirement or the GUI scripting flat out fails.

share|improve this answer
add comment

Ctrl-Shift-⏏ Eject (on older Macs),
Ctrl-Shift-⌽ Power (on newer macs)

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.