Skip to main content
Participant
April 23, 2024
Question

why does this code works with Nitro Pro but not with Adobe?

  • April 23, 2024
  • 2 replies
  • 609 views
function getFileName() {
var sequenceNumber = this.getField("Form1.0").value;
var fileName = global.documentNum + "_" + sequenceNumber + ".pdf";
 
return fileName;
}
 
function getFilePath() {
var userName = "GAC_Line";
var path = "/c/users/" + userName + "/desktop/safe work permits/" + getFileName();
 
return path;
}
 
function validate() {
var fieldNames = [];
var displayFieldNames = [];
var regexEmpty = /^\s*$/;
const fieldCount = 24; // increment to add more fields
 
for(var i = 0; i < fieldCount; i++) {
var field = this.getField("Form1." + i);
 
if(regexEmpty.test(this.getField(field.name).value) || field.valueAsString == field.defaultValue) {
fieldNames.push(field.name);
displayFieldNames.push(field.userName);
}
}
 
if(fieldNames.length > 0) {
var field_sp = fieldNames.length == 1 ? "field" : "fields";
app.alert("Please complete the following " + field_sp + ":\n\n" + displayFieldNames.join("\n"));
getField(displayFieldNames[0]).setFocus();
} else {
this.saveAs(getFilePath());
app.alert("Form " + getFileName() + " has been saved to the Safe Work Permits folder.  The print dialog window will now open.", 3);
this.print();
}
}
 
validate();
This topic has been closed for replies.

2 replies

try67
Community Expert
Community Expert
April 23, 2024

The saveAs method requires a privileged context, such as a trusted function in a folder-level script, in Acrobat. Maybe Nitro is not so strict...

KtradAuthor
Participant
April 24, 2024

Thank you for your replay. Are you able to help me adding a privileged context?

try67
Community Expert
Community Expert
April 24, 2024

See these tutorials by Thom Parker:

https://acrobatusers.com/tutorials/trust-and-privilege-in-acrobat-scripts

https://acrobatusers.com/tutorials/using_trusted_functions

Before you start, though, are you able to install a script file on the local computer of each user? If not, then don't bother, as you won't be able to do it.

Thom Parker
Community Expert
Community Expert
April 23, 2024

Is this your code?

How does it not work?

What debug have you done?

Where is this code executed from? 

What, if any, errors are reported in the console window? 

 

The global.documentNum  property is not defined anywhere, perhaps this is the issue?

 

 

Thom Parker - Software Developer at PDFScriptingUse the Acrobat JavaScript Reference early and often
KtradAuthor
Participant
April 24, 2024

It is my code, I'm trying to create one button that will execute the followigs in order:

1- validated required fields, if any field left empty a notification will appear to show which field is empty; code will stop

2- If all required fields are filled up, then it will save as using a format: date plus 3 digit number.

3- after savinf as, it will promt to print

Done.