If you're going to use the "Execute a Shell Script" action in the future, take a moment to understand how you "broke" things and why this fixes it -- my explanation feels really clumsy, so if I've confused more than helped just say!
By which time its probably better to give up on all the complicating "If..."s, "Appends..." and so on and just go with a hard-coded list in KM's "friendly list" format. Added benefit is that you can use whatever names are best for you (see the last 4 lines of the list).
But I'm betting that you will, eventually, want more than this. At which point split each item into its own "opener" macro so you can do things like "open Obsidian at this vault, put the window to the top left of the display and select today's Today note, creating one if it doesn't already exist". Much more manageable is separate macros than one huge, case-determining, monster.
The way I would do that is to follow your actions above with this:
Each block of code should probably start with a "pause until [app] is at front" action.
P.S. I was working on a single sed command that could append a path if a "." character was missing, but I guess that's not really helpful now, since we know now that the paths are different for many apps.
Yes, this is really the solution to providing the macro I want. Perfect! The only thing it doesn't do that my complex kluge did is append currently-open apps that are not in the hard-coded list. But that is easily addressed with a separate two-action macro.
Perhaps, but not as a replacement for this. This is really a kind of keyboard-based replacement for the Dock.
It wouldn't be that much work to combine the currently-running apps list from the other thread with your hard-coded one (stripping duplicates for display purposes) -- the %ApplicationPath%Foreground% token would come in useful here, as would the fact that using the "Open" action with a path to an already-open app will frontmost the app and not launch a second instance.
If you are like most of us, this will start to grow as you notice things like "every time I launch app A I want document 1 to open, unless I'm in focus mode blah in which case open document 2". As you add more and the complexity increases it's good to know that you can hive off entire sections as new macros that can be called from this one.
The easy way makes use of the fact that both %ApplicationPath%Foreground% and %ApplicationName%Foreground% return their lists in application index order -- collect both, then iterate through combining line by line:
...although there's a one in a million (billion?) chance you could change app order in the microseconds between the two actions!
Perhaps safer, certainly shorter, but a bit more work for the OS, would be to collect the paths as above then "For Each..." them with "Get Attribute... Display Name":
One reason for not extracting the name from the path is that it isn't always "correct" -- for example, Things3.app actually has the display name "Things". Not really an issue here, but something to remember in the future.