WoW:USERAPI RunSlashCmd: Difference between revisions

m
Move page script moved page USERAPI RunSlashCmd to WoW:USERAPI RunSlashCmd without leaving a redirect
m (winner)
m (Move page script moved page USERAPI RunSlashCmd to WoW:USERAPI RunSlashCmd without leaving a redirect)
 
(6 intermediate revisions by 6 users not shown)
Line 1: Line 1:
Thanks to cladhaire and Josh_Borke for all the effort in coming up with this code that allows you to pass a /command to wow from a lua script. The following code is cladhaire's final product
{{userfunc}}
 


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)
  function RunSlashCmd(cmd)
   local slash,rest = cmd:match("^(%S+)%s*(.-)$")
   local slash, rest = cmd:match("^(%S+)%s*(.-)$")
   for name in pairs(SlashCmdList) do
   for name, func in pairs(SlashCmdList) do
       local i = 1
       local i, slashCmd = 1
      local slashCmd
       repeat
       repeat
         slashCmd = getglobal("SLASH_"..name..i)
         slashCmd, i = _G["SLASH_"..name..i], i + 1
       
         if slashCmd == slash then
         if slashCmd == slash then
             -- Call the handler
             return true, func(rest)
            SlashCmdList[name](rest)
        end
             return true
      until not slashCmd
  end
  -- 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);
         end
         end
         i = i + 1
         j, cn = j+1, _G["EMOTE" .. i .. "_CMD" .. j]
       until slashCmd == nil
      end
       i = i + 1
   end
   end
  return false
end
  end
 
=== Usage ===
RunSlashCmd("/help")
RunSlashCmd("/addon do this")
 
 
== Alternative ==
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")
ChatEdit_SendText(editbox)


RunSlashCmd("/help")
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.


RunSlashCmd("/addon do this")
<EditBox name="MyAddOnEditBoxTemplate" inherits="ChatFrameEditBoxTemplate" virtual="true">
  <Scripts>
    <OnLoad>
        self.chatFrame = self:GetParent();
        ChatEdit_OnLoad(self);
    </OnLoad>
  </Scripts>
</EditBox>


Try
And then use:
  local t={}; t.GetText = function(self) return self.text end; local function RunSlash(slash) t.text = slash; ChatEdit_ParseText(t) end
-- Create our editbox
  local editbox = CreateFrame("EditBox", "MyAddOnEditBox", UIParent, "MyAddOnEditBoxTemplate")
editbox:SetText("/command here")  
ChatEdit_SendText(editbox)
[[Category:User defined functions]]
Anonymous user