How to lock a Mac lock screen without using keystrokes in the script? 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 &quot;Finder&quot; 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

osascript -e 'tell application &quot;Finder&quot; to sleep' - my computer does not show the lock screen on sleep.
alias afk=&quot;osascript -e 'tell application \&quot;System Events\&quot; to keystroke \&quot;q\&quot; using {command down,control down}'&quot; - this requires using the keyboard which is not possible for my use case
pmset displaysleepnow - same problem as 1
tell application &quot;System Events&quot; to key code 12 using {control down, command down} - same problem as 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:
&quot;/System/Library/CoreServices/Menu Extras/User.menu/Contents/Resources/CGSession&quot; -suspend

All one line