(The following description and code is generated by ChatGPT)
Here’s a drop-in volume limiter for Hammerspoon (open source macOS automation app using Lua) that enforces per-output-device max volume, and automatically follows the active output device.
Paste this into ~/.hammerspoon/init.lua (or a separate file and require it), then edit the config table at the top.
(see file below)