Copy link to clipboard
Copied
Hello,
Using setAction(cTrigger, cScript) I’m able to send javascript to a bunch of fields using hierarchale ("txt") naming like in (txt.0, txt.1, txt.2, etc.). This works fine for: Keystroke, Validate, Calculate, Format but will not do it for MouseUp/MouseDown/MouseEnter/MouseExit/OnFocus/OnBlur. For theses I need to specify the full name of the field.
This is my test javascript executed form the console. Is this a limitation? a Bug? or is there a workaround?
var cScript1 = "app.beep(0);"
var cScript2 = "app.beep(0);"
var cScript3 = "app.beep(0);"
var cScript4 = "app.beep(0);"
var cScript5 = "app.beep(0);"
var cScript6 = "app.beep(0);"
var cScript7 = "app.beep(0);"
var cScript8 = "app.beep(0);"
var cScript9 = "app.beep(0);"
var cScript10 = "app.beep(0);"
var f = this.getField("txt");
f.setAction("MouseUp", cScript1);
f.setAction("MouseDown", cScript2);
f.setAction("MouseEnter", cScript3);
f.setAction("MouseExit", cScript4);
f.setAction("OnFocus", cScript5);
f.setAction("OnBlur", cScript6);
f.setAction("Keystroke", cScript7);
f.setAction("Validate", cScript8);
f.setAction("Calculate", cScript9);
f.setAction("Format", cScript10);
f.fillColor = color.red;
Copy link to clipboard
Copied
I would add the script on a field by field basis, since not all the actions are shared in common. You can get the names of all the children in a hierachcial field by using the getArray method of the field object.
Copy link to clipboard
Copied
Copy link to clipboard
Copied
Not all fields with the same name share all the properties. For example, the button field does not share the various mouse actions. On can set any of the mouse actions for a different button with the same name.
Copy link to clipboard
Copied
I understand that. But in my test, all the fields were Text fields and share the same properties. It fail for the MouseUp/MouseDown/MouseEnter/MouseExit/OnFocus/OnBlur. Just did a quick test with only buttons and it fail also to add a MouseDown action for all. bt.1, bt.2, bt.3.
Copy link to clipboard
Copied
Per Acrobat JavaScript Reference Manual Field versus widget attributes for a listing of which action apply to the field and which are only available to the widget.
Note: The setAction method can apply at the field or widget level, depending on the event. The Keystroke, Validate, Calculate, and Format events apply to fields. The MouseUp, MouseDown, MouseEnter, MouseExit, OnFocus, and OnBlur events apply to widgets.
The checkThisBox, defaultIsChecked, isBoxChecked, and isDefaultChecked methods take a widget index, nWidget, as a parameter. If you invoke these methods on a Field object f that represents one specific widget, the nWidget parameter is optional (and is ignored if passed) and the method acts on the specific widget encapsulated by f.
Copy link to clipboard
Copied
Than how can it works for a single field, and not all hierarchical named one?
It's the same setAction(cTrigger, cScript) code. Or is there a way to traget the widget of an event specificaly? I don't think so.
Copy link to clipboard
Copied
Copy link to clipboard
Copied
Copy link to clipboard
Copied
Hi Jean-Claude
gkaiseril is right, action, format, etc cannot be applied to an array of fields ("txt" is an array since it have children/widgets).
So you should try this script which is intended for each widget (not tested) :
var cScript1 = "app.beep(0);"
var cScript2 = "app.beep(0);"
var cScript3 = "app.beep(0);"
var cScript4 = "app.beep(0);"
var cScript5 = "app.beep(0);"
var cScript6 = "app.beep(0);"
var cScript7 = "app.beep(0);"
var cScript8 = "app.beep(0);"
var cScript9 = "app.beep(0);"
var cScript10 = "app.beep(0);"
for (var i=1 ; i<11 ; i++) {
this.getField("txt."+i).setAction("MouseUp", cScript1);
this.getField("txt."+i).setAction("MouseDown", cScript2);
this.getField("txt."+i).setAction("MouseEnter", cScript3);
this.getField("txt."+i).setAction("MouseExit", cScript4);
this.getField("txt."+i).setAction("OnFocus", cScript5);
this.getField("txt."+i).setAction("OnBlur", cScript6);
this.getField("txt."+i).setAction("Keystroke", cScript7);
this.getField("txt."+i).setAction("Validate", cScript8);
this.getField("txt."+i).setAction("Calculate", cScript9);
this.getField("txt."+i).setAction("Format", cScript10);
this.getField("txt."+i).fillColor = color.red;
}
Copy link to clipboard
Copied
JR, the actions for the "txt" is working without having to do an array. Only the Actions was not.
You added line for (var i=1 ; i<11 ; i++) { make it works on all triggers. I only add to change it to (var i=0 ; i<99 ; i++). to get all of the fields starting txt.0 to txt.99
Thanks!
Copy link to clipboard
Copied
You code are working, but still give an error when executed from a button. The console give me this:
TypeError: this.getField(...) is null
12:AcroForm:Push:Annot1:MouseUp:Action1
Line 12 is where start the this.getField But all the code are pused into places.
Copy link to clipboard
Copied
When one gets a return of a null value when trying to access a field it indicates that field cannot be found. This could be because of a misspelling, the deletion of a field, not creating a field and expecting it to be present. Using the getArray method will provide an array of all the children field objects below the parent field. This list does not need the last level to be a number so one can use letters, does not include missing names in a series, and can provide a count of the total number of children fields. Using this apporach one does not need to update the script when more fields are added or deleted.
var oParentField = this.getField("txt");
if(oParentField == null) app.alert("Missing txt field", 1,0, "Field Error");
var aChildrenFields = oParentField.getArray();
console.show();
console.clear();
console.println("Number of children fields: " + aChildrenFields.length);
for(var i = 0; i < aChildrenFields.length; i++){
console.println(i + " child name: " +aChildrenFields[i].name);
}