Skip to main content
Participant
March 22, 2025
Answered

Code won't persist after Save

  • March 22, 2025
  • 2 replies
  • 519 views

Scenario: I'm building a form to track production over the week.  A series of text fields for different widgets, with odd numbers (Widget A = Text1, WidgetB = Text3, Widget C = Text5).  I have a series of buttons to increment the text field up and down, numbered sequentially (Button1, Button2, Button3, Button4, Button5, Button6).  In the end i'm going to have over 125 text fields, and 250 buttons.  I wrote a for loop that with the click of the "goButton" will write the script to all of those buttons for me.  Right now I only have 3 text fields and 6 buttons until proof of concept, before I make this gigantic pdf.

 

Problem: The button and the script work perfectly as intended, however they do not persist after a save and a close.  I would have to click the "goButton" every time I open it to insert the script into the buttons again.  My first instinct is that the individual buttons only have a function in their javascript and not the actual script.  It works, but it doesn't work right... Any help is appreciated

 

"goButton" Mouse Up event looks like this:

//increment text field up
function foo(i) {
   var x = this.getField("Text" + i).value;
   x += 1;
   this.getField("Text" + i).value = x;
}

//increment text field down, don't go below zero
function bar(i) {
   var x = this.getField("Text" + i).value;
   if(x>0) {
      x-= 1;
   }
   this.getField("Text" + i).value = x;
}

var myButton1="";
var myButton2="";

for(i=1;i<7;i+=2){
   //odd number buttons
   myButton1 = this.getField("Button" + i);
   myButton1.setAction("MouseUp","foo("+i+")");
   myButton1.setCaption("+");
   //even number buttons
   var j = i+1;
   myButton2 = this.getField("Button" + j);
   myButton2.setAction("MouseUp","bar("+i+")");
   myButton2.setCaption("-");
}
Correct answer chrisk93124384

Thank you all so much for your help.  It was a combination of your replies that got me where I needed to be. The correct answer was to drop the functions and put literal text in the setAction.  

for (i=1;i<251;i+=2){  this.getField("Button"+i).setAction("MouseUp","this.getField(\"Text"+i+"\").value +=1");
Var j=i+1;
this.getField("Button"+j).setAction("MouseUp","this.getField(\"Text"+i+"\").value -= 1");
}

2 replies

chrisk93124384AuthorCorrect answer
Participant
March 30, 2025

Thank you all so much for your help.  It was a combination of your replies that got me where I needed to be. The correct answer was to drop the functions and put literal text in the setAction.  

for (i=1;i<251;i+=2){  this.getField("Button"+i).setAction("MouseUp","this.getField(\"Text"+i+"\").value +=1");
Var j=i+1;
this.getField("Button"+j).setAction("MouseUp","this.getField(\"Text"+i+"\").value -= 1");
}
PDF Automation Station
Community Expert
Community Expert
March 22, 2025

The function calls you are adding to the buttons:  bar() and foo() only work in the initial session because they are defined when you run the script.  When the document is reopened they are no longer defined, so an error is thrown.  The functions should be put in a document level script so they are defined every time the document is opened.  You also have a couple of errors in your script.  There is no such method as field.setCaption.  The method is field.buttonSetCaption().

Participant
March 22, 2025

Thank you! That answers the questions perfectly.  The only problem is adobe wants more $$$!!  Apparently the paid subscription I have now isn't good enough, I need a more expensive paid subscription.... awesome.

try67
Community Expert
Community Expert
March 22, 2025

To create doc-level scripts (via the application's UI) you need Acrobat Pro, not Acrobat Standard, yes.