These forums are now Read Only. If you have an Acrobat question, ask questions and get help from one of our experts.

What did I do wrong? First time attempt using variables and arrays

Michael Frommer
Registered: Apr 3 2009
Posts: 80

This is my first attempt at writting a doc level script that includes variables and arrays. I obviously did something wrong since it doesnt work; but I got no syntax errors when I added it to my doc.

(Assumption made: I can use arbitrary names for the variables; hoping they do not conflict with any hard coded ones.) Is there a source where I can look for the hard coded (allowed) variables?

Would someone please assist me to get it to work.
Goal of the script: When a specific checkbox is selected, check for the value of a specific radio/checkbox, each with respective widgets (#1 vs. #1, #2 vs. #2, etc). If criteria of argument is met, run app.alert.

function test()
{
// List all radio style checkboxes.
var regtype = new Array(10);
  regtype[0] = this.getField("Reg.Type.1");
  regtype[1] = this.getField("Reg.Type.2");
  regtype[2] = this.getField("Reg.Type.3");
  regtype[3] = this.getField("Reg.Type.4");
  regtype[4] = this.getField("Reg.Type.5");
  regtype[5] = this.getField("Reg.Type.6");
  regtype[6] = this.getField("Reg.Type.7");
  regtype[7] = this.getField("Reg.Type.8");
  regtype[8] = this.getField("Reg.Type.9");
  regtype[9] = this.getField("Reg.Type.10");
 
// List all checkboxes which will call this script if and when checked.
var chkbx = new Array(10);
  chkbx[0] = this.getField("Reg.Event.Workshop.1");
  chkbx[1] = this.getField("Reg.Event.Workshop.2");
  chkbx[2] = this.getField("Reg.Event.Workshop.3");
  chkbx[3] = this.getField("Reg.Event.Workshop.4");
  chkbx[4] = this.getField("Reg.Event.Workshop.5");
  chkbx[5] = this.getField("Reg.Event.Workshop.6");
  chkbx[6] = this.getField("Reg.Event.Workshop.7");
  chkbx[7] = this.getField("Reg.Event.Workshop.8");
  chkbx[8] = this.getField("Reg.Event.Workshop.9");
  chkbx[9] = this.getField("Reg.Event.Workshop.10");
 
// Respective fields used in 'if' argument (examples).
// Reg.Event.Workshop.1 verses Reg.Type.1.; Reg.Event.Workshop.2 verses Reg.Type.2; etc.
 
var eventTarget = this.getField(chkbx[i]);
var oCk = {bAfterValue:false};
fld = this.getField(regtype[i]);
if ((eventTarget.isBoxChecked) && (eventTarget.value == "yes") && (fld.value == "member") && !this.hideWarning1)
{
app.alert ({cMsg: "[i]my message[/i]", nIcon:3, cTitle:"REGISTRATION ALERT", oCheckbox:oCk}); hideWarning1 = oCk.bAfterValue;
}
}

I know the app.alert works. The problem is with all of the code above the app.alert.

My Product Information:
Acrobat Pro 8.0, Windows
try67
Expert
Registered: Oct 30 2008
Posts: 2398
So what's the problem? Do you get an error message? It doesn't work, or what?

- AcrobatUsers Community Expert - Contact me personally at try6767 [at] gmail [dot] com
Check out my custom-made scripts website: http://try67.blogspot.com

Michael Frommer
Registered: Apr 3 2009
Posts: 80
It just doesn't work; that is, the alert message never pops up, even when I know it should.
try67
Expert
Registered: Oct 30 2008
Posts: 2398
Well, you have four components that all need to be true in order for it to display. Start debugging them one by one and see which one(s) is false, then go backwards and try to find out why.

BTW - I don't see anywhere where the variable "i" is defined.

- AcrobatUsers Community Expert - Contact me personally at try6767 [at] gmail [dot] com
Check out my custom-made scripts website: http://try67.blogspot.com

Michael Frommer
Registered: Apr 3 2009
Posts: 80
try67 wrote:
BTW - I don't see anywhere where the variable "i" is defined.
I don't think I should have used that. I still dont fully understand how the code knows to use each line item of the arrays. Do i need any other variable, or modification of syntax to so that it looks at each respective item in each of the two arrays?

var eventTarget = this.getField(chkbx[i]);fld = this.getField(regtype[i]);
try67
Expert
Registered: Oct 30 2008
Posts: 2398
The most common way of accessing each item of an array is with a loop, for example:
for (i=0; i

- AcrobatUsers Community Expert - Contact me personally at try6767 [at] gmail [dot] com
Check out my custom-made scripts website: http://try67.blogspot.com

Michael Frommer
Registered: Apr 3 2009
Posts: 80
try67,

Thanks for the suggestion and explanation. But since "regtype" is a radio/checkbox (with export value = "member", if 'checked, does it matter that you suggest I use:

for (i=0; i
try67
Expert
Registered: Oct 30 2008
Posts: 2398
You defined an array called "regtype" at the top of your function. As such it has a property "length" which holds the number of items in the array (10, in this case). It's true that the same property can also be used for strings to return their length, but this is not related*.
I suggest you try to learn a bit more about arrays and the way to work with them. Open the link I gave before and read through it. There's a page dedicated to arrays there as well.


* Actually it is related since a String is basically an array of chars, but never mind.

- AcrobatUsers Community Expert - Contact me personally at try6767 [at] gmail [dot] com
Check out my custom-made scripts website: http://try67.blogspot.com

Michael Frommer
Registered: Apr 3 2009
Posts: 80
Sorry for being such a noob. i am trying to learn. Before I post, I know to search first. It was the result of my searches that helped me write my code, but I was still guessing a little. I'll check out that link over the weekend.

By the way, I did put that into my script but it still isnt executing. I'll try to tear it apart again, but I do feel lost at this point.
try67
Expert
Registered: Oct 30 2008
Posts: 2398
No need to apologize, everyone is a "noob" at some point. The first thing to know, though, is that writing a script (or any other computer program) can't be done by simply copying and pasting bits of code from different sources. If even one thing is off, it's very likely the script will not work at all or will give bad results.

- AcrobatUsers Community Expert - Contact me personally at try6767 [at] gmail [dot] com
Check out my custom-made scripts website: http://try67.blogspot.com

thomp
Expert
Registered: Feb 15 2006
Posts: 4411
Refering back to the code in the original post. You define two arrays and populate them with a series of field objects. Later in the code you use an element of one of these arrays to get two field objects:

var eventTarget = this.getField(chkbx[i]);
fld = this.getField(regtype[i]);

This code is unnecessary since "chkbx[i]" and "regtype'i] are already field objects. The code should be

var eventTarget = chkbx[i];
fld = regtype[i];

Or why not just use "chkbx[i]" and "regtype[i]" directly.

Also as Try67 pointed out, "i" is not defined, so the code won't work anyway until it is.

Also, in the "if" statement:

if ((eventTarget.isBoxChecked) && (eventTarget.value == "yes") && (fld.value == "member") && !this.hideWarning1)"isBoxChecked" is a function so it has to be called with the argument deliimiters "()". In fact, this funntion requires the instance index of the check box as input. The second comparison (eventTarget.value == "yes") is redundant. Remove it. And if you are using "!this.hideWarning1" to block execution of the warning message, it should be the first test in the "if" statement.

The one very important piece of information that you haven't answered is: are the check boxes "Reg.Type" and "Reg.Event.Workshop" multiple instances of the same check boxes, i.e. you have 10 check boxes named "Reg.Type"? or did you actually all the check boxes ""Reg.Type.1" thru "Reg.Type.10"?

Thom Parker
The source for PDF Scripting Info
[url=http://www.pdfScripting.com]pdfscripting.com[/url]

The Acrobat JavaScript Reference, Use it Early and Often
[url=http://www.adobe.com/devnet/acrobat/javascript.php]http://www.adobe.com/devnet/acrobat/javascript.php[/url]

Thom Parker
The source for PDF Scripting Info
www.pdfscripting.com
Very Important - How to Debug Your Script

Michael Frommer
Registered: Apr 3 2009
Posts: 80
thomp wrote:
Refering back to the code in the original post. You define two arrays and populate them with a series of field objects. Later in the code you use an element of one of these arrays to get two field objects:var eventTarget = this.getField(chkbx[i]);
fld = this.getField(regtype[i]);

This code is unnecessary since "chkbx[i]" and "regtype'i] are already field objects. The code should be

var eventTarget = chkbx[i];
fld = regtype[i];

Or why not just use "chkbx[i]" and "regtype[i]" directly.
Okay. Got rid of those two lines and modified the 'if' statement.

thomp wrote:
Also as Try67 pointed out, "i" is not defined, so the code won't work anyway until it is.
I've added the line Try67 gave me, but I'm not sure if I placed it correctly within the script.

thomp wrote:
Also, in the "if" statement:if ((eventTarget.isBoxChecked) && (eventTarget.value == "yes") && (fld.value == "member") && !this.hideWarning1)"isBoxChecked" is a function so it has to be called with the argument deliimiters "()".
I thought I had. The only other time I've used it in another script, which was called from within the checkbox being referenced, I wrote it as:

(event.target.isBoxChecked)

But this time I am calling the function from a different checkbox ("Reg.Event.Workshop") to find out if another checkbox "Reg.Type" is checked.

[b]I'm not sure how to write it then. Please show me.[/b]

thomp wrote:
The second comparison (eventTarget.value == "yes") is redundant. Remove it.
You mentioned this to me in [url=http://www.acrobatusers.com/forums/aucbb/viewtopic.php?id=19617]another thread[/url], but I found it actually necessary in order to prevent the alert message from executing a 2nd time when and if the checkbox is unchecked.

thomp wrote:
And if you are using "!this.hideWarning1" to block execution of the warning message, it should be the first test in the "if" statement.
It's placement in the 'if' statement didn't seem to matter in my [url=http://www.acrobatusers.com/forums/aucbb/viewtopic.php?id=19617]other script[/url].

thomp wrote:
The one very important piece of information that you haven't answered is: are the check boxes "Reg.Type" and "Reg.Event.Workshop" multiple instances of the same check boxes, i.e. you have 10 check boxes named "Reg.Type"? or did you actually all the check boxes ""Reg.Type.1" thru "Reg.Type.10"?
I thought I made that clear in my OP and that you'd be able to tell by my arrays.

There are 10 checkboxes with parent/child naming (is that the right phrase?) whose widget (did I use that term corectly?) is the 'number':

* Reg.Event.Workshop.1
* Reg.Event.Workshop.2
* Reg.Event.Workshop.3
* etc.

There are 20 radio/checkboxes with parent/child naming whose widget is the 'number', in 10 groups of 2 each. Every 2 radio/checkboxes have the same child (widget) number with different export values (member or non-member):

* Reg.Type.1
* Reg.Type.2
* Reg.Type.3
* etc.

Every row of my form also has a respective field ("Reg.Name.1"; "Reg.Name.2"; "Reg.Name.3"; etc.) for a registrant's name. The user has to identify (by selecting the checkboxes of each applicale row) if the registrant is a member or non-member (of my business association) and which event they want to attend. (There is a third group of checkboxes for the other event but that is irrelevant to this script.)

[b][u]What I want my script to do[/b][/u]
Calling the doc level script [u]from each[/u] Reg.Event.Workshop checkbox [u]so that it looks at [b]that row's coresponding[/b][/u] Reg.Type radio/checkbox; Generate an alert message:

* If Reg.Event.Workshop.1 is checked & Reg.Event.Workshop.1 has export value of "yes" & Reg.Type.1 has export value of "member" & the alert message hasn't been checked off to hide it.
* If Reg.Event.Workshop.2 is checked & Reg.Event.Workshop.2 has export value of "yes" & Reg.Type.2 has export value of "member" & the alert message hasn't been checked off to hide it.
* If Reg.Event.Workshop.3 is checked & Reg.Event.Workshop.3 has export value of "yes" & Reg.Type.3 has export value of "member" & the alert message hasn't been checked off to hide it.
* Etc.

Okay. Here is my current code:
function test(){var regtype = new Array(10);regtype[0] = this.getField("Reg.Type.1");regtype[1] = this.getField("Reg.Type.2");regtype[2] =  this.getField("Reg.Type.3");regtype[3] = this.getField("Reg.Type.4");regtype[4] = this.getField("Reg.Type.5");regtype[5] = this.getField("Reg.Type.6");regtype[6] = this.getField("Reg.Type.7");regtype[7] = this.getField("Reg.Type.8");regtype[8] = this.getField("Reg.Type.9");regtype[9] = this.getField("Reg.Type.10"); var chkbx = new Array(10);chkbx[0] = this.getField("Reg.Event.Workshop.1");chkbx[1] = this.getField("Reg.Event.Workshop.2");chkbx[2] = this.getField("Reg.Event.Workshop.3");chkbx[3] = this.getField("Reg.Event.Workshop.4");chkbx[4] = this.getField("Reg.Event.Workshop.5");chkbx[5] = this.getField("Reg.Event.Workshop.6");chkbx[6] = this.getField("Reg.Event.Workshop.7");chkbx[7] = this.getField("Reg.Event.Workshop.8");chkbx[8] = this.getField("Reg.Event.Workshop.9");chkbx[9] = this.getField("Reg.Event.Workshop.10"); var oCk = {bAfterValue:false};for (i=0; i<chkbx.length; i++){if ((chkbx[1].event.isBoxChecked) && (chkbx[i].value == "yes") && (regtype[i].value == "member") && !this.hideWarning1){app.alert ({cMsg: "my message.", nIcon:3, cTitle:"REGISTRATION ALERT", oCheckbox:oCk}); hideWarning1 = oCk.bAfterValue;}}}
thomp
Expert
Registered: Feb 15 2006
Posts: 4411
Ok, so let me see if I have this straight. You have form with a number of Rows. In each row there are some fields, "Reg.Event.Workshop.1", "Reg.Name.1", "Reg.Type.1", etc. Where the ".1" in these names represents the Row Number. Is this correct?

On each line there are at least two check boxes named "Reg.Type.#", where the export value of one of the check boxes is "member". Is this correct? Or is the is the member check box the only one on the line with the "Reg.Type.#" name?

You stated that you want this function to work on a per row basis. In order to do that the code in the function has to be aware of the current Row. The way this code is constructed it checks all rows at the same time. This is not necessary. You only need to check a specific row. There are two ways to do this. 1) pass the row number into the function from where it's called in the check box, or 2) deconstruct the name of the checkbox, where the function is called from, to find the Row number. The arrays the code builds should be placed outside the function since they only need to be defined once, when the form is first opened.

You also stated that you are new to both Acrobat and programming. This task you are trying to perform is not a simple one. It is not suitable for a noob. As you've been finding out you need much more experience and knowledge to perform this task effectively. So you have two choices, learn more or hire a developer. You can find lots of video tutorials, articles, sample scripts, and files at www.pdfscripting.com. I would suggest you watch the free videos. In particular the ones on Form scripting basics, which covers the very concept you are struggling with here.

Thom Parker
The source for PDF Scripting Info
[url=http://www.pdfScripting.com]pdfscripting.com[/url]

The Acrobat JavaScript Reference, Use it Early and Often
[url=http://www.adobe.com/devnet/acrobat/javascript.php]http://www.adobe.com/devnet/acrobat/javascript.php[/url]

Thom Parker
The source for PDF Scripting Info
www.pdfscripting.com
Very Important - How to Debug Your Script