Thursday, June 30, 2011

Lock and unlock a Mac using Bluetooth detection (from Mac OS X Hints)

NOTE:
This is original post, I have updated instructions here that work better with OSX Lion.


This is really cool!
I found basic info about this here:
http://hints.macworld.com/article.php?story=20091221173111783

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.


Step 1:
Do this:
http://kprops.blogspot.com/2011/07/sleep-display-instantly-hack-found-not.html
This creates a unix executable that will sleep the display instantly when run,
save it in Applications folder
Then in system preferences, "security" check "require password....blah blah blah" to "immediately"

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 1 applescript in scriptwriter, named as below:

SleepDisplay.scpt
===========
on run
tell application "Terminal"
do script "/Applications/SleepDisplay"
end tell
end run
==========(below also)









This script just calls the executable you made in Step 1
Save it in the Applications folder

NOTE: Do ONLY step 5a or 5b, 5a for pre-LION, 5b for Lion
(since Apple removed Keychain Scripting app in Lion)
There is another problem with Lion. 5b script below only logs you in, it doesn't actually nudge the display awake. Once you sit down you have to hit any key (i use the down button) to wake up the display, but at least you don't have to enter your password.

Step 5a: (for pre "OSX Lion")

SleepWake.scpt
==========
on run
tell application "Keychain Scripting"
set thePW to the password of first key of current keychain whose name is "myPass"
tell application "System Events"
tell application process "loginwindow"
keystroke 125
delay 1
keystroke thePW
delay 0.2
keystroke return
end tell
end tell
end tell
tell application "Terminal" to quit
end run

=============== (below also)













Save it in the Applications folder

Step 5B (for Lion)

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" to quit
end run

=============== (below also)





















Save it in the Applications folder

Step 6:
open Proximity app you just downloaded
(icon will be in upper right of screen)
pair your phone with your computer 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 isnt 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 7: (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.

You could also tell the wake up script to play a sound file that says hello to you, and the sleep script to say goodbye, use text to voice engines etc, etc... :)

10 comments:

Anonymous said...

Thanks for posting this!

I've got a question for you. Whenever i walk away and my display is put to sleep, I come back and a new Terminal window is open for each login. How do I disable this?

Thanks

Kevin said...

Hey,

I got this suggestion from a comment on another blog.

http://www.idownloadblog.com/2011/07/03/unlock-your-mac/

Thanks for commenting b/c it motivated me to finally change it here, I've added a change in SleepWake.scpt that kills terminal right before the computer screen wakes, that gets rid of those annoying leftover terminal windows, it's the yellow text above.

-kevin

arteggio said...

Would you be up for figuring out a Lion version? Apparently, Apple has removed Keychain Scripting.app.

There's a third party replacement, though: http://www.red-sweater.com/blog/2035/usable-keychain-scripting-for-lion

I tried playing around with the code a bit, but it's all beyond me.

Kevin said...

I would be up for it, for sure, as soon as i get lion installed.
I work about 70 hours a week on tv shows and really just play with scripts in my free time, which I have none of now. but soon i hope.

An easy fix would be to skip the whole keychain thing all together, and type the password directly into SleepWake.scpt.
to do this edit what's above for SleepWake.scpt and...

1. remove lines 2 & 3
2. change line 8 to:
keystroke "your-password-here"
(you need the quotes)
3. get rid of one of the three "end tells"

I haven't tested it but I think that would work.
I just thought it was safer to use the keychain. If you check the 'run only' box when you save the script it turns it into computer code gibberish that most people wouldn't be able to understand. That's probably safe enough.

I'll check out that red-sweater thing when I get a chance. Thx for commenting :)

arteggio said...

I'm going into the same industry, so I know what you mean. ;)

Did as you said, but couldn't get it working. Replacing thePW with my password will type something into the password window, but it's two characters shorter than my actual password. Running the script however brings up my full password at the end. (If, on the other hand, you met the *new* line 8 after deleting 2 and 3, that just didn't work at all. I looked into the scripting dictionaries a bit, but couldn't find the exact little part that might be missing.)

Oh well. Someone else had a script with the password imbedded, but I can't find it again to compare. The fact that it locks is good enough, though, since it's not too hard to just type the password when I return. So, thanks for the first script anyway! =)

Kevin said...

try changing

delay 0.2
to
delay 1
or
delay 2

it's probably hitting return before the password is fully entered.

thanatos said...

On Lion, the display won't wake, even if I simulate keystrokes in AppleScript. Is there a way to code a similar binary for waking it? I tried everything, but nothing works, really a pain. Thanks!

BattleBird said...

this script is awesome i just wish it worked in lion :/

Darius said...

It works in lion . It does inputs your password for you. Only you need touch mouse to wakeup screen .

Kevin said...

Hey, thank you for the comments! I finally had a few moments to update this. I finished my season of 70 hour weeks, but then did some traveling for the last two months. I updated the SleepWake script to use keychain now, without installing any other software. But as Londonas says, you still have to bump the screen to wake it, (move the mouse, or hit the down arrow on the keyboard, just something to nudge it awake) but at least you don't have to enter your password. I found how to fix that, the sleepdisplay app has been updated, but I'm going to just put it in another post:

http://kprops.blogspot.com/2012/07/secure-sleep-and-wake-mac-using-bt.html

This post is all cluttered already.

Post a Comment