Page 1 of 1

Calling MT macro from Javascript (fixed: I DID IT!!)

Posted: Thu Apr 23, 2020 2:30 pm
by raezenkane
I am wondering how to execute a Maptool macro from inside Javascript.
A script entry in the <head> of the sheet has the following script entry:

Code: Select all

    <script>[r:"
		function valueChange(val) {
			alert('The input value has changed. The new value is: ' + val);
		}
			"]
	</script>
I have an input on my character sheet:

Code: Select all

<p>Last Roll  <input type="text" value=[r: getProperty("LastRoll")] onchange="valueChange(this.value)"> </p>
This works; It displays the input, which has the token property, and if you change the value it pops up the javascript alert.

I want to take the value 'val' from Javascript and use somethng like:

Code: Select all

[h: setProperty("LastRoll", val)]
to change the token property.

Is this possible, and if so, how can I do it?

Re: Calling MT macro from Javascript

Posted: Thu Apr 23, 2020 4:20 pm
by aliasmask
I think the only way is to do a form submit. Wiki should have details on forms.

Re: Calling MT macro from Javascript

Posted: Thu Apr 23, 2020 5:06 pm
by raezenkane
Thank, you, AlienMask, for your quick reply.
I already use a form submit for another part of the sheet.

I would like to have an input that writes its value to a token when the value changes.
I can get onchange to call a javascript script, no problem, but I can't figure out how to call a MT macro from javascript.

Re: Calling MT macro from Javascript

Posted: Thu Apr 23, 2020 10:25 pm
by Merudo
raezenkane wrote:
Thu Apr 23, 2020 5:06 pm
Thank, you, AlienMask, for your quick reply.
I already use a form submit for another part of the sheet.
You can have multiple forms, which can be submitted individually with javascript.

Re: Calling MT macro from Javascript

Posted: Fri Apr 24, 2020 7:15 am
by raezenkane
I DID IT!!!!
<doin' the Snoopy dance>
I made an HTML5 frame.

I built a form, like so:

Code: Select all

[h: processFormLink=macroLinkText("processForm@Lib:Torg", "all")]
<form name="LastRollForm" id="LastRollForm" action=[r: processFormLink] method="get">
	<input type="hidden" name="tokID" value=[r: TokID]>
	<input type="hidden" name="tokTorg" value=[r: tokTorg]>
	<input type="hidden" name="mName" value=[r: getCurrentMapName()]>
	<br><br>
	Last Roll
	<input name="LastRoll" id="LastRoll" type="number" onchange="valueChange('LastRoll', 'LastRollForm')" value='[r: getProperty("LastRoll")]'>
</form>
I created a Javascript script and put it in character sheet's <head>, like so:

Code: Select all

<!--submits form on value change-->
    	<script type="text/javascript">
    		[r:"function valueChange(id, form) {
			document.forms[form].submit();
		}
		"]
	</script>
...and, I created a macro for the form submit action, like so:

Code: Select all

	<!-- Get macro args-->
	<!--tokID,tokTorg,mName,textInputName-->
	[h: mArgs=macro.args]
	<!-- Get Token ID-->
	[h: TokID = json.get(mArgs, "tokID")]
	<!--Get Map Name of Target Token-->
	[h: mName = json.get(mArgs, "mName")]
	<!-- Get Torg Library Token ID-->
	[h: tokTorg = json.get(mArgs, "tokTorg")]
	<!-- Remove Non-Skill related entries in mARgs-->
	[h:mArgs = json.remove(mArgs, "tokID")]
	[h:mArgs = json.remove(mArgs, "mName")]
	[h:mArgs = json.remove(mArgs, "tokTorg")]
	<!--Get Tken Property Name from mArgs-->
	[r: tokProp = json.fields(mArgs)]
	[r: propValue = json.get(mArgs, tokProp)]
	[r: setProperty(tokProp, propValue, tokID, mName)]
So, you change a value in the text input box, it fires the onchange event which calls the javascript script.
The javascript 'submits' the form, which runs the MT macro.
The MT macro grabs the changed value and (silently and invisibly) writes it to the Token Property!

This will work for Proof of Concept.
I have to add error checking and stuff, but...IT WORKED!!!

Re: Calling MT macro from Javascript (fixed: I DID IT!!)

Posted: Fri May 22, 2020 12:14 pm
by RPTroll
This is great.

I'm about to try implementing it. Any updates to your method?

Re: Calling MT macro from Javascript (fixed: I DID IT!!)

Posted: Fri May 22, 2020 12:30 pm
by Phergus
[h: processFormLink=macroLinkText("processForm@Lib:Torg", "all")]
Torg! Cool.

Re: Calling MT macro from Javascript (fixed: I DID IT!!)

Posted: Thu Apr 08, 2021 8:35 am
by wolph42
how did i ever miss this post. good to know this is possible!