Wow. Didn’t take long for Snow Leopard to render my previous hint on how to lock your Mac’s screen utterly obsolete.
This is one of the things I love most about Mac OS X: Apple just keeps adding features that often eliminate the need for third party solutions to simple problems. In this particular case, we’re going to use a combination of Automator, a terminal command, Snow Leopard’s new improved Services and a system-wide keyboard shortcut to enable a hotkey that will suspend the currently logged in user session and display the Login Window as though you had chosen the “Login Window…” option from the Fast User Switching menu extra.
I know, enough with the chatter, get on with the tutorial! Just follow these steps and you’ll be locking your Mac running Mac OS X 10.6 Snow Leopard like it’s running Windows XP! Wait, did I just write that? Shudder. Sorry, I think I just threw up a little bit in my mouth. Here’s the tutorial:
- Open Automator (in your Applications folder) and choose Service from the list of templates provided and click the Choose button.
- In the left hand column under Library, select Utilities.
- In the second column, drag “Run Shell Script” to the right hand pane.
- At the top of the right hand pane where you dragged the Run Shell Script action, click on the menu next to “Service receives” and choose “no input”.
- Copy and paste the following Terminal command into the empty text area of the Run Shell Script action:
/System/Library/CoreServices/Menu\ Extras/User.menu/Contents/Resources/CGSession -suspendThe entire command should be entered on a single line and note there is a space after “/Menu\”.
- Choose File–>Save, and give the new service a meaningful name like “Lock Computer” that will appear in the Services menu. Once you’ve done that, you can go to the Services menu (located in the current application menu, next to the Apple menu) and your newly created service should appear there.
- Next open System Preferences –> Keyboard –> Keyboard Shortcuts and select Services in the left column.
- Scroll down to the bottom and under the General category, you should see your newly created service listed there. Select it, then Double-click close to the right side of the selected line to reveal a field where you can enter a custom keyboard shortcut. Enter an easy to remember but unique keyboard shortcut (I decided to go with ctrl+option+command+L), and then quit System Preferences.
That’s it! Go ahead and test your keyboard shortcut!
Welcome back! As you should have just seen, pressing the assigned keyboard shortcut fast user switches to the login screen leaving all your programs running in the background waiting for you to log back in again. If you’re more mouse inclined, you can of course also lock your screen by choosing your Automator action from the Services menu. All accomplished with the awesome customization tools included with Mac OS X 10.6 Snow Leopard!



Great hint and nice modification. As to the Windows XP reference, well, it had to be said (sorry you had to experience the revolting side effects though.
Instead of invoking fast user switching, I suggest:
/System/Library/Frameworks/ScreenSaver.framework/Resources/ScreenSaverEngine.app/Contents/MacOS/ScreenSaverEnginewhich starts the screensaver. You do lock your computer when the screensaver starts, right?
I prefer to not use the screensaver for a few reasons:
a) If you do share the computer with other users, they can’t use the computer using Fast User Switching if you’ve locked it with the screensaver.
b) The screensaver is less secure because if the screensaver engine crashes, your computer will likely not be locked anymore.
c) You have to unlock the computer to restart or shutdown your computer, whereas the login screen gives you these options.
d) You don’t get the cool rotating cube effect when activating the screensaver as you do when activating the login screen.
Given the above, really what benefit is there to using the screensaver engine over the login screen?
madgunde:
Your point ‘a’ is incorrect. There is a switch user button on the password dialog when you lock with the screensaver. Pressing it will bring up the login window, which allows user switching.
Point ‘b’ is also incorrect. If the screensaver crashes, you get a Black Screen of Death. The computer will become unusable. The password dialog that appears with a locked screensaver is part of the loginwindow process, which the screen saver will invoke on a normal exit. If the screen saver crashes, however, it becomes impossible to get the login window up. This was not an infrequent problem in Leopard, but I haven’t experienced it much in SL.
Your point ‘c’ is misinformed a bit. When you hit the Switch User button on a password dialog brought up from a locked screen saver, it brings you to the normal login window. The restart and shut down options will be available, but you will need administrator access to do either if other users are logged on.
Your point ‘d’ is likewise affected by these cascading errors. The switch user button from a locked screen saver will indeed have the rotating cube effect.
Finally, and I’m really not trying to be a dick here, but this whole article was somewhat superfluous and unnecessary. In Snow Leopard, the key combo Control-Shift-Eject is built in to lock the screen.
Thanks for the corrections Dan. Your final conclusion is incorrect however:
“Finally, and I’m really not trying to be a dick here, but this whole article was somewhat superfluous and unnecessary. In Snow Leopard, the key combo Control-Shift-Eject is built in to lock the screen.”
The article is superfluous only if you WANT to have to enter your password every time the screensaver engages. A lot of people don’t. I for instance don’t want my computer to lock when I’m at home, only when I’m at work. Using the screensaver password doesn’t give me a choice without changing settings back and forth between work and home, which is inconvenient. The solution I’m using in this tip allows me to easily lock my computer only when I want to while retaining the ability to still have the screensaver activate when the computer is idle.
In short, there’s no reason why Apple should make the screensaver and screen lock features one and the same. They are two distinct functions that should be able to be activated exclusive of each other.
If you want different settings by location, you might want to check out MarcoPolo, a little menuitem app that can do things based on your computer’s environment. You add rules and it uses those rules to determine with a point-rated accuracy which of your locations you’re at, and set the certainty level required to switch to a location. It then runs a set of scripts when entering or leaving a location.
Very nice… Can’t believe Apple is dealing with this issue so recklessly… this should come as a default option.
…and as a reply to Bradley Wight… if you do, as I do, prefer to come back from sleep and screen saver without having to log back in, invoking Fast User Switching is the way to go! Otherwise, at least for my use, this little trick would be absolutely useless.
Here’s what I wanted:
- sleep without having to log back in
- screen saver without having to log back in
- a selective shortcut to lock before sleeping (which this trick provides me)
When using the fast user switching, do all my programs like IM, downloads and etc keep running? Cause that’s the benefit from locking and not directly sleeping my laptop. I have some doubts about that, because colleagues saw that when locking my screen this way I went offline in Bonjour. Maybe it’s specific to the protocol or to Adium, I don’t know. But I hope that doesn’t count for the downloads or other running processes as well (like video processing).
Yes they do keep running. IM programs should have a preference to select whether they set you as away or offline when you use fast user switching. iChat does. You may also have to set your network settings to not disconnect when logging out if you have a DSL or dialup connection where the Mac uses PPP or PPPoE to get it’s Internet IP address. Same goes for if you use a wireless connection.
Please see this previous article comment for more details about iChat and the correct network settings to work with fast user switching.
madgunde:
here’s a question -> is there any similar script I could use to make a shortcut to LOCK + SLEEP? I’d really like that option. (keep in mind I don’t want to lock at every sleep.)
Thanks
@Kyle
Just add the following command below the first one in the “Run Shell Script” Automator action:
osascript -e 'tell application "System Events" to sleep'This will execute the applescript code within the single quotes to put the system to sleep.
WOW!!!! Thank you! You are a rockstar!
Do have any idea how many people in other forums/blogs are looking for this?
I’m glad I happened to find yours!
Thank you for this great tip…
You’re welcome. Feel free to post links to this article on said forums to drive some traffic my way.
Not sure why you want a Service to put your Mac to sleep. Why not use the key combination – option+command+Eject.
If you read the comments, you’ll see he wanted a solution to Lock AND sleep. Just hitting Option+Command+Eject will only sleep your computer but not lock it. So the solution I provided allows him to accomplish both with one keystroke rather than two.
This is just what I’d been looking for — Winswitch (which does this and a few other things) stopped working in Snow L.
Awesome! I’ve been looking for a way to do this for 3 years.
Thank You!
Why not just press the built-in screen-lock shortcut: shift-ctrl-eject?
Because we want to see the animation when the desktop is turning to to the login screen.
What if my keyboard doesn’t have an eject key? I tried F12, no luck. I had to use the Services trick.
Great! Build in with SnowLeopard, no more 3rd party tools =)
Now it would be great – pushing the buttons and the animation executes right away -like directly when clicking fast user switch at menu.
But – better then nothing =)
Thanks
Thank you very much. I have been looking for this corollary to the very much used (by me at least) shortcut in Windows XP since I switched to Mac in 2007 with 10.4 Tiger. Now all I need to have everything I use for work in XP is UltraEdit.
Ultraedit for the mac appears to be on the way soon, per the website.
hello,..
It’s not working here, could that be because of Filevault?
When entering the command in the terminal it does work.
Any clue’s?
What happens when you run the Automator action in Automator by clicking the “Run” button in the toolbar? Expand the results pane at the bottom of the Run Shell Script action to check and see if any errors are reported.
You most likely missed a step or made a mistake somewhere. I’d just delete the service and start again from the beginning. I can’t think of any reason why it would work from Terminal but not the service.
There is actually functionality built into Snow Leopard (and Leopard before it) that does this and doesn’t require any scripting at all.
Just run Keychain Access, go to Preferences and check ‘Show Status in Menu bar’. This will show a “Lock” symbol next to the clock in the OS X menu bar.
If you click on the lock, simply select “Lock Screen”. The screen goes black and will not unlock until the correct password is entered.
Granted, no nice animations but you do get a cool lock icon in the menu bar.
But unfortunately, there is no keyboard shortcut associated with the Keychain Access Lock menu. That was sort of the point of this hint, but thanks for commenting, I’m sure someone will find the Keychain Lock menu to be useful.
[...] Lock your Mac’s screen like in Windows, Snow Leopard edition [...]
Note that in order for this tip to work, the Show fast user switching menu option must be enabled in the Accounts preferences.
Incorrect. I don’t have the fast user switching menu enabled and I use this service all the time. In fact, in Mac OS X 10.5 and earlier, you don’t even have to have fast user switching enabled for the terminal command used in this service to function. In Snow Leopard fast user switching is always ‘on’.
HELP!
I followed the above and now my password doesn’t work and I can’t log in!!!
Anyone??
Is this a crafty hack??
I’m guessing you had your Mac set to automatically log in, so aren’t used to entering your password, so you’ve probably forgotten it. Are you sure your password isn’t blank?
In any case, this Apple Knowledgebase Article explains how to reset your password. Good luck, and please post back your results.
Hey,
No I always enter my password – hence wanting it as an option. For some reason it just wouldn’t accept it. I then switched off the machine and restarted – logged in fine, as usual. Haven’t tested it again as I fear the results!!
Is this the first time? Sorry to be a pain in the ass!
You’re the first to report this issue, and I really don’t think it’s this tip that’s the root of the problem. The permissions on your OS X install might be messed up. Have you repaired your disk permissions using Disk Utility lately?
This tip does the same thing as choosing “Login Window…” from the Fast User Switching menu extra. Only difference is we’re executing it via a terminal command (in step 5, above) rather than the GUI. We’re not touching your user account or password in any way.
No need to do all this, simply combine what others have said in the comments:
Go to System Preferences–>Security and choose Require Password Immediately After Sleep
Then press Control+Shift+Eject to sleep/lock your Mac. When you wake the Mac, it will ask for a password. Easy peazy.
As mentioned already a couple of times, some people don’t want to use the screensaver to lock their screen because it requires them to enter their password EVERY time the screensaver activates. This tip allows you to lock the screen independent of the screensaver. I’m usually either using two computers or a computer and a PS3 for instance. So it’s quite common for the screensaver to activate on one computer while I’m busy doing stuff with the other or playing PS3 games. I don’t want to have to unlock my screen every time my screensaver kicks in. This hint allows me greater control over when my screen locks, for instance, when I walk away from my desk at work, but not at home.
I think the original article is great because it shows how easier Automator is. What I thought was even better is that there are build key combos (the shirt-ctrl-eject for example) that I never new existed so bonus for both!
Wow thanks! it works like charm, but finally I decided to enable lock screen in menu bar using keychain access. thanks guys!
Hi,
I tried the things from the article, but have a little problem.
The shortcut doesn’t trigger the lock of the screen.
In Automator, when I go from the menu bar to Automator -> Services -> The-Service-Name-I-Made-Up (i.e. the one to lock the screen) and press it, it locks the screen. Great, so that works.
What I found out is that none of the Services’ shortcuts really work.
The shortcuts from all categories work, but the ones for Services.
Should I enable them form somewhere? (I’m a new Mac user, made the switch a couple of weeks ago).
I tried different shortcut variations and they all don’t work.
Any suggestions?
Thanks!
PS: I’m on Mac OS X v.10.6.2
Actually now it’s fine.
Forget about my question
I use your choice of shortcut and it work’s fine.
I found what I was looking for! Great article!
Thank you very much!
^_^
Many thanks for your article! (BTW very clearly and well written!)
Very helpful!
Pe@ce
I have some problems:
1) “At the top of the right hand pane where you dragged the Run Shell Script action, click on the menu next to “Service receives” and choose “no input”.” – I DONT SEE THIS!?
2)”Scroll down to the bottom and under the General category, you should see your newly created service listed there.” —- I DONT HAVE A GENERAL CATEGORY!?
I am new to MAC world – my MBP is just 3 days old.
So bear with me
Hi Ram, if you take suggestions from another Mac newbie.
1) “No input” is the last should be the last option on the bottom, it’s separated from the other options by a line.
2) You’ll have the General Category, when you create the lock-the-screen “service”.
Hi Martin,
The mistake was – I chose the WorkFlow template instead of the Services template – as soon as Automator is started.
Hence all these issues.
Thanks for your reply and wish you loads of fun with your Mac.
Excellent solution, I’m just looking for it.
But I have a small request, is it possible when I start to run the script by pressing keyboard shortcut or clicking menu item, that a confirmation window appears and asks me “You are going to lock your computer while all your applications will continue running, are you sure to do this?”?
So I can click ‘Yes’ to lock and ‘No’ to cancel.
This will help me undo my accidental invoking of the script.
After all, the script helps me a lot. Thank you!
Wow!
I just find the answer to my question!
There is an “Ask for Confirmation” atop “Run Shell Script”. I drag it to the right hand pane and put it above the lock script section. Then save and it works as exactly what I want!
Great toolkit that Apple provides to us.
Hahaha!
Very nice. Thanks for the tip.
Actually just linked it up with Proximity so it automatically locks the screen when I step out of range. Very handy indeed!
Proximity?? Right now I only have a MBP but I am picking up a new iMac today. Do the new iMacs have proximity sensors??
Proximity is a Mac app that uses your BlueTooth cell phone as a proximity sensor to trigger events. So when your Mac loses connection with the cell, it knows you’re away, when it detects it again, it knows you’re back.
This works great when I have Automator running, if I quite Automator, it does not work.
So in order for madgunde’s hint to work, Automator has to be left running all the time?
Absolutely not. luap71 is either not following the instructions properly, or has something wrong with his OS X installation. I can’t think of any reason why he would need Automator running to get this to work, seeing as it’s executed by Services not Automator.
I have been using this for a few weeks now. I had the same issue another user had – I couldn’t log back in to my machine. I was convinced I forgot to lock my mac and someone changed my password. I use this password all the time and tried multiple keyboards, caps lock on, caps lock off …. it wouldn’t work. Rebooted – typed in my password and worked right away. Also – when I had to reboot – I could do so from the login screen by clicking reboot. That just made the login window go away and nothing else happen. I had to hold down on the power button. Maybe a service died or something.
Might indicate some issue with your installation. Been using this method to lock my many Macs for years now and never encountered that. Have you ran a permission repair lately?
[...] artofgeek.com [...]
Hi guys!
Although this works just fine, like the command says it suspends the session. So this stops active downloads, stops loading YouTube videos, stops the music you’re playing and maybe all processes (Am I wrong?) It’s something like to log out fast, but when you log in again everything’s there.
So it’s not quite like in Windows.
So the shortcut: Control-Shift-Eject + a checked box in System Preferences -> Security -> General -> “Require password IMMEDIATELY after sleep or screen saver begins” is what I now use to lock the screen as I did when I was a Windows user. This instantly locks your screen AND everything continues to work. And it’s built-in the OS (Snow Leopard).
I love it! It worked great! Thank you for the tip!
Hiya,
Great post btw, but the command line doesn’t work on 10.6.2 in Terminal unless i’m in bash first. Hence, my automator script doesn’t work. Any ideas?
Cheers
Sorry, got it working, user error.
I did it slightly differently. Built an Automator Application rather than a Service which I invoke via Spotlight. I did this only because my keyboard shortcuts pane keeps crashing…. grrr.
Thank again!
Thanks for this tip! I set it up and have been using it for the past few weeks — I mostly really wanted to cube animation effect.
Unfortunately it looks like I am going to go back to using ctrl+shift+eject method instead, mostly because I found this alternative method too “laggy”. The delay between hitting my services shortcut and the actual cube rotation effect was too long to bear (up to 10 seconds). I am on a relatively fast Macbook Pro, although often with lots of programs open. By comparison, ctrl+shift+eject is satisfyingly snappy.
Has anyone else experienced this?
Great tip! But is there any way to make this keyboard shortcut apply to all users of a machine? (including network users)
Just wanted to thank you. Switched to mac several days ago from linux, and this is indeed a great way to lock the screen. (and the animation is awesome
)
I guess nobody’s used the built in “Lock Screen” feature under the KeyChain manager…
In the KeyChain Access app, open Preferences, choose ‘Show Status in Menu Bar’, check out the new PadLock icon you have in your menu bar:
Lock Screen
It uses built in controls, works on all versions of OSX, and is independent of any screen saver/fast-user switching configurations.
Cheers~
Sean Constantine
Hi Sean,
It was mentioned by commenter “Arthur” above and again, the key thing about this hint is the ability to activate it using a keyboard shortcut as in Windows. There doesn’t seem to be any built-in way to set a keyboard shortcut to activate the Keychain lock feature.
So is there anyway to automatically envoke this after x min of inactivity like the screensaver?
This would be the prerfect setup for me then, this way if I am doing something and walk away/get side tracked, it will automatically go to the login window so a guest or another user can use it.
Awesome, this was exactly what I was looking for! Thank you!
[...] found a hack courtesy of ArtOfGeek and added my Vine VNC server and a new port for the win! My favorite quote from the article [...]
is there any way to control the rotation of the animation when you lock the mac? i’d like to have the power to choose which way the desktop cube will rotate into the locked screen. ^_^
if there is any way of doing so, it would be most appreciative of you to post what needs to be done to attain such awesomeness…