Benches macOS

6

Applescript to lock the screen

apple Verified

Imported from the Apple Support Forums via the Wayback Machine 🔗

Hello all,

I need an Applescript to lock the screen, the same behaviour I get when I click on the Keychain icon in the menubar and choose "Lock screen".

I have no experience with Applescript, so I need some tips to start...

6 replies

Sign in or join Beachball Help to reply.

  1. 0

    V.K.

    Imported from the Apple Support Forums via the Wayback Machine 🔗

    this exact function is not scriptable. but the following should do just as well. it will suspend your session and take you to the login window without logging you out. you'll need to enter your password to resume the session.

    <pre style="

    font-family: Monaco, 'Courier New', Courier, monospace;

    font-size: 10px;

    margin: 0px;

    padding: 5px;

    border: 1px solid #000000;

    width: 720px;

    color: #000000;

    background-color: #ADD8E6;

    overflow: auto;"

    title="this text can be pasted into the Script Editor">

    do shell script "/System/Library/CoreServices/Menu\ Extras/User.menu/Contents/Resources/CGSession -suspend"

    </pre>

  2. 0

    ilDave

    Imported from the Apple Support Forums via the Wayback Machine 🔗

    Thank you for your answer, this is nice but it seems to shut down the network connections, I had file downloading in the browser when I tried it and the download stopped. The Lock screen from Keychain insted does not stop the connections.

    Maybe I can resolve this by just launching the screensaver and set it to ask me the password when reactivated.

  3. 0

    V.K.

    Imported from the Apple Support Forums via the Wayback Machine 🔗

    ilDave wrote:

    Thank you for your answer, this is nice but it seems to shut down the network connections, I had file downloading in the browser when I tried it and the download stopped. The Lock screen from Keychain insted does not stop the connections.

    Maybe I can resolve this by just launching the screensaver and set it to ask me the password when reactivated.

    yes, that's another option. you can launch screensaver via apple script as follows

    <pre style="

    font-family: Monaco, 'Courier New', Courier, monospace;

    font-size: 10px;

    margin: 0px;

    padding: 5px;

    border: 1px solid #000000;

    width: 720px;

    color: #000000;

    background-color: #ADD8E6;

    overflow: auto;"

    title="this text can be pasted into the Script Editor">

    try

    tell application id "com.apple.ScreenSaver.Engine" to launch

    end try</pre>

  4. 0

    ilDave

    Imported from the Apple Support Forums via the Wayback Machine 🔗

    I'll do this way 🙂

    There is also and Applescript way to stop the screensaver and enter the password if prompted to?

  5. 0

    V.K.

    Imported from the Apple Support Forums via the Wayback Machine 🔗

    ilDave wrote:

    I'll do this way 🙂

    There is also and Applescript way to stop the screensaver and enter the password if prompted to?

    no, there is no script for that. I also I don't understand the point. you can stop sceensaver by moving the mouse, using the keyboard or activating any program remotely. I thought the idea was to lock the screen?

  6. 0

    ilDave

    Imported from the Apple Support Forums via the Wayback Machine 🔗

    I'm trying to setup Proximity (
    http://code.google.com/p/reduxcomputing-proximity/), a handy utility that check the bluetooth connection with a cellphone and can run applescripts when device comes in range or when goes out of range.

    I'd like to set it up so that when I leave my desk with my phone in the pocket the screensaver starts, and when I come back the screensaver stops.

    It's not an absolute must-have for me, but I find the "my mac is aware of me leaving" very fun and I'd like to be able to implement it 😀