Update I should note I only partially sorted out getting this to actually work. Probably requires more tinkering and may be best to just go get an app that does the lock/unlock for you.
This procedure is roughly based on Lock Your Mac When Your iPhone Is Out of Range. I’d seen this in the past, but never got around to figuring out how to set it up. Since I’ve got an iPhone with good battery life, leaving Bluetooth on isn’t as scary as it was on my old Android phone, so I thought I’d give it a shot.
I’ve gone the extra steps of figuring out how to retrieve your password from Keychain in order to do the unlock. The sample unlock AppleScript in that post suggests storing your Mac account password in plain text in the script (not so great) and offers that you can save your script as “Run-only” to obfuscate it. I tried that for kicks and while the script itself is obfuscated, your plain text password is still there if you just cat the file.