Copy link to clipboard
Copied
For my project, I have pages spawn from a template. I use a hidden form field containing a count variable which I append to the names of spawned fields and increment. There are fields such as topPh#, topCap#, etc. These numbers may or may not match with page numbers or be in sequential order.
I used regular expressions to do something similar:
var num = event.target.name.match(/\d/g).join('');
this.getField("botPh" + num).display = (event.target.value=="Off") ? display.hidden : display.visible;
this.getField("botCap" + num).display = (event.target.value=="Off") ? display.hidden : display.visible;
But in the above case, my target widget has a number on it (e.g. togBot5), and it talks to other widgets with the same number (e.g. botPh5), regardless what the number is.
Copy link to clipboard
Copied
Check the property page of the field.
Copy link to clipboard
Copied
I'm sorry, it seems my intent is unclear.
Each page of my document has a top photo and a bottom photo. I'm working on buttons/features for the document to help manipulate the order of the photos. If I have 3 pages, so 6 photos total, and I want to delete photo 3 (which is the top photo of page 2), I plan to fill an array using buttonGetIcon for all the photos past photo 3. Then I can use buttonSetIcon and replace photo 3 with photo 4, replace 4 with 5, etc. (I'm going to worry about what happens to the last one later. It's much less disruptive to the user than deleting in the middle of the document.)
A rub to the problem is the names of the photo button fields. They are all named "topPh" or "botPh" with a unique number at the end. The number may not match the page number or be in sequence with the last number. I need to make a loop that will fill an array with the icons from each photo, starting at photo 3 and going to the end. In the following code, I need to figure out how to make the for loop find the fields, regardless of their number.
//pop up a confirmation window; yes = 4, no = 3
var confirm = app.alert("Are you sure you want to delete this photo?\nThis cannot be undone.", 1, 2, "Confirm Photo Deletion");
if(confirm == 4){ //this only runs if they say "yes", otherwise nothing happens.
var pg = event.target.page;
var aPg = this.numPages - pg;
var icons = []
for(var i = 0, i < aPg, i++){
icons[i] = ???
} //end of for loop
} //end of if statement
The ??? is where I need to make it find the field "topPhX", for any value of X (as well as "botPhX"), then go the next page and find its fields, "topPhY" and "botPhY", where there is not necessarily a relationship between X and Y.
Copy link to clipboard
Copied
I am thinking I need to make a function that I can pass a page number, and it figures out the field names on that page. Is there a way to use getField on a certain page maybe?
Copy link to clipboard
Copied
Use getField and check the property page of the field.
Copy link to clipboard
Copied
How can I use getField wihtout knowing the number that the field name will end with?
Copy link to clipboard
Copied
Use a loop over all fields in the document.
Copy link to clipboard
Copied
I can kind of see where to go with this, but I need a little more direction. Sorry... I'm definitely a beginner.
I came across a somewhat similar discussion which suggested the following:
function getPageFields(doc, p) {
var fields = [];
for (var i=0; i<doc.numFields; i++) {
var f = doc.getField(doc.getNthFieldName(i));
if ((typeof f.page=="number" && f.page==p) || (typeof f.page!="number" && f.page.indexOf(p)>-1)) fields.push(f.name);
}
return fields;
}
I don't understand the if statement though. If I mimic this approach, and have an array "fields" as in the example, how could I then keep only the ones on certain pages or with certain names?
Will looping through every field make the page run more slowly as the number of pages and fields grows?
Copy link to clipboard
Copied
This function creates an array of all field names on page p.
When you have copies of fields with the same name the property page of the field gives an array.
Copy link to clipboard
Copied
I'm getting closer. I need to modify the following:
var fields = [];
for (var i = 0; i < this.numFields; i++){
var f = this.getField(this.getNthFieldName(i)).name;
if(f == "topPh2") fields.push(f);
} //end of for loop
app.alert(fields);
Instead of
if (f == "topPh2") fields.push(f);
I need something for any name beginning with "topPh".
Copy link to clipboard
Copied
Look like this works
var fields = [];
for (var i = 0; i < this.numFields; i++){
var f = this.getField(this.getNthFieldName(i)).name;
if(f.indexOf("topPh") > -1) fields.push(f);
} //end of for loop
app.alert(fields);
I was avoiding indexOf. Is there an alternative?
Copy link to clipboard
Copied
You can also use the search method.