Sunday, July 1, 2012

Secure Sleep and Wake a Mac using BT, updated for Lion, WORKS

How I made this work to lock/unlock & displaysleep/wake my computer when I am (or my phone is, actually) near it:

The way I improved upon the above link is by making my computer display actually go to sleep, turn off, this saves power and makes it last longer. The same as pushing "shift-control-eject" at the same time. It's better than screen saver, because it saves the backlight, and better then sleeping the computer, because the computer can still do stuff, it's not "sleeping" just the display is.

Unfortunately, Apple removed keychain scripting in Lion, along with some other cool stuff. So this post updates my previous post here to work with Lion.

I found basic info about this here:
and here:

Step 1:
Go here:
download as zip
look inside extraction folder at:
copy "SleepDisplay"to your applications folder

run it and authorize it if it asks

Step 2:
open keychain access (in utilities folder)
then file menu, new password item:
Keychain item name and account name, set to "myPass"
Password: set to the password you use to log onto your computer.
This stores your password in a secure place that we can later access via script.

Step 3:
Download the opensource free file "Proximity":
from this page
(short link direct to file:
extract it to Applications folder.
(read about it below before you install cause it's smart to do)

Step 4:
Write 2 applescripts in scriptwriter, named as below:

on run
tell application "Terminal"
do shell script "say goodbye" --hehe
delay 0.5
do script "/Applications/SleepDisplay"
end tell
end run

==========(pic below)

This script just calls the executable you made in Step 1

on run
set thePW to ""
set label to "myPass" -- use the name of the password you made above
set thePW to do shell script "security 2>&1 >/dev/null find-generic-password -gl " & label 
-- following several lines from: 
set oldelim to text item delimiters
set text item delimiters to "\""
set tlst to every text item of thePW
set thePW to item 2 of tlst
set text item delimiters to "\\"
set tlst to every text item of thePW
set thePW to item 1 of tlst
set text item delimiters to oldelim
-- this will now work if your password contains problematic characters or is not 7 bit ASCII
-- see this : 
tell application "System Events"
tell application process "loginwindow"
keystroke 125
delay 1
keystroke thePW
delay 0.2
keystroke return
end tell
end tell
tell application "Terminal"
do shell script "/Applications/SleepDisplay -w" -- this is the missing part that wakes the display in Lion
do shell script "say hello" -- hehe, I love that! delete this line if you want
do script "killall Terminal"
end tell 
end run
=============== (pic below)

Save both of these scripts in the /Applications folder

Step 5:
open "Proximity" app you just downloaded in Step 3
(icon will be in upper right of screen)
connect your phone with the app by using the "change device" button

set "Out of Range Script" to:
(the first script you created above)

hit "test"
Screen should sleep, log back in

set "In Range Script" to:
(the second script you created above)

hit "test"

You may get several repeated security warnings asking you to allow keychain access etc by several programs, just enter your password whenever it asks and click "allow always" or "allow" if always isn't available. this is giving the app "Proximity" and the script permission to look up your password that you securely stored in keychain, so you don't have to store your password in plain text format.

Step 6: (Important!)

Once it is working and tested and you are happy, open the script "SleepWake.scpt" in AppleScriptEditor, and re-save it by using the "Save As..." File command and check the option box for "Run Only" this will prevent someone from opening it up in script editor and running it and then viewing your password in the script results. Name it the same name and overwrite the original file.


Leave bluetooth active on your phone. When you walk away from your computer with your phone in your pocket the display/s will sleep and lock, requiring a password to login, (in case you come back without your phone)

When you walk back with your phone in your pocket the screen will automatically unlock and light up.
(and say hello)

Saturday, June 30, 2012

Compile Updated SleepDisplay for OSX Lion

NOTE: I didn't write any of this code. I just found it and put it together.

Git here:

copied the code below from the file SleepDisplay.m found here:

forum discussion here:
last post

This is an updated version of the code I described in this post. it is updated to wake the screen also, with a -wake argument, since Lion doesn't seem to do that now with scripted keystrokes

open Textedit, paste whats between the lines below into it:

// gcc -Wall SleepDisplay.c -o SleepDisplay -framework CoreFoundation -framework IOKit
// I just put that above line in to remind me what command to run in terminal
 * SleepDisplay
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * GNU General Public License for more details.
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <>. 

#import <Foundation/Foundation.h>
#import <IOKit/IOKitLib.h>

int main (int argc, const char * argv[]) {
    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
    NSArray *params = [[NSProcessInfo processInfo] arguments];
    BOOL shouldWake = NO;

    for (NSString *arg in params)
        if([arg isEqualToString:@"-wake"] || [arg isEqualToString:@"--wake"] || [arg isEqualToString:@"-w"])
            shouldWake = YES;
        if([arg isEqualToString:@"-help"] || [arg isEqualToString:@"--help"] || [arg isEqualToString:@"-h"])
            printf("usage: SleepDisplay [-w]\n");
            printf("Without options, sleeps the display (not system sleep)\n");
            printf("use with the -w (--wake) option to wake\n");
            // quit.
            [pool drain];
            return 0;
    io_registry_entry_t entry = IORegistryEntryFromPath(kIOMasterPortDefault, "IOService:/IOResources/IODisplayWrangler");
        IORegistryEntrySetCFProperty(entry, CFSTR("IORequestIdle"), shouldWake ? kCFBooleanFalse : kCFBooleanTrue);
    [pool drain];
    return 0;

if you're using text edit make sure you go under format menu "Make Plain Text"
Save this file as "SleepDisplay.txt"


change the extention .txt to .c so now the file name is "SleepDisplay.c" to do this I ran in terminal:

mv SleepDisplay.txt SleepDisplay.c


(because apple removed it or something form Lion, see:

run terminal in the same folder as the file and copy the next line into it & hit return:
gcc -Wall SleepDisplay.c -o SleepDisplay -framework CoreFoundation -framework IOKit

if it doesn't work you might have to use "su" first and type in your admin password then run the command again.

--- still testing but moving on to the next post, this bores me now. seems to work tho...