macOS Screen Unlock


So, I’ve been looking at ways to get my mobile (Android) or my watch (wearOS) to be able to unlock my macOS-based computer. The difficulty lies in the fact that macOS is geared towards the Apple ecosystem. As such, it will not easily accept a device running somethign such as Android to unlock it. Instead, the easy approach is to just use an iPhone or an Apple Watch. I’d rather not because of my personal tastes.

Enter this blog post!

Software

There’s some software needed on the macOS machine:

  • web server
  • PHP-FPM
  • caffeinate (Homebrew helps with this)

What’s not covered by this document is how to get your web server to call a PHP script. Instead, we’re just going to dive right in and assume that you’ve set your web server (e.g. Apache or nginx).

Here’s the PHP script:

<?php
echo shell_exec("osascript '<path to Apple Script>'");
?>

Here’s the Apple Script:

do shell script "caffeinate -u -t 3"
tell application "System Events"
    keystroke "<password>"
    delay 1
    keystroke return
end tell

Permissions

Next, you need to grant in php-fpm rights in the macOS Security settings. My version of macOS was helpful when I ran the script locally to go through the permission process. Because Apple keeps changing stuff, it’s beyond this post on how your particular version will behave.

Getting it to Work

Now, all you need to do is call the PHP script by accessing the URL associated with the above PHP script (e.g. http://bob.local/unlock.php). For this, it could be something as simple as an IFTTT task, which is not recommended because you need to expose your machine to the outside world and defeats the security purposes, or, in my case, an NFC chip that when scanned from my mobile will unlock it.

There are many ways to accomplish it as long as the device has networking and can access the computer from that particular network.

Pretty spiffy!