Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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
add comment

4 Answers 4

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/ScreenSaverEngine.app"

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
    			click
    			try
    				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
add comment

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
add comment

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

share|improve this answer
add comment

Just run

/System/Library/CoreServices/"Menu Extras"/User.menu/Contents/Resources/CGSession -suspend`

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

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.