Six Colors
Six Colors

Apple, technology, and other stuff

Support this Site

Become a Six Colors member to read exclusive posts, get our weekly podcast, join our community, and more!

By Jason Snell

Quick Tip: Which USB devices are currently attached?

A shortcut showing the command below being used in the Shortcuts app.

I’ve recently switched from using two computers in two different offices (the hard-to-heat garage and my well-heated back bedroom) to using one computer (a MacBook Pro) docked in either location. There’s a lot that has gone into this decision, which I will detail in a future post, but this post is about one of the most frustrating aspects of this switch: the inability of my computer and automations and settings to understand when the context has changed.

Fortunately, my Mac reacts to switches to my network configuration with aplomb, so I don’t have to dive deeep into Network Locations, though if I did, there are several alternatives to old classic utilities like ControlPlane.

But I record a lot of podcasts, and I have two entirely different USB devices that I use for audio. How can I make it so that when I press the “record” button, the right USB device is recording and receiving audio?

My overall solution ended up being pretty complex, but the key insight was to use a Terminal command to list all the connected USB devices. You don’t need to know a lick of Terminal to use it, because you can stick it in a “Run Shell Script” block in Shortcuts. Here’s the command:

ioreg -p IOUSB -w0 | sed 's/[^o]*o //; s/@.*$//' | grep -v '^Root.*'

This command will output a list of all your currently attached USB devices in plain text.

(Optional explanation: ioreg will display an enormous list of devices and ports on your system. -p IOUSB restricts it to USB devices and -w0 makes it display complete devices one per line, without truncation. That result is sent to sed, which uses a regular expression to match just the names of the attached USB devices. The final step is to use grep to remove the initial line, which summarizes the list rather than listing an actual USB device. You don’t need to know this.)

At that point, you can build a Shortcut that alters its behavior based on the contents of the output. For example, my Shortcut’s next step is an If/then block that checks to see if the result contains the text “MV7” for my Shure MV7 microphone or “USBPre” for my USBPre2 audio interface. But it’ll work with any USB device that happens to be attached at a given time.

If you appreciate articles like this one, support us by becoming a Six Colors subscriber. Subscribers get access to an exclusive podcast, members-only stories, and a special community.


Search Six Colors