How can I trigger an Applecript or Automator app when a specific bluetooth device comes into range?
This is a sort of tricky thing to do if you don't want to use 3rd party software. However, with some bash and plist scripting we can get it to work!

First, in your ~/Library/LaunchAgents folder create the following .plist file (in this case I have named it com.my.bluetoothdetector.plist; if you change the name, make sure to change line 4 in the file as well):

&lt;?xml version="1.0" encoding="UTF-8"?&gt;
&lt;!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"&gt;
&lt;plist version="1.0"&gt;
&lt;dict&gt;
    &lt;key&gt;Label&lt;/key&gt;
        &lt;string&gt;com.my.bluetoothdetector&lt;/string&gt;
    &lt;key&gt;ProgramArguments&lt;/key&gt;
    &lt;array&gt;
        &lt;string&gt;/bin/sh&lt;/string&gt;
        &lt;string&gt;/Users/YOURUSERNAME/Desktop/script.sh&lt;/string&gt;
    &lt;/array&gt;
        &lt;key&gt;WatchPaths&lt;/key&gt;
    &lt;array&gt;
        &lt;string&gt;/Library/Preferences/com.apple.Bluetooth.plist&lt;/string&gt;
    &lt;/array&gt;
&lt;/dict&gt;
&lt;/plist&gt;


The above file will call whatever script is located at /Users/YOURUSERNAME/Desktop/script.sh when there is a change in the Bluetooth environment. You can of course change the location of the file called from the Desktop to wherever you want (ensuring that it is a full path).

Now, we will make a bash script, which, when triggered by our com.my.bluetoothdetector.plist file, will check whether your device is connected. In this case, we will save this file to /Users/USERNAME/Desktop/script.sh:

#!/bin/bash

connectedyesorno="$(system_profiler SPBluetoothDataType | awk '/BLUETOOTHDEVICENAME/{f=1}/Connected:/ &amp;&amp; f{print $2; exit}')"

if [ "$connectedyesorno" == "Yes" ]
then
  open /Applications/YOURAPPNAME.app
elif [ "$connectedyesorno" == "No" ]
then
  echo "Not connected..."
else
  echo "ERROR"
fi


Here, substitute BLUETOOTHDEVICENAME for the name of your device and the /Applications/YOURAPPNAME.app part of the open /Applications/YOURAPPNAME.app command for the location of the Automator/AppleScript app you want to run. You could also replace this line entirely to run any shell script you want when the device is connected which may negate the need for you to have a separate AppleScript/Automator app to be called.

Finally, use the command:

launchctl load ~/Library/LaunchAgents/com.my.bluetoothdetector.plist


This will start up the process which will check for the change in the Bluetooth environment and then call the script.bash when this is detected. 

Now, hopefully, when you connect the Bluetooth device to your computer, after just a few seconds, your AppleScript/Automator file should run!


  I have an Applescript that connects a bluetooth speaker to my Mac...


Previously connected bluetooth devices should connect automatically to a Mac when they become available. If this isn't the case for you, maybe there is some sort of problem to do with your bluetooth settings. Have a look at this article if the problem persists.

P.S. In the future, if you want to delete this, run the following commands:

launchctl unload ~/Library/LaunchAgents/com.my.bluetoothdetector.plist
rm -r ~/Library/LaunchAgents/com.my.bluetoothdetector.plist
rm -r ~/Desktop/script.sh