UI FAQ/Macros and scripts

The UI FAQ is posted here on Wowpedia as well as on Blizzard's UI & Macros Forum and at WoWInterface.com.
Some of the texts will direct you to "post followups" and the like; this is referring to those two forums.
== What? (Definitions) ==
No, really. What the heck are macros and scripts? 
=== Macros ===
Macros are a list of commands designed to be executed in series.
A macro can consist of any number of Slash commands or pure text to up 255 characters. With some exceptions, these commands behave exactly as if you had typed them into the chat window. Users wishing to use more advanced commands are recommended to see the Interface Customization and World of Warcraft API sections at wowpedia.org.
=== Scripts ===
Usually, not just in WoW, the term Script refers to a short to medium length program, written in some kind of simple programming language (called a scripting language). The purpose of a script is usually to control another program.
World of Warcraft has a powerful programming language called Lua embedded inside it, which is used for scripting. What this means is that WoW is able to understand and execute little programs written in the Lua language. However, in WoW, a script is usually not something that you use by itself, i.e. you don't just type in a script and execute it (although this is also possible). Instead, there are several places in WoW where you have the option to enter scripts as part of something else.
The kind of places that you can use a script:
;/script [command] or /run [command]: If you type the slash command "/script" or equivalently "/run" in the chat box, you can follow it with one or more valid Lua language statements (i.e. a script).
;Macros: you can enter scripts as part of macros, by putting one or more "/script" commands in your macro.
So what can a script do? There are many resources on Lua scripts (the main one being http://www.wowpedia.org); However, the most important point is that scripts are able to perform many more game actions than slash commands. This is done via a set of functions (called API or Application Programming Interface) that WoW makes available for use in scripts. There is a quite extensive list of all the API functions available for use in scripts at http://www.wowpedia.org
NOTE: Scripts are now mainly reserved for UI, the allowable API used within the /script tag is greatly reduced. It is however still usable.
If you wish to create a macro to cast spells or use an item, refer to the 2.0 Macro guide. The majority of the new limitations on the "/Script" tag cover these actions. For miscellaneous actions such as switching weapons, playing music, or sending chat messages; scripts still work fine. --Digital Utopia 05:25 2 February 2007 (CST) 
== Where? (Where can I learn more about macros / scripting?) ==
The Blizzard UI & Macros Forum, for one. Another great source of info is this site.
Also:
* http://www.wowpedia.org/Useful_macros for even more pre-made macros
* http://www.rpgexpert.com/4118.html for help with macro commands and scripting in general.
== How? (How do I use?) ==
=== General ===
NOTE: Macro scripts cannot be used in combat anymore
How do I create a macro? 
In a chat box, start by typing /macro. then click NEW, give it a name and an icon, and then start entering some commands.
How can I make a conditional macro? 
To use conditionals, you'll need to invoke the LUA interpreter. To do that, you start a macro with "/script" Anything after /script will be run as lua code (or a 'chunk' as lua calls it). It is VERY important to keep all parts of a Lua chunk on ONE LINE in a macro, and make it fit within 255 characters.
You can then use the "if ... then ... end" codeblock to complete your macro. it should look similar to this when you're done:
NOTE: As of 2.0.1 you may not use anymore conditional macros to initiate spellcasting, give orders to your pet or use an item.
 /script if UnitName("target") == "fred" then SendChatMessage("Hi fred!","WHISPER",nil,"fred"); end
Note the use of "end" at the end. It's important to Lua.
When using Lua script code, do I need to end each line with a semicolon? 
Lua doesn't require this, but you may do so if you like. It does make it easier to read, but Lua does have strict enough syntax that it can understand commands without semicolons. Even something as ugly as this is legal: /script a =5 b =6 Message(a..b)c= a +b
How do I output text to the chat window?
Use DEFAULT_CHAT_FRAME:AddMessage(msg,r,g,b,a), where msg is the text, and "r,g,b" are red,green,blue (0-1) and "a" is alpha (transparency; 0 for completely transparent, 1 for completely solid). The alpha parameter can be left out to mean "completely solid".
This is easier if you write a function that calls the above, :AddMessage(), function:
 function cprint(...)
 DEFAULT_CHAT_FRAME:AddMessage(...)
 end
Then you can use cprint("foo",1,0,0) multiple times in the macro without it taking quite so many characters.
NOTE: As of 3.0, print() is now available.
Is it possible to wait in a macro for a certain amount of time, then continue running the rest of the macro? 
No, it is not possible. See the question regarding multiple abilities in one macro for a more detailed explanation of the sorts of things not being able to wait prevents you from doing.
ADDENDUM:
It is possible, if you have the Chronos stand-alone library, the AceEvent-2.0 stand-alone library, or any addon that uses either of those installed, to do something like:
 /in 5 /say Hello,
 /say Fred!
And in 5 seconds you will say "Hello,". But you will say "Fred!" right now. /in is not a macro pause command, it simply lets you queue up a single command to execute at a future time.
HOWEVER, you cannot use /in to cast a spell, use an ability, or run a macro, due to the hardware event restriction. So, its usefulness is very limited. You can use it to talk, or to use items, but that's about it. Which is one reason I didn't mention it in the first place. Because 99% of the time people want a pause, its so they can cast another spell in the same macro, or switch inventory items and then use an ability, neither of which is possible using /in.
Why won't this macro / addon work? I'm almost positive my code is correct! 
For macros: double- and even triple-check your syntax AND SPELLING. Remember that Lua is case-sensitive! Triple-check the names of any spells, as well. CastSpellByName("Curse of Shadows(Rank 2)") is NOT the same as CastSpellByName("Curse of Shadow(Rank 2)")
For AddOns: Follow the advice given in the "For macros:" section above. Remember that both Lua *and* XML are case-sensitive! </OnLoad> is NOT the same as </onload>. If your AddOn is listed in the [AddOns] list at character select, but doesn't seem to be loading in-game, check your XML, you likely have an error there somewhere.
If you've tried the above and STILL can't get your script to work: Make a NEW post in this forum asking for help. Please DO NOT clutter up this thread asking for help with your broken macros!
How do I register a /slash command? 
In my UI AddOn, what do I need to do to create a new "/slashcommand" that the user can type into the chat box?
Slash command information is stored in two places. The first is a global table named SlashCmdList. In order to add a new command, create a new function with one argument. That argument will contain whatever text the user typed after the name of your command.
MyAddon = {};
function MyAddon.SlashCommand(Argument)
DoSomethingWith(Argument);
end
Then create a new entry in the table for your new slash command.
SlashCmdList["My_AddOn_Slash_Command"] = MyAddon.SlashCommand;
The second place slash command information is stored is in a haphazard series of global variables. These variables' names all start with "SLASH_", then the name of an entry in SlashCmdList, and end with a digit. The value of each variable is the text the user types to active the command. It's important to note that while the names of the variables are case-sensitive, the command the user types is case-insensitive (so "/MASC", "/Masc" and "/masc" all do the same thing.
SLASH_My_AddOn_Slash_Command1 = "/MASC";
SLASH_My_AddOn_Slash_Command2 = "/MASlashCommand";
SLASH_My_AddOn_Slash_Command3 = "/MyAddonSlashCommand"; 
=== Spells, Abilities and Buffs ===
What is the best way to know the exact spelling of a spell to cast? 
Open your spellbook along side your macro editor. Position the edit cursor in the macro window, and then hold down the SHIFT key while you LEFT click on a spell icon in your spellbook, and the game will type in the correct /cast spellname command for you. You can then cut/paste or edit it if needed.
Can I make a conditional macro that does something if I have a certain buff? 
You can't do that with such a macro since 2.0.1.
Is it possible to use multiple abilities in one macro, such as cast Corruption, Curse of Agony, and then Shadowbolt? 
No. The reason is that it is not possible to wait in a macro, but to cast one spell after another, you would have to cast the first one, wait for it to finish, then cast the other. Even instant spells activate a global cooldown timer of 1.5 second (or 1 second for rogues/warriors/druids in bear/cat form), and you cannot cast another spell until this timer is done. Even with the ability to wait in a macro, you wouldn't be able to cast multiple spells: an action from the user is required to start a spell cast (like a keypress or a mouse click, not at any other time - which is why "do something, wait a bit, then cast XXX" is impossible).
It is, however, possible to do multiple things that don't require waiting. You could, for example, cast a spell and turn on attack. Or use two items (as long as they had independent refresh timers).
You just can't do anything that would require waiting between action A and action B.
NOTE: It is possible, however, since 2.0.1, to define a /castsequence macro which would allow you to cast a different spell on each button click. See 2.0 macro guide for more details.
How do I set up a macro or script to automatically cast XXX when... 
You don't. There is no way to automatically cast any spell or ability. You can ONLY use spells and abilities in response to a hardware event (mouse button or keyboard press), and furthermore, it appears that the hardware event must trigger a normal action button or the spell will fail to cast. Furthermore, since 2.0.1, many cast/use/pet functions have been protected and cannot be used directly by AddOns and scripts.
How do you link multiple spells together in a single macro? 
Spells can be linked into one macro if the first one does not activate the global cooldown (such as Nature's Swiftness). To do so, simply put the two spells on two different lines. E.g.
 /cast Presence of Mind
 /cast Pyroblast(Rank 1)
See also the Addendum on chaining spells at the end of this FAQ.
 The above has been rewritten to match 2.0.1 new restrictions. 
 The rest of the FAQ also needs to be updated. Everything below this message is still pre-2.0.1 documented.
=== Interacting with the World ===
Can you help me write a macro that will automatically loot a corpse / automatically skin and loot a corpse / automatically pickpocket and loot?
No. There is no way to loot items except via mouse clicks.
How do I make a macro that jumps and spins 180 degrees?
Movement related functions now require a hardware event to work so such a macro is no longer feasible. Even when it was, you could only turn at the same speed that the keyboard can turn you which is too slow to be usable in PvP. It is better to practice this maneuver using the mouse.
The default Blizzard UI has a camera function (Main Menu->Key Bindings->Camera Functions) called "Flip Camera" which rotates the camera (but not the character) 180 degrees.
How do I make a macro/addon that tells me the distance to my target?
The ability to obtain the exact distance between you and a non-friendly target was removed. At best, you can now only obtain a range (i.e.: Between 36 and 30 units) based on which abilities on your action bar are usable (IsActionInRange(slot)). 
You can get some more range information by using CheckInteractDistance(unit, distanceType)
Can I make a Macro that automatically picks up the WSG flag? 
This is not possible. Any interaction with the 3d world (talking to NPCs, looting...) must be done via mouse click
== Addendum on chaining spells ==
Despite the popular (and usually correct) notion that you can only cast a single spell or use a single item per button press, it is possible to do so. However, what spells you can chain together is extremely limited. If you read nothing else in this post, at least read the Limitations section before posting questions.
;Limitations
You can only cast spells during a button press. You cannot have the macro wait for any period of time and then cast the spell. The limiting factor is cooldowns. Casting a spell (usually when you start to cast) usually activates a global cooldown. You can only chain together spells that could otherwise be cast together near-instantly. This means that the first spell must be instant, and it must not activate a cooldown that prevents the second spell from being used immediately.
;What You Can't Do
All of you that are looking for a way to cast Frostbolt, followed by Arcane Missiles, followed by Frost Nova, followed by Blink, can just give up now. The first spell (or item) must be instant and incur neither the global cooldown nor the next spell's (or item's) cooldown. You cannot code the macro to wait the global cooldown before proceeding to the next spell.
;What You Can Do
There are a few spells, usually a couple per class, that fit the rules for chaining spells. They're usually spells that affect the next casted spell, or abilities that affect the next used ability. This includes abilities such as Divine Favor, Nature's Swiftness, Presence of Mind, Heroic Strike, Raptor Strike. They have no casting time (instant) and they do not activate the global cooldown.
;How Do You Do It
The script for doing it is relatively simple. Make a macro to cast the first spell that fits the above rules. After that, you can cast whatever you want that isn't on cooldown. For example:
 /cast Nature's Swiftness
 /cast Healing Wave
That gives you a single button press to cast an instant Healing Wave. Another example:
 /cast Presence of Mind
 /cast Pyroblast
That gives you an instant Pyroblast. Here's a nice one for Rogues with First Aid:
 /cast Gouge
 /use Netherweave Bandage
Spell that are 'On next Attack', like Heroic Strike or Raptor Strike can also be chained in macros. Hunters should like this one:
 /cast Raptor Strike
 /cast Wing Clip
That's will do a Wing Clip immediately, and a Raptor Strike on the next attack (which might be immediately following the Wing Clip, if the weapon speed timer is up). Warriors could do the same thing with Heroic Strike and Hamstring, though that's quite a lot of rage to burn through in their case.
;Items Can Be Used As Well
Most items aren't hit by the global cooldown, which means that spells that activate the global cooldown, can still be chained with many items. Druids could make an emergency self heal that chained Rejuvenation with using a healing potion. I like to have my Lightning Shield up at all times, so I chain that before using my mount. Some trinkets can be used and immediately followed by casting spells (such as the fire trinket from the new lvl50 Mage quest).
;Tips for Effective Use
This functionality is best used to enhance your existing play style, not totally change it. For example, a warrior (especially one who has the talents to reduce their bloodrage damage) could chain bloodrage before some/all attacks (Bloodrage+Execute would be useful), to make sure they're using it whenever possible. You could make sure you're making the most of your Elemental Mastery/Presence of Mind by attempting to cast it with every Chain Lightning/Pyroblast you cast. Explore the many possibilities!
Many of these macros are best used in emergency situations, especially the NS+Heal ones. If you were to already have a spell casting when you hit the emergency heal button, it would begin casting the heal without NS. To get around this, if you have the room in the macro (macros can only be 255 characters long, remember), add a '/script SpellStopCasting()' line at the top of your macro. That will make it abort whatever spell you might already have been casting, and do the NS+Heal. Note that this doesn't just apply to NS+Heal; I use it for my EM+ChainLightning.
Please note that the examples here are just examples of how to chain spells together. In order to be very useful, some of them (ie the healing ones) will require some other addon or script to handle their targeting, so that your heal targets yourself if you don't have another friendly unit targeted, or automatically targets a certain party member, or whatever target you want.
;Chaining More Than 2 Spells
Though even more rarely useful, it is possible to chain more than just 2 spells/items. For example, we could enhance that druid emergency heal even further:
 /cast Nature's Swiftness
 /cast Healing Touch
 /use Major Healing Potion
;Exceptions
One thing to note about this is that if you chain Nature's Swiftness or Presence of Mind together with a spell with a cast time, it won't work while moving, whereas it would if you casted them separately.
The reason for this is because the client doesn't know that Nature's Swiftness or PoM is up so therefore denies the spell from being cast. This is due to the time required to communicate with the server - even latency of 1ms would cause this issue. Your latency would have to be effectively 0 (in actuality somewhere in the nanosecond range, the exact latency would depend on how fast your computer would be able to process the macro) which is not possible. This is a limitation of current technology and I suspect that it will always be the case.
This functionality also doesn't work with form or stance switches, where the second spell is only available in the form or stance you are changing to. This is probably because the client has not been told by the server that the change really took place. For example, the following can be used to revert from any druid form, and start tracking herbs.
 /script local j,_,a;for i=1,GetNumShapeshiftForms() do _,_,a=GetShapeshiftFormInfo(i);j=a and i or j;end j=j and CastShapeshiftForm(j);
 /stopcasting
 /cast Find Herbs
I really wanted a 'switch-back-to-caster-form-and-start-healing' macro, but it doesn't look like that's possible. If anyone has any way to get it to work, I'd love to know.
;A few more examples
Vanish+(anything that requires Stealth) - this will never work unless you're in Stealth already when you vanish, since the client will deny use of abilities that require stealth until the server tells it you're in stealth.
Any Seal+Judgement - this will fail unless you have an active Seal. This doesn't have to be the same seal as the one you are trying to Judge, any seal will do for the purposes of validating the Judgement spell in the client. However, there are no issues with Divine Favor+Holy Light macros.
;Is This An Exploit?
Nope. Slouken has confirmed that this is an allowed practice. It doesn't get around cooldowns in any way. Have fun!
Category:UI
Category:FAQs
Category:Macros