Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I need a little Applescript to lock the screen, the same behaviour I get when I click the Keychain icon in the menu bar and choose "Lock screen".

I found a way to activate the screensaver, but it's not exactly what I'm needing.

share|improve this question
Did you see the June 8th answer? I'd say that's the best solution! – Arjan Aug 15 '14 at 20:10
up vote 4 down vote accepted

Do you realize in the Security Preference Pane you can require a password to wake from the screen saver, but it defaults to not requiring authentication?

Then you can activate the screen saver with your method, or this AppleScript:

do shell script "open /System/Library/Frameworks/ScreenSaver.framework/Versions/A/Resources/"

You could also add the Keychain lock and use this AppleScript that I found here.

activate application "SystemUIServer"
tell application "System Events"
    tell process "SystemUIServer"
    	repeat with i from 1 to number of menu bar items of menu bar 1
    		tell menu bar item i of menu bar 1
    				if name of menu item 1 of front menu is "Lock Screen" then
    					click menu item "Lock Screen" of front menu
    					exit repeat
    				end if
    			end try
    		end tell
    	end repeat
    end tell
end tell

This script requires Universal Access --> Enable access for assistive devices be checked.

share|improve this answer
I went the way of screensaver with password, it seems that the Keychain is not scriptable. – Davide Gualano Dec 4 '09 at 9:39

Don't sweat it, just use tell application "Finder" to sleep

share|improve this answer

Here is a solution from Mac OS X Hints, but it seems to requires installing JackSMS first, whatever that is. Then you can do

tell application "JackSMS" to set lock screen to true
tell application "JackSMS" to set lock screen to false
share|improve this answer

Just run

/System/Library/CoreServices/"Menu Extras"/ -suspend`

This doesn't need any additional software and it is much easier than gui scripting.

share|improve this answer

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .