1

I'd like to go to the Mac lock screen with a command line script. Many of the existing answers suggest something like osascript -e 'tell application "Finder" to sleep', but my computer does not allow me to change the settings to show the lock screen on sleep.

The AppleScript includes a command to open KeyboardCleaner which disables access to the keyboard and I'd like to run this while KeyboardCleaner is active. So any script/command that doesn't depend on keystrokes is required.

I'll run my AppleScript (which opens KeyboardCleaner and also locks the computer ) by running

python3 -c 'from file import *; script();'

This is what I've tried

  1. osascript -e 'tell application "Finder" to sleep' - my computer does not show the lock screen on sleep.
  2. alias afk="osascript -e 'tell application \"System Events\" to keystroke \"q\" using {command down,control down}'" - this requires using the keyboard which is not possible for my use case
  3. pmset displaysleepnow - same problem as 1
  4. tell application "System Events" to key code 12 using {control down, command down} - same problem as 2
anki
  • 11,753
Cauder
  • 104
  • Does this answer your question? Lock Screen Command One-Liner – Allan Jul 28 '20 at 15:25
  • No for the reason I talked about at the beginning – Cauder Jul 28 '20 at 15:26
  • There’s 23 answers. Which one specifically? The question itself referenced the answer you just said “Amazing, did exactly what I wanted” so how can it be “no”? – Allan Jul 28 '20 at 15:30
  • While you're right that the command is included in the description, the command is not included as any of the answers (and not as the accepted answer_ – Cauder Jul 28 '20 at 15:33
  • 3
    Your question is a bit confusing, you are asking to "lock screen on my computer without using the keyboard" but are open for "Any command line script will do." If you can't use the keyboard, how do you want to run an command line script? Most probably we are missing some essential background here, can you please add this and also explain which means of control you have at your disposal? – nohillside Jul 28 '20 at 16:57
  • Great! The missing piece is that I can run commands with apple scripts – Cauder Jul 28 '20 at 17:02
  • Because you are alternating questions between here & Super User, it makes it hard for anyone not constantly in both to figure out what you are actually trying to do… which appears to be to manage some errant child who can't be trusted with a computer [reading between the lines]. It appears that what you really need is some solid parental control app. – Tetsujin Jul 28 '20 at 17:03
  • Thanks for the edit, but the key issue remains: How do you want to run Apple Scripts if you can't use the keyboard? And why (assuming you can still use a mouse) can't you just select the Lock Screen option from the Apple menu? – nohillside Jul 28 '20 at 17:07
  • 1
    Great question! The apple script includes a command to open KeyboardCleaner which disables access to the keyboard and I'd like to run this after KeyboardCleaner is active. I'll activate apple script by running python3 -c 'from file import *; script();' – Cauder Jul 28 '20 at 17:08
  • This just adds more weight to my assumption that we are missing essential details here. The question probably needs a rather big edit to describe the context better and focus on the problem you want to solve. – nohillside Jul 28 '20 at 17:11
  • Ok it's alright because TJ's answer works my use case – Cauder Jul 28 '20 at 17:11

1 Answers1

2

I’d like to go to the lock screen on my computer without using the keyboard. Any command line script will do.

I’m not sure what you mean by “Without using the keyboard” if you are willing to accept a script… which would have to be triggered using the keyboard.

If there is something specific / unusual / unique about your setup, it would be good to explain what that is.

In the meantime, if you are willing to accept a shell script, try this:

"/System/Library/CoreServices/Menu Extras/User.menu/Contents/Resources/CGSession" -suspend

All one line

TJ Luoma
  • 20,659
  • I'll explain the use case which is that I'd like to fire this after opening an app that disables the keyboard, but I can still use scripts because I'm using apple script – Cauder Jul 28 '20 at 15:14