I hate to be the bearer of bad news, but what you are asking for cannot be done, as it runs afoul of Blizzard's anti-botting/-scripting security measures.
The first part of the problem is that all of the targeting functions are protected, meaning they can only be called by "secure" code (i.e. the built-in interface), not by addons or via the
/run command. As a result, changing your target can only be done via built-in slash commands like
The second part of the problem is that none of those commands support the use of Lua variables (those are what you can set in a macro). Instead, they accept either a literal unit name (e.g.
Fluffy Bunny) or a "unit ID" (which work like variables, but are set by the game's internal code).
Finally, raid target icons can only be set on unit IDs as well, making it impossible to set a raid target icon on a unit whose name isn't stored in the macro (or available via a unit ID).
There are a couple of ways to work around this, but they aren't great. The first would be to write an add-on which dynamically generates macros containing the desired name. The macro couldn't be generated in combat, but it could be run. i.e. you could only run "1st macro" before combat, but then run second whenever you like.
Alternatively, you could create a macro with a large number of
/targetexact lines — one for each mob you might wish to run the macro on.
/target commands "fall through" if no valid target is found, meaning it would use the first available mob in the list.
Normally, you would be able to use your focus target for this, but it tracks real, individual mobs. You can't use it to target other mobs of the same name/type.