This page documents a user-defined function that you can copy and paste into your addon. Replace PREFIX with your addon or lib prefix to avoid conflicts between different versions of these functions.

User defined functions

The following snippet provides a global function RunSlashCmd that can be used to execute an arbitrary slash command (i.e. "/myaddon hi").

local _G = _G
function RunSlashCmd(cmd)
  local slash, rest = cmd:match("^(%S+)%s*(.-)$")
  for name, func in pairs(SlashCmdList) do
     local i, slashCmd = 1
        slashCmd, i = _G["SLASH_"], i + 1
        if slashCmd == slash then
           return true, func(rest)
     until not slashCmd
  -- Okay, so it's not a slash command. It may also be an emote.
  local i = 1
  while _G["EMOTE" .. i .. "_TOKEN"] do
     local j, cn = 2, _G["EMOTE" .. i .. "_CMD1"]
     while cn do
        if cn == slash then
           return true, DoEmote(_G["EMOTE" .. i .. "_TOKEN"], rest);
        j, cn = j+1, _G["EMOTE" .. i .. "_CMD" .. j]
     i = i + 1

Usage[edit | edit source]

RunSlashCmd("/addon do this")

Alternative[edit | edit source]

Another method to accomplish the same effect is to insert the command into an editbox and send the command. Note that it's usually better to do this with your own EditBox to avoid taint or conflicts with user input into the default editbox.

-- Create our editbox (may not need a name, untested)
local editbox = CreateFrame("EditBox", "myEditBox", UIParent, "ChatFrameEditBoxTemplate")

editbox:SetText("/command here") 

However as of 3.3.5 you must set the chatFrame variable on your editbox before the OnLoad function runs. So if you want to use the above code you need to create the frame in XML or create a template and then use that template to create the frame.

<EditBox name="MyAddOnEditBoxTemplate" inherits="ChatFrameEditBoxTemplate" virtual="true">
       self.chatFrame = self:GetParent();

And then use:

-- Create our editbox
local editbox = CreateFrame("EditBox", "MyAddOnEditBox", UIParent, "MyAddOnEditBoxTemplate")

editbox:SetText("/command here") 
Community content is available under CC-BY-SA unless otherwise noted.