Unlocking and locking mac screen via ssh

remote controlremote desktopssh

Is there a way to unlock and lock screen from command line, via ssh? I know /System/Library/CoreServices/Menu\ Extras/User.menu/Contents/Resources/CGSession -suspend command, but it doesn't seem to work via ssh. The perfect solution would be a command, that I could type logged via ssh and it would unlock the screen or lock it back again, so I don't need to tell anybody my password, if he wants to use it. Is there any builtin tool, that would provide such functionality?

Best Answer

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.