I have an Applescript that connects a bluetooth speaker to my Mac and switches “Play sound effects through:” to “Internal Speakers”.

I want this script to run every time I turn on my bluetooth speaker. I am currently triggering the script manually with a keyboard shortcut every time I switch the speaker on, but it would be even more convenient if I could set the script to run automatically whenever the bluetooth speaker becomes available to the Mac, preferably without using third-party software.

Is there a way to do this?


1 Answer 1


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):

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">

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:


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

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

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
  • Thanks for the help. When I get to the launchctl command, I get the error “invalid property list”.
    – Macrod
    Aug 2, 2018 at 12:11
  • @Macrod That's odd, I never received that error during my testing; have you ensured you have pasted it exactly? You may also want to use Atom to make the file if you have been using TextEdit or a similar program. However, after a few google searches I came across some people having similar problems. I have made an edit to the original answer with one of the solutions as linked. Hope this solves your question! If not just do some googling around trying to find what exactly it is in the .plist file that is creating the error. Best
    – Tom
    Aug 2, 2018 at 12:53
  • BTW I use a program called EventScripts by Mousedown however, it's no longer available in the US App Store (at the present time anyway). It's nice to know though how to workaround it if the current version I have stops working in macOS Mojave, when I upgrade. Aug 2, 2018 at 17:07
  • @user3439894 I hadn't heard of that program before but I'll have to look into it because it looks like it has a lot of functionality! Is there not a way to get it from somewhere other than the App Store? I'm glad I was able to provide you with a possible solution assuming your program stops working when you upgrade. As I mentioned in the answer, the whole process would be simplified by using a 3rd party app like yours but it was specifically requested to use built in resources only.
    – Tom
    Aug 2, 2018 at 17:12
  • @user3439894 Also, I'm sure that with some adjustments to my answer, you could modify it to trigger when other, non Bluetooth, related events occur like an SSID change. P.S. I am still confused as to why the original, flawed, '.plist' file worked for me but not for the person asking the question... odd.
    – Tom
    Aug 2, 2018 at 17:14

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .