0

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.

Ashutosh
  • 5,614
  • 13
  • 52
  • 84

1 Answers1

1

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.

Dave Satch
  • 1,161
  • 6
  • 12
  • But this is a bash command i am trying to achieve the same using AppleScript. – Ashutosh Sep 01 '15 at 16:40
  • Applescript can run this shell script. There is no direct command that can do what you ask, although some have had luck with using the button found in the Keychain application using the following `tell application "System Events" to tell process "SystemUIServer" to click (first menu item of menu 1 of ((click (first menu bar item whose description is "Keychain menu extra")) of menu bar 1) whose title is "Lock Screen")`. See more options here http://apple.stackexchange.com/questions/80058/lock-screen-command-one-liner – Dave Satch Sep 02 '15 at 01:06