Help Needed: Acrobat Folder Level Scripting Error
The laborious task of scripting multiple fields within an Acrobat form presents a formidable challenge. Driven by this very difficulty, I endeavored to craft a Folder Level script, a mechanism to imbue numerous fields with specialized code within their Properties tabs. Alas, my creation falters.
Consider, if you will, a scenario. From the dialog box's dropdown, I select the "Day" field, its true nature revealed in the sequence "Day1" through "Day31." Within this same dialog, I specify "1" as the Start Number and "31" as the End Number, thus defining the scope of my intended action. Furthermore, I choose "Format" from the Target Action menu. Finally, in the designated area for JavaScript Code, I input the specific script intended for the Format property of these selected fields. The culmination of these actions, however, yields not the desired result, but rather a disheartening error, etched in the console log.
I find myself in a state of hopeful anticipation, awaiting your invaluable assistance in resolving this perplexing matter.
App Alert:
Please select a Field Prefix.
Console Log:
Before execDialog
Dialog initialized
Commit function called
After execDialog, result: ok, dialog.results: {}
Complete Script Code:
// FormScriptUtility.js - Folder Level Script for Acrobat
app.addToolButton({
cName: "FormScriptUtility",
cLabel: "Populate Properties",
cTooltext: "Set custom scripts for multiple form fields",
cEnable: "event.rc = (app.doc != null);",
cExec: "launchFormScriptUtility();",
nPos: -1
});
function launchFormScriptUtility() {
var allFields = [];
for (var i = 0; i < this.numFields; i++) {
allFields.push(this.getNthFieldName(i));
}
var prefixMap = {};
allFields.forEach(function(field) {
var match = field.match(/^([A-Za-z]+\.?)(\d+)$/); // Nokta için \.?
if (match) {
var prefix = match[1]; // "ProjectCode." gibi
prefixMap[prefix] = true;
}
});
var formPrefixes = Object.keys(prefixMap);
var dialog = {
results: {}, // Dialog sonuçlarını saklamak için
initialize: function(dialog) {
console.println("Dialog initialized");
var fieldItems = {};
fieldItems[""] = ""; // Boş seçenek
formPrefixes.forEach(function(prefix) {
fieldItems[prefix] = prefix; // Metni doğrudan kullan
});
var actionItems = {
"": "",
"Validate": "Validate",
"Calculate": "Calculate",
"Keystroke": "Keystroke",
"Format": "Format",
"Mouse Up": "Mouse Up",
"Mouse Down": "Mouse Down",
"Focus": "Focus",
"Blur": "Blur"
};
dialog.load({
"fpre": fieldItems,
"snum": "",
"enum": "",
"actn": actionItems,
"scpt": ""
});
},
commit: function(dialog) {
console.println("Commit function called");
var results = dialog.store();
console.println("Store results: " + JSON.stringify(results));
// Field prefix
this.results.fieldPrefix = results["fpre"] || "";
// Sayılar
this.results.startNum = parseInt(results["snum"], 10) || 0;
this.results.endNum = parseInt(results["enum"], 10) || 0;
// Action
this.results.action = results["actn"] || "";
// Script
this.results.script = results["scpt"] || "";
console.println("dialog.results updated: " + JSON.stringify(this.results));
},
description: {
name: "Set Form Scripts",
elements: [{
type: "view",
width: 450,
height: 450,
elements: [{
type: "static_text",
name: "Step 1: Select Field Prefix from Form Fields",
width: 400,
height: 20,
alignment: "align_center"
}, {
type: "popup",
item_id: "fpre",
width: 200,
height: 20
}, {
type: "view",
align_children: "align_row",
elements: [{
type: "static_text",
name: "Start Number:",
width: 100,
height: 20
}, {
type: "edit_text",
item_id: "snum",
width: 50,
height: 20,
char_width: 4
}, {
type: "static_text",
name: "End Number:",
width: 100,
height: 20
}, {
type: "edit_text",
item_id: "enum",
width: 50,
height: 20,
char_width: 4
}]
}, {
type: "static_text",
name: "Step 2: Select Target Action:",
width: 400,
height: 20,
alignment: "align_center"
}, {
type: "popup",
item_id: "actn",
width: 200,
height: 20
}, {
type: "static_text",
name: "Step 3: Enter JavaScript Code",
width: 400,
height: 20,
alignment: "align_center"
}, {
type: "edit_text",
item_id: "scpt",
width: 400,
height: 200,
multiline: true
}, {
type: "cluster",
alignment: "align_right",
elements: [{
type: "ok_cancel",
ok_name: "OK",
cancel_name: "Cancel"
}]
}]
}]
}
};
console.println("Before execDialog");
var result = app.execDialog(dialog);
console.println("After execDialog, result: " + result + ", dialog.results: " + JSON.stringify(dialog.results));
if (result === "ok") {
processDialogResults(dialog.results);
} else {
console.println("Dialog cancelled");
}
}
function processDialogResults(results) {
if (!results || typeof results !== "object") {
app.alert("Error: No valid results from dialog.");
return;
}
var fieldPrefix = results.fieldPrefix ? results.fieldPrefix.trim() : "";
var fieldPrefixes = fieldPrefix ? [fieldPrefix] : [];
var startNum = results.startNum || 0;
var endNum = results.endNum || 0;
var action = results.action ? results.action.trim() : "";
var script = results.script || "";
var validActions = ["Validate", "Calculate", "Keystroke", "Format", "Mouse Up", "Mouse Down", "Focus", "Blur"];
var errorMessages = [];
if (!fieldPrefix) {
app.alert("Please select a Field Prefix.");
return;
}
if (isNaN(startNum) || startNum < 1) {
app.alert("Start Number must be a valid positive number.");
return;
}
if (isNaN(endNum) || endNum < startNum) {
app.alert("End Number must be a valid number greater than or equal to Start Number.");
return;
}
if (!action || !validActions.includes(action)) {
app.alert("Please select a valid Target Action: " + validActions.join(", "));
return;
}
if (!script) {
app.alert("JavaScript Code cannot be empty.");
return;
}
var totalSuccessCount = 0;
fieldPrefixes.forEach(function(prefix) {
for (var i = startNum; i <= endNum; i++) {
var fieldName = prefix + i; // "ProjectCode." + "1" -> "ProjectCode.1"
var field = this.getField(fieldName);
if (field) {
try {
field.setAction(action, script);
totalSuccessCount++;
} catch (e) {
errorMessages.push("Error setting script for " + fieldName + ": " + e);
}
} else {
errorMessages.push("Field not found: " + fieldName);
}
}
}, this);
if (errorMessages.length) {
app.alert("Some fields could not be updated:\n" + errorMessages.join("\n"));
} else {
app.alert("Scripts successfully applied to " + totalSuccessCount + " fields for prefix: " + fieldPrefix);
}
}
