Skip to main content
Inspiring
July 3, 2023
Question

script writing help, multiple text boxes and if/than parameter

  • July 3, 2023
  • 3 replies
  • 806 views

the below script breaks every time at the this.getfield command. Rank and Abbreviated rank are list boxes. I am trying to run this InDesign and willing to push it to acrobat but dont know if that would work either.

 

// Get references to the required fields
var rankField = this.getField("Rank");
var firstField = this.getField("First");
var lastField = this.getField("Last");
var dutyTitleField = this.getField("Duty Title");
var unitField = this.getField("Unit");
var inputField = this.getField("input");
var decField = this.getField("dec");
var abbreviatedRankField = this.getField("Abbreviated Rank");

// Check the values of the checkboxes
var longCheckbox = this.getField("long").value;
var shortCheckbox = this.getField("short").value;

// Get the values from the input fields
var rank = rankField.valueAsString;
var first = firstField.value;
var last = lastField.value;
var dutyTitle = dutyTitleField.value;
var unit = unitField.value;
var input = inputField.value;
var abbreviatedRank = abbreviatedRankField.valueAsString;

// Build the output based on the checkbox values
var output = "";
if (longCheckbox === "Yes") {
output = rank + " " + first + " " + last + " distinguished " + (firstField.value === "female" ? "herself" : "himself") + " by meritorious service as " + dutyTitle + ", " + unit + ".";
} else if (shortCheckbox === "Yes") {
output = rank + " " + first + " " + last + " distinguished " + (firstField.value === "female" ? "herself" : "himself") + " by meritorious service while assigned to " + unit + ".";
}

// Set the value of the 'dec' field
decField.value = output;

// Build and set the closing phrase
var closingPhrase = "The distinctive accomplishments of " + abbreviatedRank + " " + last + " reflects credit upon " + (firstField.value === "female" ? "herself" : "himself") + " and the United States Air Force.";
this.getField("closingPhrase").value = closingPhrase;

This topic has been closed for replies.

3 replies

Legend
July 4, 2023

You can't use the API from one app in a different app. Each one does different things, in different ways. This is for the Acrobat API only. But you can't justify text in a fillable PDF field, not by any trick; whether you made it in InDesign doesn't give more power to the PDF fields. However, it's normal to design the form in InDesign then export to PDF.

Inspiring
July 4, 2023

Thank you! sounds like I have to leave instructions for people to select the text frame, ctrl+E and select justified to get the result they need to view.. not what i wanted but seems to be the only option I have with limited experience and wanting this to be able to be a mass distrobution.

Legend
July 4, 2023

If you attempt a mass distribution you may have many other issues. Can you FORCE people to use suitable software - that is, Acrobat Reader on Mac or Windows? When they already have an app that can read PDF? And will you tell people on iOS and Android that they are out of luck? I think you're reaching beyond what is feasible. If you want to give people a precisely formatted PDF, you need to generate them on a server and deliver them - a very different kind of programming.

rob day
Community Expert
Community Expert
July 4, 2023

Hi @Daniel308633812s5g , Your code looks like it’s using the Acrobat API—there is no getField() method in the InDesign API.

rob day
Community Expert
Community Expert
July 4, 2023

An InDesign document has a formFields property, which would be a collection of all formFields in a document.

This would get the active document’s form fields:

 

var ff = app.activeDocument.formFields.everyItem().getElements();

for (var i = 0; i < ff.length; i++){
    $.writeln(ff[i].name)
    //returns the name of every form field in the active document
}; 

 

 

https://www.indesignjs.de/extendscriptAPI/indesign-latest/#FormField.html#d1e127934

Inspiring
July 4, 2023

Thank you!

Community Expert
July 4, 2023

Where is the definition of the getField method you are using? What is the value of the this object? This seems to be an incomplete set of code. Please share the complete one, only then will we be able to troubleshoot it.

-Manan

-Manan
Inspiring
July 4, 2023

All references are inside the current document. all values are text fields besides 2 check boxes. The current code that works in acrobat is attached. I needed the added functionality of being able to have justified alignment set in the space so I wanted to use InDesign.