Require a Password When Woken From Sleep Only When Specifically Requested

applescriptkeyboardpasswordSecuritysleep-wake

I want to create one hotkey that puts my monitor to sleep without requiring a password to wake it, and another hotkey that puts my monitor to sleep which does require a password to wake it.

I'll be using BetterTouchTool to create the hotkeys – but how do I create one that requires a password and another that doesn't? Bonus points if I can do this using Applescript since I already have a BTT/Applescript hotkey for when I'm leaving the house which turns off my Hue lights, mutes my Mac and puts my monitor to sleep.

I realize I can go into system prefs > security & privacy and select "require password immediately after sleep or screen saver" but that would mean I'd have to enter my password every time I put my monitor to sleep, which is what I don't want.

Best Answer

The following assumes you have/use a Login password:

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

This can be used in AppleScript to invoke the Login window, which will not close whatever you currently have open and as such when you log back in, your Desktop is as you left it.

This way you can have the "Require password ... after sleep or screen saver begins" check box in Security & Privacy's General tab unchecked but still not let someone have immediate access after you walk away, having invoked the Login window.