Want an alias to repeat a command x times

Posted by Fraust on Sun 10 Feb 2008 03:32 AM — 7 posts, 37,363 views.

#0
I just starting using MushClient and have always been a Zmud user. My question is how I go about doing a repeat on the command line or in an alias/trigger.

For example. In Zmud I would have entered the following:

<command line> #7 drink potion

the output would have been 7 lines of drink potion.

Is there something similar for that in Mushclient?

Help!

Fraust
Australia Forum Administrator #1
There is a bit of a discussion about this here:

http://www.gammon.com.au/forum/?bbsubject_id=3311

A fairly good way is to simply add this alias (I have amended the one in the above thread a bit to use Lua, as that is now the recommended script language):


<aliases>
  <alias
   match="^#(\d+) (.*?)$"
   enabled="y"
   regexp="y"
   send_to="12"
   sequence="100"
  >
  <send>Execute (EvaluateSpeedwalk ("%1 (%2)"))</send>
  </alias>
</aliases>


See http://mushclient.com/pasting for how to add this into your world.

The alias above uses EvaluateSpeedwalk to do things repeatedly (as in, you can do 4n or 4(burp)).

However that one fails if you have a bracket in the thing you are trying to do (as that will confuse EvaluateSpeedwalk). The version below simply does a loop, so that might be better:


<aliases>
  <alias
   match="^#(\d+) (.*?)$"
   enabled="y"
   regexp="y"
   send_to="12"
   sequence="100"
  >
  <send>

for i = 1, %1 do
  Execute "%2"
end -- for loop

</send>
  </alias>
</aliases>


All that does is take the number you supply, and do the command that many times.

Maybe it is wise to check the repeat count is not too small or too big, this is my final version:


<aliases>
  <alias
   match="^#(\d+) (.*?)$"
   enabled="y"
   regexp="y"
   send_to="12"
   sequence="100"
  >
  <send>

if %1 &lt; 1 then
  ColourNote ("white", "red", "Too few repeats specified (%1)")
  return
end -- if

if %1 &gt; 50 then
  ColourNote ("white", "red", "Too many repeats specified (%1)")
  return
end -- if

for i = 1, %1 do
  Execute "%2"
end -- for loop

</send>
  </alias>
</aliases>

Australia Forum Administrator #2
The reason I used "Execute" in the script is to send whatever you typed back through the command processor.

You could substitute "Send" for "Execute" but then whatever you type is literally sent (which may be OK).

The difference shows up if you use aliases or speedwalks.

Say for example you have an alias "healme" (which might do "drink potion").

If you want to be able to type:


#5 healme


Then you want to use the alias I posted above.

See:

http://www.gammon.com.au/scripts/function.php?name=Send

and

http://www.gammon.com.au/scripts/function.php?name=Execute
Amended on Sun 10 Feb 2008 05:46 AM by Nick Gammon
#3
Okay,

I think I understand what you did. I followed your suggestion and made the alias you posted, but now when I try do to #4 burp, I get the following.

Error number: -2147352567
Event: Execution of line 1 column 13
Description: invalid syntax

Line in error:

if 4 < 1 then
Called by: Immediate execution


Suggestion?
Australia Forum Administrator #4
I mentioned above that this alias was for the Lua script language. Is your scripting language set to Lua?
Australia Forum Administrator #5
I have turned my idea into a plugin, you can use this to achieve what you want:


<?xml version="1.0" encoding="iso-8859-1"?>
<!DOCTYPE muclient>
<!-- Saved on Sunday, February 17, 2008, 2:05 PM -->
<!-- MuClient version 4.19 -->

<!-- Plugin "Repeat_Command" generated by Plugin Wizard -->

<muclient>
<plugin
   name="Repeat_Command"
   author="Nick Gammon"
   id="7f69fe2f7913b2ac312ecbad"
   language="Lua"
   purpose="Repeats a command &quot;x&quot; times"
   date_written="2008-02-17 14:04:23"
   requires="4.00"
   version="1.0"
   >
<description trim="y">
<![CDATA[
To use, type:

#n command

This sends "command" n times to the MUD.

For example:

#3 kick kobold


]]>
</description>

</plugin>


<!--  Aliases  -->

<aliases>
  <alias
   match="^#\s*(\d+) (.*?)$"
   enabled="y"
   regexp="y"
   send_to="12"
   sequence="100"
  >
  <send>
-- check at least 1 repeat
if %1 &lt; 1 then
  ColourNote ("white", "red", "Too few repeats specified (%1)")
  return
end -- if

-- check not more than 50 repeats
if %1 &gt; 50 then
  ColourNote ("white", "red", "Too many repeats specified (%1)")
  return
end -- if

-- execute (send to command processor) the command
for i = 1, %1 do
  Execute "%2"
end -- for loop

</send>
  </alias>
</aliases>

</muclient>
Australia Forum Administrator #6
This plugin is now available on the plugins page:

http://www.gammon.com.au/mushclient/plugins/

Specifically, right-click to download this file:

http://www.mushclient.com/plugins/Repeat_Command.xml

Save that to disk and install it into MUSHclient using the File menu -> Plugins.