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

setAction on multiple fields

Community Expert ,
Sep 21, 2019 Sep 21, 2019

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;
TOPICS
Acrobat SDK and JavaScript
2.5K
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

correct answers 1 Correct answer

LEGEND , Sep 22, 2019 Sep 22, 2019

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

...
Translate
LEGEND ,
Sep 21, 2019 Sep 21, 2019

 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.

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 ,
Sep 21, 2019 Sep 21, 2019
I know but, it should be working since it's working for the others script area.
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
LEGEND ,
Sep 21, 2019 Sep 21, 2019

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. 

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 ,
Sep 21, 2019 Sep 21, 2019

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.

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
LEGEND ,
Sep 21, 2019 Sep 21, 2019

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 MouseUpMouseDownMouseEnterMouseExitOnFocus, 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.

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 ,
Sep 21, 2019 Sep 21, 2019

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.

 

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
LEGEND ,
Sep 21, 2019 Sep 21, 2019
You can create an array of the individual field names using the getArray() method.
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 ,
Sep 21, 2019 Sep 21, 2019
Will have to do that even if it does not make sense much. At least for me. 🙂
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 ,
Sep 21, 2019 Sep 21, 2019

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


Acrobate du PDF, InDesigner et Photoshoptographe
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 ,
Sep 21, 2019 Sep 21, 2019

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!

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 ,
Sep 21, 2019 Sep 21, 2019

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.

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
LEGEND ,
Sep 22, 2019 Sep 22, 2019
LATEST

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

 

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