Contents  ToothFairy Manual  Technical Support

4.9   Run shell script after connecting/disconnecting

ToothFairy itself is AppleScriptable:

applescript dictionary

ToothFairy can also 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

#!/usr/bin/osascript
display notification "Connected" with title "AirPods"

Speak a Phrase Using Text-to-Speech

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

Connect Another Bluetooth Device

You can attach a script like this to your mouse so that when the mouse connects it auto-connects your keyboard at the same time:

#!/usr/bin/osascript
tell application "ToothFairy"
    set connected of device "Keyboard" to true
end tell

Disconnect Another Bluetooth Device

You can attach a script like this to your AirPods so that when the headphones connect your speaker disconnects:

#!/usr/bin/osascript
tell application "ToothFairy"
    set connected of device "Speaker" to false
end tell

Launch iTunes or Another App

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

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

Start Playing an iTunes Playlist

#!/usr/bin/osascript
tell application "iTunes" to play playlist "My Playlist"

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

Quit iTunes

#!/usr/bin/osascript
tell app "iTunes" to quit

Open Music.app

#!/bin/sh
open -a iTunes

Change the Sound Input or Output Device

macOS normally sets the sound input and output automatically when connecting a device. However, in some cases it may not switch them automatically or you may wish to specify different devices. You can do this by installing switchaudio-osx and using a script like this:

#!/bin/sh
/usr/local/bin/SwitchAudioSource -t output -s "DeviceName"
/usr/local/bin/SwitchAudioSource -t input -s "DeviceName"

Adjust the Volume

macOS normally remembers the volume for each device, but you can also set it via script each time ToothFairy connects a device:

#!/usr/bin/osascript
set volume output volume 25 without output muted
set volume input volume 75

Turn Off Bluetooth When Disconnecting

Some Macs will wake from sleep to try to connect to a nearby Bluetooth device that becomes available. This can be prevented by turning off Bluetooth on your Mac when disconnecting a device. You can do this by installing blueutil and using a script like this:

#!/bin/sh
blueutil --power 0

Connect to Another Mac When Disconnecting

Say that you have Mac A and Mac B that are on the same local network (mac-a.local and mac-b.local) and you want them to share a keyboard (named Keyboard in ToothFairy):

  1. Go to System Preferences ‣ Sharing and enable Remote Apple Events.

  2. On Mac A, set a disconnection script:

    #!/usr/bin/osascript
    tell application "ToothFairy" of machine "eppc://user:password@mac-b.local"
        set device "Keyboard"'s connected to true
    end tell
    

    You’ll need to fill in your username, password, and Bonjour name.

  3. On Mac B, set a disconnection script:

    #!/usr/bin/osascript
    tell application "ToothFairy" of machine "eppc://user:password@mac-a.local"
        set device "Keyboard"'s connected to true
    end tell
    

Now, whenever you disconnect the keyboard on one Mac, it will use AppleScript over the network to tell the other Mac to connect to it.

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

These scripts were contributed by a customer using macOS 10.14 and may not work on all Macs or with newer versions of macOS.

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

#!/usr/bin/osascript
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

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

     Contents  ToothFairy Manual  Technical Support