Bluetooth Proximity Detection on OS X

I use this to lock and unlock my Mac, at work, based on the bluetooth connection between my phone and computer. Once my Mac detects that the phone is unavailable it locks itself.

What you’ll need:

  • Proximity 1.0 a free download from Apple.
  • A compiled version of notify.c
    #include
    int main(int argc, char ** argv)
    {
    CFMessagePortRef port = CFMessagePortCreateRemote(NULL, CFSTR("com.apple.loginwindow.notify"));
    CFMessagePortSendRequest(port, 500, 0, 0, 0, 0, 0);
    CFRelease(port);
    return 0;
    }

    You can download a pre-compiled version here, or compile it yourself (Apple’s Dev Tools required) with the command
    cc -arch ppc -arch i386 -o notif notif.c -framework CoreFoundation
  • An Apple Script to set the screen saver
    -- Turn on the screen saver password
    do shell script "defaults -currentHost write com.apple.screensaver askForPassword -int 1"
    set MyName to do shell script "whoami"
    set runFile to "/Users/" & MyName & "/notif"
    -- display dialog runFile
    do shell script runFile
    -- Activate the screen saver
    tell application "ScreenSaverEngine" to activate

    N.B. You’ll need to change “/notif” to match the location if the compiled notif binary
  • An Applescript to unset the screensaver.
    -- Disable the screen Saver Password
    set MyName to do shell script "whoami"
    set runFile to "/Users/" & MyName & "/notif"
    do shell script "defaults -currentHost write com.apple.screensaver askForPassword -int 0"
    do shell script runFile
    -- Turn OFF the screen saver
    tell application "ScreenSaverEngine" to quit

    N.B. You’ll need to change “/notif” to match the location if the compiled notif binary
  • Connect your bluetooth device to the Mac via bluetooth.
  • Run Proximity, set your device, set the Applescripts

Useful Links:

One Comment

  • mastercheif wrote:

    IF you can provide the link to the pre-compiled version, I would appreciate it. I don’t have any room on my laptop for the Dev Tools

Post a Comment

Your email is never shared. Required fields are marked *