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

Discuss macro implementations, ask for macro help (to share your creations, see User Creations, probably either Campaign Frameworks or Drop-in Resources).

Moderators: Azhrei, dorpond, trevor, giliath, jay, Mr.Ice

Post Reply
raezenkane
Kobold
Posts: 4
Joined: Wed Nov 30, 2016 10:10 am
Location: New Kensington, PA

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

Post 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?
Last edited by raezenkane on Fri Apr 24, 2020 7:44 am, edited 2 times in total.

User avatar
aliasmask
Deity
Posts: 8580
Joined: Tue Nov 10, 2009 6:11 pm
Location: Bay Area

Re: Calling MT macro from Javascript

Post by aliasmask »

I think the only way is to do a form submit. Wiki should have details on forms.

raezenkane
Kobold
Posts: 4
Joined: Wed Nov 30, 2016 10:10 am
Location: New Kensington, PA

Re: Calling MT macro from Javascript

Post 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.

Merudo
Giant
Posts: 228
Joined: Wed Jun 05, 2019 7:06 am

Re: Calling MT macro from Javascript

Post 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.

raezenkane
Kobold
Posts: 4
Joined: Wed Nov 30, 2016 10:10 am
Location: New Kensington, PA

Re: Calling MT macro from Javascript

Post 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("[email protected]: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!!!

User avatar
RPTroll
TheBard
Posts: 3148
Joined: Tue Mar 21, 2006 7:26 pm
Location: Austin, Tx
Contact:

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

Post by RPTroll »

This is great.

I'm about to try implementing it. Any updates to your method?
ImageImage ImageImageImageImage
Support RPTools by shopping
Image
Image

Phergus
Deity
Posts: 7112
Joined: Fri May 12, 2006 8:56 pm
Location: Middle of Nowhere, NM
Contact:

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

Post by Phergus »

[h: processFormLink=macroLinkText("[email protected]:Torg", "all")]
Torg! Cool.

Post Reply

Return to “Macros”