Skip to main content
Inspiring
September 25, 2023
Question

Possible to limit spawn(ed) page(s) using script in a calculate field to a single execution?

  • September 25, 2023
  • 1 reply
  • 5131 views

I am having difficulty limiting the following script to a single execution upon tabbing to the next field... I've tried placing it in the "Actions-->Mouse Up-->Run a JavaScript" where it will only execute after tabbing out of the field and then going back to the field (via 'shift-tab' or mouse cursor, which is entirely unacceptable. I've placed it in the calculate field to ensure it runs after a numeric entry as I have not figured out a way to avoid the 'Run a JavaScript' tabout/mouse cursor limitation.

 

Is there possibly a line of script that may prevent the script from running more than once in the calculate field after it has executed?  Or is there a method that can be implemented within the 'Run a Javascript' to ensure single execution upon tabbing to the next field?

My sincerest thanks in advance for your assistance.

 

EDIT:  Apologies, I failed to add a test reference document.  


The script works as written excepting the subject line 

 

var templateCountField = this.getField("EQUIP.No");
var pageCount = parseInt(templateCountField.value);
var temp

if (!isNaN(pageCount) && pageCount > 0) {
for (var i = 0; i < pageCount; i++) {
this.getTemplate("EQUIP.TEMP").spawn();
}
} else {
app.alert("Please enter a valid positive number in the 'Equipment Number' field.");
}

this.getField("Narrative").setFocus();

This topic has been closed for replies.

1 reply

Thom Parker
Community Expert
Community Expert
September 25, 2023

So, the mouseUp event is not triggered on a tab. Scripts that spawn page tempates are best placed in a button MouseUp script.

 

Putting it in the calculation script is not a good idea. If you detecting a change in the field value is the trigger for spawning, then use the custom Validation script. 

 

Now, there is an error in your script. Do not use "this.getField" to get the value of the field in which the script is executing. This doesn't work because the field is in transition. The value has not settled.  The field object and the changing value are members of the event object. 

Here's the reference entry:

https://opensource.adobe.com/dc-acrobat-sdk-docs/library/jsapiref/JS_API_AcroJS.html#event-properties

 

You'll also find lots of using form scripting info here:

https://www.pdfscripting.com/public/PDF-Form-Scripting.cfm

   

And this one explains some of the event processing:

https://www.pdfscripting.com/public/Formatting-Form-Fields.cfm

 

 

 

 

Thom Parker - Software Developer at PDFScriptingUse the Acrobat JavaScript Reference early and often
Inspiring
September 25, 2023

I have attempted to place the script in the custom validation script but it would not execute unless I tab through all fields and change the value to "0".-- it will then add number of templates of the previous numerical entry. Once I tab through again and change the value to a "valid positive number" I receive the app alert telling me to "Please enter a valdue positive number in the Equipment Number field".

And thank you for the links... Just loaded the pages and I'll be reading through your suggested reference material immediately..

Example attached.

Thom Parker
Community Expert
Community Expert
September 25, 2023

Before adding all the code, the first thing you need to do is to determine the exact criteria that determine when the pages are spawned.

 

The custom validation script is triggered when the field value changes.  The reason the previous field value is used is because your script acquires the current field value when the field is in transition and the value has not yet changed. The newly entered value is in the event object. 

 

 

Thom Parker - Software Developer at PDFScriptingUse the Acrobat JavaScript Reference early and often