Unlocking and locking mac screen via ssh

Solution 1:

There are many ways to lock and unlock your Mac via SSH. The easiest and probably the fastest way is to use osascript (applescript).

Locking Mac

To do this, log in via ssh and type in

osascript -e 'tell application "Finder" to sleep'

You can switch Finder for System Events, but that means more characters to type.

Unlocking Mac

To wake your Mac is a little harder.

There are two ways to do this.

You can use osascript again.

osascript -e 'tell application "System Events" to key code 123'

This tells it to keystroke the left arrow key which should wake your Mac up from sleep.

You can also use the pmset command.

pmset schedule wake "mm/dd/yyyy 00:00:00"

Use a date and time that has already passed.

Of course you will need to keystroke your password via Applescript, e.g.

osascript -e 'tell application "System Events" keystroke "password"'

An alternative to this could be the Remote Wake Up app for Mac, but I am not sure if you can put a password in.

Solution 2:

I use this for locking the screen (works on Big Sur): osascript -e 'tell application "System Events" keystroke "q" using {control down, command down} end tell'