Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

Code won't persist after Save

Community Beginner ,
Mar 22, 2025 Mar 22, 2025

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("-");
}
TOPICS
Create PDFs , Edit and convert PDFs , How to , JavaScript , PDF , PDF forms
142
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
1 ACCEPTED SOLUTION
Community Beginner ,
Mar 30, 2025 Mar 30, 2025
LATEST

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");
}

View solution in original post

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Mar 22, 2025 Mar 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().

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Mar 22, 2025 Mar 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.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Mar 22, 2025 Mar 22, 2025

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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Mar 22, 2025 Mar 22, 2025

You can add document level scripts with the Javascript console.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Mar 22, 2025 Mar 22, 2025

I tried to access the console with ctrl+j and it still prompts me to upgrade from "standard" to "pro"

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Mar 22, 2025 Mar 22, 2025

Standard, just like Reader, has a console but it can't be accessed by the UI.  Here's the workaround:

1)  Add a button with the following mouse up action:  console.show()

2)  Click the button to open the console.

3)  Copy, paste, and run the following script in the console:

this.addScript('MyScript','function foo(i){var x = this.getField("Text" + i).value;x += 1;this.getField("Text" + i).value = x;}function bar(i) {var x = this.getField("Text" + i).value;if(x>0) {x-= 1;}this.getField("Text" + i).value = x;}')

4) Remove the button.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Mar 30, 2025 Mar 30, 2025
LATEST

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");
}
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines