Category: Automation

Switching monitor inputs programmatically, on M1 MBP

I’ve got a monitor that has multiple inputs: one is DisplayPort from my M1 MacBook, and VGA from my TrueNAS server.
For a while I was just switching the input using the buttons on the monitor, but who wants to keep reaching up and to the right for that?!

Here’s how I was able to map Cmd+F6 and Cmd+F7 to switch between two arbitrary inputs on my monitor.

  1. Install m1ddc using Homebrew: brew install m1ddc
    m1ddc is a command-line tool for controlling the display on an M1 Mac. I’m not sure what the equivalent is for Intel Macs.

  2. List your displays: m1ddc display list
    This should produce a list of displays. Like this:

    $ m1ddc display list                              
    [1] DELL U2720QM (BB7A124B-B761-4DE2-B597-7E1A432EAAA2)
    [2] (null) (37D8832A-2D66-02CA-B9F7-8F30A301B230)
    [3] DELL U2410 (320C4B79-F89E-4601-8592-DA40A547289B)
  3. Change the input on the display you want to control: m1ddc display 320C4B79-F89E-4601-8592-DA40A547289B set input 15
    This should switch the input on the monitor to whatever you specified. In my case, 15 is DisplayPort.
    The docs specify the following inputs: DisplayPort 1: 15, DisplayPort 2: 16, HDMI 1: 17, HDMI 2: 18, USB-C: 27.
    It’s possible to use 1/2 for picking a display, but the indexes change when your displays change, for instance if you close the lid on your laptop. So it’s better to use the display UUID instead.

  4. Experiment with setting the input on whatever display you want.
    I experimented a bit and found that 1 was VGA and 15 was DisplayPort on my monitor.

  5. Using BetterTouchTool, I mapped Cmd+F6 to m1ddc display 320C4B79-F89E-4601-8592-DA40A547289B set input 15 and Cmd+F7 to m1ddc display 320C4B79-F89E-4601-8592-DA40A547289B set input 1.
    Now I can switch inputs on my monitor with a simple keyboard shortcut!

Note:
m1ddc1 doesn’t seem to work when the monitor turns off due to inactivity.
Not sure why, and not sure if I’ll bother debugging this issue just yet.
The workaround is to not let the monitor turn off due to inactivity.
So either configure the device on the second input to never go into sleep mode, or remember to switch the input before the monitor goes to sleep.

Keep screen on while MacOS plays music

My MacBook is connected to a 4K screen with a single USB-C cable. This is great, since I can use the monitor as a hub, for things like USB devices and even audio.

The only time this is an issue is when I am listening to music in my office, but not actively using the computer, like if I am reading. That’s because I have configured by Mac to turn off an external monitor to save power. So after about 20 minutes of inactivity, my external monitor turns off, and I lose the audio.

I want to keep the “power display off after 20 minutes” feature, but I want to have a way to keep the audio active when I’m listening to music.

Macs seem to keep the screen alive in some cases, like if you’re watching a video with YouTube or Plex, but doesn’t do the same for Spotify. And the Spotify app doesn’t expose this as a feature.

(In fact, Spotify suggests changing the power settings. Umm, no thanks, I don’t want to waste electricity. I’d rather if your developers just fixed the app to call caffeinate whenever music is playing, that would be the best and simplest approach here. But given that Spotify has been asked about this feature 6 years ago, and they still haven’t added it, means that it’s unlikely they’ll ever have this functionality.

Apple Music seems to be impacted as well, so it’s not just third-party developers who can’t/won’t get this basic functionality working.)

After searching around a bit and asking ChatGPT about my options (link to that chat), I was about to start implementing a Golang CLI to do this work for me.

Then I stumbled onto Amphetamine! Amphetamine is an app that will prevent your computer from putting a monitor to sleep (among other features).

Amphetamine is pretty simple to use: just define the triggers that should keep the Mac awake.

At first I tried creating an audio trigger, but this didn’t work: my Mac stayed awake even if I wasn’t playing any audio.
(Not sure if this is a bug or not. It’s hard to find the home website for a given MacOS app, for some reason. And Google doesn’t help, just gives me a bunch of articles about Apple blocking Amphetamine. So I haven’t yet reported this to the developers.)

The trigger that ended up working is this one:


amphetamine-trigger-screenshot

Simply put, this trigger is active when Spotify is the frontmost application. So now all I have to do is make Spotify active, and that is enough to keep my music playing indefinitely. A simple no-code solution. Though I do wish either Apple or Spotify would include this functionality in their products from the start.