Useful patterns in macros

This page is intended to provide patterns that may be useful in constructing your own macros, filling a niche between a reference page for macros (HOWTO: Make a Macro) and a list of generally Useful macros.
== Managing Focus ==
'Focus' is a UnitId newly available in WoW 2.0 which provides for a means for macros (and addons under keyboard/mouse control) to 'remember' some other UnitId. Unit frame addons allow a player to track the health and status of the focused unit while targeting other units. Macros can be written to cast spells on the focused unit. Moreover, click-style addons such as Clique can be used on the focus' unit frame as appropriate. These possibilities are the motivation for including focus management in macros.
== Sheep/Monitor/Resheep Pattern ==
 /focus [target=focus,exists,nodead] focus; target
 /cast [target=focus,harm,nodead] Polymorph
If you have a focus, and it exists and is not dead, then your focus is set to your focus (in other words it remains unchanged). Otherwise, focus is set to your current target. Then, now that you have a focus set, if it is an enemy, Polymorph it.
As a result, this macro can be placed on the action bar, and be used as a single-button solution to sheep an initial target (as long as you have no focus, or the one from the previous fight has died already), and to resheep as necessary.
== Last Pet Target Pattern ==
Keep in mind that this may or may not have any usefulness in combat. The following work is presented only as an exercise.
Because there's no petlasttarget function, we use the player's abilities instead. Thankfully we can accomplish this without actually altering the player's target in the end. There are really only two steps that we want to accomplish:
1. Send the pet to attack the focus. 
2. Set the focus to your pet's old target. 
These two steps will allow us to mash a single macro and have the pet go back and forth between two targets.
Since the following macro will use the focus ability to do that, you won't want to try it if you're using the focus ability for your own evil deeds, like keeping track of a seduction target, or what-have-you.
Here's the simple version. We assume that your pet is attacking someone, and that you want to send it to attack the target that you focused.
 /target pettarget
 /petattack focus
 /focus target
 /targetlasttarget
Rinse and repeat. If you met all the prerequisites, then your pet will bounce back and forth between the two targets each time you press the macro.
Now let's try adding a small amount of error correction:
 /focus [target=focus,exists,harm,nodead] focus; [exists,harm,nodead] target; [target=mouseover,exists,harm,nodead] target
 /bonk [target=focus,noexists] player
 /stopmacro [target=focus,noexists]
 /target pettarget
 /petattack focus
 /focus target
 /targetlasttarget
Step 1: if you don't have a focus, set it to your current target, or failing that, to whomever you are mousing over.
Step 2: if you STILL don't have a focus, bonk yourself on the noggin' and stop.
And then back to the meat and potatoes of the macro:
Step 3: send your pet to attack the focus.
Step 4: switch the focus to your pet's previous target.
Steps 1 & 2 make it so that you don't have to take time out of your busy schedule to focus a target before you send your pet to attack it. You just target it or mouseover it, then hit the macro, and the focusing is done for you. That's the point of macros anyway.
That takes care of the possibility of no focus when you start. But, what if your pet doesn't have a target at first? Oh, my. Well it's actually alright. A quick look shows us what will happen: your target will be set to None, the pet will attack the shiny new focus, the focus will be set to None, and your target will revert to what it was before the macro started.
So, at this point that's about as much functionality as we can reasonably ask for, but you may have noticed that we're pushing the 255 character limit. We can eliminate a few extra words that are implied by context, get rid of the bonks, and keep the same functionality:
 /focus [target=focus,exists,harm,nodead] ; [exists,harm,nodead] ; [target=mouseover,harm,nodead]
 /stopmacro [target=focus,noexists]
 /target pettarget
 /petattack focus
 /focus target
 /targetlasttarget
That's 193 characters if I count correctly.
Category:Macros