A Better Proximity Applescript

A Better Proximity Applescript


A while ago I came across an application called Proximity. It let's you use your bluetooth phone to notify your computer if you are near or not. "Now how," you say, "how would I use this to keep me from experimenting some techmological differences?" Well, allow me to explicate. This particular piece of software executes an applescript of your choice when your computer detects your phone, and, you guessed it, when your computer can no longer detect your phone.

So, naturally, Proximity is quite versatile. I've seen a couple example applescripts here and there, but none quite did what I wanted. So, stealing (yes, stealing) bits and pieces from all of them (and with the addition of my own little special ingredient) I frankenstiened the mother of all Proximity AppleScripts together. Ok, technically there's two scripts.

"Enough with this popcockery!" You say, "Show me the scripts!"

Wait, wait, wait! The nerve you have. I haven't even told you what these scripts do. These scripts do the following when exiting the premises of your beloved macintosh.

  • Activates JackSMS
    This program will detect if you computer has been unplugged or picked up, and alert you by either text-message or e-mail (including a picture of the perp if you have a macbook/pro with iSight) or *Gasp* both.
  • Activates DeskShade
    Not exactly necessary, it could be accomplished with another (free) program, but this will place a cool transparent lock icon on top of your desktop background and not let anyone past without a password (which you can set to be different from you default user password). Also it can record and report attempted entries.
  • Pauses iTunes
    This is the sexy part. The problem with all of the other scripts that I came across was that they would pause iTunes, yeah, but if you came back to your computer and hadn't been playing iTunes, your music would start playing anyway. With this script, your music will only resume playing if it was paused by the script, meaning you don't have to worry about coming home late at night only to start blasting Britney Spears (I know you like it). Techmologically, this could still happen if you were blasting Britney Spears when you walked out, but you should know better. But wait, there's more! Instead of just pausing or playing, iTunes will fade in and out! OMG! OMG! OMG! OMG!

When you come back to your computer with your phone, the opposite of everything above will happen. Also, you don't have to have Deskshade or JackSMS running all the time, because the script opens the applications when you leave, and then closes them when you come back. So, without further Abu, Paste this into Script Editor and call it something like "Lock Script"

  1. global okflag
  2. set okflag to false
  3. set front_app to (path to frontmost application as Unicode text) -- So we can switch back to this after running the fade
  5. -- check if iTunes is running
  6. tell application "System Events"
  7.         if process "iTunes" exists then
  8.                 set okflag to true --iTunes is running
  9.         end if
  10. end tell
  13. if okflag is true then
  14.         try
  15.                 tell application "iTunes"
  16.                         set currentvolume to the sound volume
  17.                         if (player state is playing) then
  18.                                 repeat
  19.                                         --Fade down
  20.                                         repeat with i from currentvolume to 0 by -1 --try by -4 on slower Macs
  21.                                                 set the sound volume to i
  22.                                                 delay 0.01 -- Adjust this to change fadeout duration (delete this line on slower Macs)
  23.                                         end repeat
  24.                                         pause
  25.                                         --Restore original volume
  26.                                         set the sound volume to currentvolume
  27.                                         exit repeat
  28.                                 end repeat
  29.                                 set comment of current track to "Proximity Paused"
  30.                         end if
  31.                 end tell
  32.                 tell application front_app
  33.                         activate
  34.                 end tell
  35.         on error
  36.                 beep
  37.         end try
  38. end if
  40. tell application "JackSMS" to set jack status to "on"
  41. delay 1
  42. tell application "DeskShade"
  43.         lock
  44. end tell
  45. end run

And then do the same for this code, calling it something like "Unlock Script"

  1. tell application "ScreenSaverEngine" to quit
  2. tell application "DeskShade"
  3.         unlock
  4.         quit
  5. end tell
  6. tell application "JackSMS"
  7.         quit
  8. end tell
  10. global okflag
  11. set okflag to false
  12. set front_app to (path to frontmost application as Unicode text) -- So we can switch back to this after running the fade
  14. -- check if iTunes is running
  15. tell application "System Events"
  16.         if process "iTunes" exists then
  17.                 set okflag to true --iTunes is running
  18.         end if
  19. end tell
  21. if okflag is true then
  22.         try
  23.                 tell application "iTunes"
  24.                         set currentvolume to the sound volume
  25.                         if comment of current track is "Proximity Paused" then
  26.                                 set comment of current track to ""
  27.                                 play
  28.                                 repeat with j from 0 to currentvolume by 2 --try by 4 on slower Macs
  29.                                         set the sound volume to j
  30.                                 end repeat
  31.                         end if
  32.                 end tell
  33.                 tell application front_app
  34.                         activate
  35.                 end tell
  36.         on error
  37.                 beep
  38.         end try
  39. end if



This sounds close to what I'm looking for but maybe not exactly. I use the iPhone app Remote to listen to audiobooks on speakers in another room (Air Tunes) but if I want to limit the playback for 30 minutes I can't use the sleep timer on the iPhone, since that only shuts off the iPhone. I need something to also stop the audiobook in iTunes on the computer so that I don't lose my place.


Hmm, the only way that I could find for you to do this is would be to use an app such as Keyboard Maestro Control that can run an AppleScript from your iPhone, but you'd have to install the companion app on your mac which costs $30. You might want to check out AirMote which imitates and Apple remote. Maybe you can get it to launch an AppleScript app that pauses iTunes after 30 minutes.

Another approach you could take would be to use a solution which launches AppleScripts when an e-mail is received, such as the one found here or here.


Give this app a shot - it's a desktop app that provides a webapp for you iPhone - it can apparently run AppleScripts.



Looks like there's some news on this front! Firstly, Mountain Lion (10.8) will support AirPlay, meaning you can play the audio from your phone and it should turn off with the timer. If, however, you don't want to wait for Mountain Lion's release, here is an applescript that should be along the lines of a working solution for you (albeit not the prettiest).


You didn't mention this in you article, but I've been using an applescript to toggle the screensaver by turning password protection off and relaunching the screensaver process. Everything was hunky dory for about a year until Snow Leopard broke the script (I suspect it was a security hole). Anyway, have you come across a workaround for this or is lampshade the only alternative? Anyone have any ideas?



Well you could use DeskShade or Utility Lock. I haven't messed around with the screensaver lock but after looking around it does appear to be a problem with Snow Leopard. I'll keep an eye out for any fixes.


I commented out the jack and deskshade stuff and replaced it with code that activates lock on screensaver and then starts the screensaver. then to unlock i have it hit enter, type my password, and hit enter again. works perfectly. there might be a better way where you access a saved keychain password, but that seems extra complicated...

and yeah snow leopard fixed the security hole where you coule applescript deactivate password requests... which is a good thing in my mind!

John Noble:

The hint at:


explains a workaround for the SL "won't unlock with a script" issue. Search that page for "keychain scripting" and you'll find it.

Robert :

For some reason the jack status doesn't turn on. I have the script copied just like you have stated.


hmm, can you test it out in a new Script Editor window with just the following line?

tell application "JackSMS" to set jack status to "on"

Let me know if that works or if Script Editor asks you where JackSMS is.

Phil Hudson:

(Why) Does okflag have to be global?


Well, the way the script is written now there does have to be a global boolean variable, in this case okflag. It could just as easily be written as something like

tell application "System Events"
        if process "iTunes" exists then
                tell application "iTunes"
                        set currentvolume to the sound volume
                        if comment of current track is "Proximity Paused" then
                                set comment of current track to ""
                                repeat with j from 0 to currentvolume by 2 --try by 4 on slower Macs
                                        set the sound volume to j
                                end repeat
                        end if
                end tell
                tell application front_app
                end tell
        on error
        end try
  end if
end tell

This exact code doesn't work for me though. So i put okflag there as a global variable so that it can be accessed throughout the script and not redefined in different places.



Thanks! This worked great... after some minor preference adjustments in JackSMS and DeskShade. Both were trying to lock the computer resulting in some confusion and extra password requests.


Thank you! Glad you like it.


That's pretty damn ingenious of you. Love the ability to pause Itunes then auto play when you come back only if you were playing it when you left. Nice script.




I love the script, but is it possible to only run these scripts when my laptop is not at home? Cause I take my laptop to work, there I want it to lock and unlock via proximity, but when I'm at home, why should it lock all the time. I have no clue about applescript, but of course I think it's possible (looking at wifi names, etc.)




And because I think of things a day after I post stuff, is it possible to make proximity trigger multiple times before the script starts its action, to prevent false positives (or in this case, false negatives)
e.g. set polling time on every 10 seconds, and only after 2 or 3 times the script will lock the screen. When the device comes in range again immediately unlock.
Something else, can proximity do stuff with signal strength, cause that might be a nice indicator when a device is going out of and coming into range.

Thanks for replying! :)



I don't think it does signal strength, although it does have the false positive check built into it in the preferences.


I didn't really read into this, but it looks like you could wrap the whole thing in an if statement with parts from this script.


Hey Keith,

Thanks for the answer, I'll look into this. :) (and sorry for the late reply)


Great app! The only thing is, I am getting a 'beep' when the 'Lock' script runs. Im not sure why. I have installed DeskShade, Growl, and JackSMS. Also, my music will fade out when the system locks, but when the system unlocks, it does not restart...any ideas why?

Thanks again!


I know exactly what you're talking about. That was happening to me for a while, I think it has to do with the script not knowing where Growl is. Does a window pop up asking you to locate any of the applications? Which version of OS X are you running?

Is your second issue reproducible or does it only happen on occasion?


tell application "JackSMS" to set jack status to "on"

Keeps saying "A identifier can't go before this identifier"

Help please


When compiling, I get an error in this line

tell application "JackSMS" to set jack status to "on"

It says that i can't have an identifier before this identifier

I don't code applescripts, but I am trying to get this to work. Please help!!!


Really cool script. Forgive me because I'm just learning, but do we copy and paste everything you have in the box? even the writing in gray?

Also I downloaded Deskshade and ran it but when it lock and I try to unlock it, it will not allow me to enter a password at all, it's like I can't even click in the field to type. I thought it might be because I am running Lion but I read where it was happening to someone running Snow Leopard as well.

Thanks for the help!!!




There are some major problems with Deskhade on OS X Lion.

These scripts ran perfectly right up until I upgraded to Lion, I have since stopped using them and await an update on Deskshade to address the issues so I can go back to it.

I suggest you wait until the apps have been optimised for your system and then you'll find troubleshooting a little easier.



Thank you for your response. Both issues were related to me not playing music and playing a radio station. Issues solved.

New issue! :)

I cannot get the Proximity script to start automatically for some reason. I have the 'Run at startup' box checked and still nothing. Any ideas? I am running OS 10.6.8


Hey Vegas, no problem! I think that's just a bug in Proximity. Try manually adding Proximity to your startup items in System Preferences, I believe that's what I had to do.


Hey I have made the applescripts, but how do i get them to run when my phone leaves proximity? maybe I'm just dumb, but I can't figure it out :/


Perhaps try EventScripts from mousedown - it has the best bluetooth proximity sensing I've tried: http://mousedown.net/mouseware/EventScripts.html


I haven't tried this, but in the interest of full disclosure, dora's contact info suggests that she works there.


Timothy, looks like the guy who made the Proximity app shut down his site, I've updated the link in the article to go to MacUpdate which still has a working download link.

Post new comment

The content of this field is kept private and will not be shown publicly.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd><img>
  • Lines and paragraphs break automatically.