Execute Only If Device is Attached

Have you ever been in the situation where you needed to open an application but you only want to open this particular application if a specific device is attached? For instance, when there is a symbolic link to a huge dependent library on an external drive. Or the audio interface needs to be attached before opening the DAW.

Executing such applications without meeting these requirements can mess up your settings and you may have to manually readjust preferences, paths, do cleanups, etc. Eventually data can get lost or overwritten.

It's too easy to forget that a device is not connected. This can be pretty annoying and is counterproductive to a creative workflow: An idea comes to your mind and you want to begin working on it immediately but then you're distracted by fixing up the mess caused by not having the proper device attached before opening the software.

My particular case

When I forget to connect the audio interface before I launch Logic Pro X it's going to use the system audio driver by default. Then I have to manually change the settings and make sure some things aren't altered like the sample rate, amongst other things.

Another case is when I open video or photo editing software without having an external disk connected where the library is stored. Then a new generic library will be created.


With the following command it's possible to list all connected USB Devices. This is the first step for finding the audio interface.

# List all USB Devices
❯ ioreg -p IOUSB -w0 | sed 's/[^o]*o //; s/@.*$//' | grep -v '^Root.*'
Apple Internal Keyboard / Trackpad
BRCM20702 Hub
Bluetooth USB Host Controller
Duet USB

Then we can isolate the audio interface.

# Grep Duet USB Device
❯ ioreg -p IOUSB -w0 | sed 's/[^o]*o //; s/@.*$//' | grep -v '^Root.*' | grep "Duet USB"
Duet USB

The following command helps to identify if a volume named DATA is mounted.

# List external Disk
❯ mount | grep /Volumes/DATA

With this information we can write a function and set up an automatic workflow which meets our requirements.


You can put this in your .zshrc/.bashrc/etc.

# Music workflow
music () {
    NC='\033[0m' # No color
    if ioreg -p IOUSB -w0 | sed 's/[^o]*o //; s/@.*$//' | grep -v '^Root.*' | grep "Duet USB" > /dev/null && mount | grep /Volumes/DATA > /dev/null; then
        open /System/Library/PreferencePanes/Sound.prefPane
        open ~/Music/Sheet\ Music/;
        open /Applications/Logic\ Pro\ X.app/;
        open /Applications/Apogee\ Maestro\ 2.app/;
        echo "Both ${RED}`DATA`${NC} and ${RED}`Duet USB`${NC} must be connected to proceed"


The music function checks if both Duet USB and /Volumes/Data are attached. It either exits for False or proceeds to open the workflow.

The output for failing the requirements is:

❯ music
Both `DATA` and `Duet USB` must be connected to proceed.

Customize as needed. Variables can be: Duet USB, /Volumes/DATA, paths

System Setup

  • OS X 10.11 El Capitan
  • ZSH with .zshrc
  • USB audio interface
  • External disk