WoW:USERAPI RunSlashCmd: Difference between revisions

From AddOn Studio
Jump to navigation Jump to search
m (winner)
No edit summary
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
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


 
local _G = _G
  function RunSlashCmd(cmd)
  local 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 in pairs(SlashCmdList) do
       local i = 1
       local i = 1
       local slashCmd
       local slashCmd
       repeat
       repeat
         slashCmd = getglobal("SLASH_"..name..i)
         slashCmd = _G["SLASH_"..name..i]
          
          
         if slashCmd == slash then
         if slashCmd == slash then
Line 16: Line 16:
         end
         end
         i = i + 1
         i = i + 1
       until slashCmd == nil
       until not slashCmd
   end
   end
  return false
  end  
  end  


RunSlashCmd("/help")
=== Usage ===
 
RunSlashCmd("/help")
RunSlashCmd("/addon do this")
RunSlashCmd("/addon do this")


Try  
Try  
  local t={}; t.GetText = function(self) return self.text end; local function RunSlash(slash) t.text = slash; ChatEdit_ParseText(t) end
  local t={}; t.GetText = function(self) return self.text end; local function RunSlash(slash) t.text = slash; ChatEdit_ParseText(t) end
== 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)

Revision as of 21:40, 19 December 2007

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

local _G = _G
local function RunSlashCmd(cmd)
  local slash, rest = cmd:match("^(%S+)%s*(.-)$")
  for name in pairs(SlashCmdList) do
     local i = 1
     local slashCmd
     repeat
        slashCmd = _G["SLASH_"..name..i]
        
        if slashCmd == slash then
           -- Call the handler
           SlashCmdList[name](rest)
           return true
        end
        i = i + 1
     until not slashCmd
  end
end 

Usage

RunSlashCmd("/help")
RunSlashCmd("/addon do this")

Try

local t={}; t.GetText = function(self) return self.text end; local function RunSlash(slash) t.text = slash; ChatEdit_ParseText(t) end

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)