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:
http://hints.macworld.com/article.php?story=20091221173111783
and here:
http://hintsforums.macworld.com/archive/index.php/t-142853.html

Step 1:
Go here:
download as zip
extract
look inside extraction folder at:
~/dist/1.0/x64/
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 1.5.zip":
from this page
http://code.google.com/p/reduxcomputing-proximity/downloads/detail?name=Proximity%201.5.zip&can=2&q=
(short link direct to file: http://goo.gl/9YATT)
extract it to Applications folder.
(read about it below before you install cause it's smart to do)
http://code.google.com/p/reduxcomputing-proximity/


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

SleepDisplay.scpt
===========
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

SleepWake.scpt
===============
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: http://macscripter.net/viewtopic.php?id=36818 
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 : http://www.j-schell.de/node/401 
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:
/Applications/SleepDisplay.scpt
(the first script you created above)

hit "test"
Screen should sleep, log back in

set "In Range Script" to:
/Applications/SleepWake.scpt
(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.

Enjoy!

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)