Contents  ToothFairy Manual  Translate  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. Normally, ToothFairy runs the connection script when the device becomes the audio output device, i.e. when it is ready to use. However, you can use this Terminal command to instead make it run the script when the Bluetooth connection is made:

defaults write com.robinlu.mac.Tooth-Fairy RunConnectionScriptWhenSettingAudioOutput NO

Here are some examples of what you can do with connection and disconnection scripts:

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

Run a Shortcut

You can trigger a shortcut, created in the macOS Shorcuts app, by specifying its name:

#!/bin/sh
shortcuts run "NameOfYourShortcut"

Launch Music or Another App

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

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

Start Playing a Music Playlist

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

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

Quit Music

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

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 Settings ‣ 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  Translate  Technical Support