Applescript to lock the screen

4

2

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.

Davide Gualano

Posted 2009-12-03T11:22:08.313

Reputation: 688

Did you see the June 8th answer? I'd say that's the best solution! – Arjan – 2014-08-15T20:10:55.670

Answers

4

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.

ridogi

Posted 2009-12-03T11:22:08.313

Reputation: 2 787

I went the way of screensaver with password, it seems that the Keychain is not scriptable. – Davide Gualano – 2009-12-04T09:39:33.963

3

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

user331567

Posted 2009-12-03T11:22:08.313

Reputation: 39

1That makes the machine go to sleep, which isn't quite the same thing as locking the screen. – duskwuff -inactive- – 2017-03-02T03:08:14.087

2

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.

ben

Posted 2009-12-03T11:22:08.313

Reputation: 333

This command logs you out of your session entirely. It is very slow. I lost all my Chrome tabs when I did this. – Bruno Bronosky – 2017-03-01T19:18:57.403

It'd be nice if it were that simple. For me it shows the system login screen which uses my non-default keyboard layout, making it really painful to log in. Also pressing any key doesn't show login prompt, like in lock screen. – quuxman – 2018-03-12T22:00:53.773

1

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

Lawrence Velázquez

Posted 2009-12-03T11:22:08.313

Reputation: 929

As far as I can tell, JackSMS no longer exists. – duskwuff -inactive- – 2017-03-02T03:11:15.543

-1

Here's my one liner for this. It uses System Events to send the default key combo for the Lock Screen command and works in all versions of OSX. Even Mojave! Though it will prompt for additional Security permissions for the App, in the newer OSX versions

tell application "System Events" to keystroke "q" using {control down, command down}

user1270949

Posted 2009-12-03T11:22:08.313

Reputation: 1