If I understand correctly you want your macro to select a target and the use three additional command on the target selected. Therefore, the final three are easiest.
What the main issue seems to be is selecting your target, here is what I came up with:
Code:
if the pet has a target
select the pet's target as my target
if (my focus has a target and the mod key is pressed)
select my focus' target as my target
if I do not have a target
select the nearest enemy as my target
if i do not have a target
stop the macro
/cast (...)
Here is the final macro I came up with.
Code:
/target [@pettarget, exists]
/target [mod, @focustarget]
/targetenemy [noexists]
/stopmacro [noexists]
/cast Hunter's Mark
/cast !auto shot
/petattack
I hope this helps