Applescript to lock the screen

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.


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


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.