Ashutosh
Ashutosh

Reputation: 5742

How to Lock (not sleep) my Mac using NSAppleScript or any other command that can be executed from a Mac app

In my Mac app i am trying to lock my Mac based on some actions in my app. Until now i have only got a way to put it to sleep but not lock. Currently i am doing this :

let appleScript = NSAppleScript(source: "tell application \"Finder\" to sleep")
        appleScript?.executeAndReturnError(nil)

Please guide.

Upvotes: 0

Views: 320

Answers (1)

Dave Satch
Dave Satch

Reputation: 1171

Instead of

tell application "Finder" to sleep

Use the following:

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

You will need to escape the " characters with \ as you have already with the Finder command.

Upvotes: 1

Related Questions