Contents  ToothFairy Manual  Technical Support

4.9   Run shell script after connecting/disconnecting

ToothFairy can trigger scripts to run automatically when you connect or disconnect a Bluetooth device. Here are some examples of what you can do:

Display a Notification in Notification Center

#!/bin/sh
osascript -e 'display notification "Connected" with title "AirPods"'

Speak a Phrase Using Text-to-Speech

#!/bin/sh
/usr/bin/say "AirPods Disconnected"

Launch iTunes

#!/bin/sh
open -a "iTunes"

Some people also like to launch a certain game when connecting their headphones.

Start Playing an iTunes Playlist

#!/bin/sh
osascript -e 'tell application "iTunes" to play playlist "My Playlist"'

For more iTunes script ideas, see Doug’s AppleScripts.

Quit iTunes

#!/bin/sh
osascript -e 'tell app "iTunes" to quit'

Open Music.app

#!/bin/sh
open -a iTunes

Prevent iTunes From Playing Automatically

Some Bluetooth headphones will make iTunes start playing automatically when you use another media player. You can prevent that by using this script when connecting to temporarily turn off the iTunes agent process:

#!/bin/sh
launchctl stop com.apple.rcd

Use this script when disconnecting to turn it back on:

#!/bin/sh
launchctl start com.apple.rcd

Add a Message to the System Log

#!/bin/sh
terminal-notifier -message "Connected AirPods" -title "ToothFairy"

This script requires Terminal Notifier.

Connect to iPhone’s Bluetooth Personal Hotspot

#!/bin/sh
osascript << EOF
set _deviceName to "Tom's iPhone"

tell application "System Events" to tell process "SystemUIServer"
    set _bluetoothMenu to (first menu bar item whose description is "bluetooth") of menu bar 1
    click _bluetoothMenu
    if exists menu item _deviceName of menu of _bluetoothMenu then
        tell (first menu item whose title is _deviceName) of menu of _bluetoothMenu
            click
            tell menu 1
                if exists menu item "Connect to Network" then
                    click menu item "Connect to Network"
                    return "Connecting..."
                else
                    key code 53 -- hit Escape to close BT menu
                    return "No connect button; is it already connected?"
                end if
            end tell
        end tell
    else
        key code 53 -- hit Escape to close BT menu
        return "Cannot find that device, check the name"
    end if
end tell
EOF

You’ll need to edit the script to include the proper _deviceName for your phone.

     Contents  ToothFairy Manual  Technical Support