Execute a script on hyperlink click?

Posted by Kahenraz on Mon 19 Mar 2018 12:29 PM — 12 posts, 42,602 views.

#0
From what I can tell the action performed on a hyperlink is sent to the world. Is it possible to have it instead execute some lua code?
USA Global Moderator #1
Yeah. It's a little buried inside the docs, but if you look at https://www.gammon.com.au/scripts/doc.php?function=Hyperlink you should find a section that starts

Quote:
From version 3.42 you can also call a script in a plugin from a Hyperlink


The description could be cleaned up, because it looks like it was written specifically for VBscript, but mostly you can just mentally replace any use of the word "sub" with "function" and then "&" between strings becomes ".." for Lua
#2
I can't find a way to make it work for the current scope. For example, the documentation says that I must provide a plugin id but I want to call a function that is already accessible and is not part of any plugin.

Is there a plugin id that I can provide that represents the current scope?
USA Global Moderator #3
Quote:
Is there a plugin id that I can provide that represents the current scope?

I don't think so.
Australia Forum Administrator #4
About the only way you can do that is make a small plugin (there is a plugin wizard that makes writing plugins easy) to be the target of the hyperlink. If you are desperate to go back to global script space have the plugin "Execute" some alias that the main script then catches and acts upon.

Template:function=Execute
Execute

The documentation for the Execute script function is available online. It is also in the MUSHclient help file.



You could pass arguments to the executed alias.

eg.


function hyperlinkCallback (arg)
  Execute ("6f5b06cfe811fb62ba399eb0 " .. tostring (arg))
end -- of hyperlinkCallback


That first thing is just some random string that hopefully you don't normally type. Then make an alias in the main world script that matches:


6f5b06cfe811fb62ba399eb0 *


Now you know when the hyperlink was clicked and what the argument to it was.




An example of such a plugin would be:

Template:saveplugin=Hyperlink_Redirector
To save and install the Hyperlink_Redirector plugin do this:
  1. Copy the code below (in the code box) to the Clipboard
  2. Open a text editor (such as Notepad) and paste the plugin code into it
  3. Save to disk on your PC, preferably in your plugins directory, as Hyperlink_Redirector.xml
    • The "plugins" directory is usually under the "worlds" directory inside where you installed MUSHclient.
  4. Go to the MUSHclient File menu -> Plugins
  5. Click "Add"
  6. Choose the file Hyperlink_Redirector.xml (which you just saved in step 3) as a plugin
  7. Click "Close"
  8. Save your world file, so that the plugin loads next time you open it.



<?xml version="1.0" encoding="iso-8859-1"?>
<!DOCTYPE muclient>
<muclient>
<plugin
   name="Hyperlink_Redirector"
   author="Nick Gammon"
   id="54ba3415104e5371948c933d"
   language="Lua"
   purpose="Catches a hyperlink function call and sends to execute"
   date_written="2018-03-20 07:37:13"
   requires="5.00"
   version="1.0"
   >
</plugin>

<!--  Script  -->

<script>
<![CDATA[
function hyperlinkCallback (arg)
  Execute ("6f5b06cfe811fb62ba399eb0 " .. tostring (arg))
end -- of hyperlinkCallback
]]>
</script>
</muclient>





Example alias:


<aliases>
  <alias
   match="6f5b06cfe811fb62ba399eb0 *"
   enabled="y"
   send_to="12"
   sequence="100"
  >
  <send>Note ("Hyperlink clicked with argument: %1")</send>
  </alias>
</aliases>


Template:pasting
For advice on how to copy the above, and paste it into MUSHclient, please see Pasting XML.





Now you can make your hyperlink as you have a plugin ID:


Hyperlink ("!!54ba3415104e5371948c933d:hyperlinkCallback(foo)", "Shown text", "hover text", "yellow", "green", 0)
#5
That's a pretty complicated workaround. Would it be too much to ask to get this kind of support added natively for a future update?
Australia Forum Administrator #6
Actually it is much simpler than that. Re-reading the source gives this solution:


Hyperlink ("foo", "Shown text", "hover text", "yellow", "green", 0)


The hyperlink action is executed (sent to the command processor) so all you have to do now is make an alias that matches "foo".

That alias can execute a script. Make the word "foo" something long and complicated as you don't want to accidentally fire if someone types it. My earlier example of a long hex string would be fine:


Hyperlink ("6f5b06cfe811fb62ba399eb0", "Shown text", "hover text", "yellow", "green", 0)
Australia Forum Administrator #7
You could be fancier and make an alias that just executes arbitrary scripts, eg.


<aliases>
  <alias
   match="6f5b06cfe811fb62ba399eb0 *"
   enabled="y"
   send_to="12"
   sequence="100"
  >
  <send>%1</send>
  </alias>
</aliases>


All that does is send its argument as a script. Now your hyperlink could be:


Hyperlink ("6f5b06cfe811fb62ba399eb0 print ('hello world')", "Shown text", "hover text", "yellow", "green", 0)


The script "print ('hello world')" will be passed as a wildcard to the alias matching '6f5b06cfe811fb62ba399eb0 *' which will then execute it. That way you could run any script you like, with a single alias as the go-between.
Amended on Tue 20 Mar 2018 09:47 PM by Nick Gammon
#8
I appreciate the workarounds but they all still add a lot of unnecessary coupling and complication.

Would it be possible to simply implement the ability to call a local function with "!!func()" rather than the form "!!plugin:func()"?
Amended on Tue 20 Mar 2018 09:48 PM by Kahenraz
Australia Forum Administrator #9
It's not that easy because the ordinary scripting interface has to allow for multiple languages, some of which have fixed numbers of arguments (unlike Lua).

If I implement !!func() next someone will want !!func(arg1,arg2,arg3) and then things will get complicated.

A single redirecting alias, as suggested in my previous post, is hardly a lot of extra complication. It's one alias that would let you call arbitrary scripts with arbitrary numbers of arguments.
#10
(deleted)
Amended on Wed 21 Mar 2018 08:32 AM by Kahenraz
#11
An alternative would be to mention in the docs that it's a Lua-exclusive feature, since that tends to be the most common language used when scripting for Mush anyways.