I'm a big fan of Rogue Amoeba and their excellent audio tools. I use SoundSource to route audio from particular macOS apps to specific output devices at specific volumes. Excitingly, they've just added Shortcuts-based automation
I've written a Shortcut to change the output device and volume for a particular application (that link is for Music, this one is for Safari), and KM macros to call them. I use this as part of a chain of macros when I swap between different audio contexts (from a conference call to listening to music, say).
Toggle description of the technical details
The macros use KM's v10.1's new Execute Shortcut action. This is the first time I've done any automation with Shortcuts, so any advice on how to improve it would be welcomed. I haven't done much error handling yet but it seems to be working well so far.
I wasn't sure whether it would be better to create separate Shortcuts to change device and volume, but I typically change them both at the same time - and I was originally hoping for a single Shortcut which would take source application, output device, and target volume as parameters. That turned out not to be possible, as the SoundSource Shortcut couldn't convert a text parameter into an application object, so I had to duplicate the Shortcut for each app I wanted to manage in this way (currently only Safari and Music). Rogue Amoeba have kindly said that they'll look into adding a feature to help with this. (I solved a similar problem with the name of the output device by looping round all of the possible output devices until one with a matching name was found, but that wasn't possible with the application name).
PS I've linked to this post from the equally lovely Automators forum.
Here's an example macro which calls the main subroutine...
Set Music volume to 100% and SoundSource to No Redirect at 100%.kmmacros (1.9 KB)
The main subroutine calls the Shortcut ...
Change SoundSource device-volume for Music.kmmacros (4.4 KB)
... and this subroutine to start SoundSource if it is not running:
Check that SoundSource is running- and activate it if not.kmmacros (6.4 KB)
And here are screenshots of the Shortcut itself, showing its magic variables (the one for Safari is identical apart from the name of the application), so that you can see what you get before you download it...