Trouble understanding looping through an array

Dec 17, 2020

Copy link to clipboard

Copied

I'm trying to understand how to loop through an arrays. I have tried a few variations with no luck, I think it may be specific to extendscripts version of javascript but I'm not totally sure. Anyhow, I have a twenty page document and I want to put a red rectangle on each page in the array and both these attempts do not work. How do I do this?

var myDoc = app.activeDocument;
var myArr = [1,5,7,14];

//Attempt 1
for (name of myArr){
    myDoc.pages[name].rectangles.add().properties = {
    geometricBounds:[0,0,200,200], 
    fillColor: "Red"}; */
}

//Attempt 2
for ( i=0; i < myArr.length; i++){
    myDoc.pages[myArr[i]].rectangles.add().properties = {
    geometricBounds:[0,0,200,200], 
    fillColor: "Red"};
}

Attempt 2 is closer to correct. I would eschew 'properties' in general; you can see that rectangles.add() returns a rectangle object. So, you can set a variable to your returned rectangle object and set individual properties after you have that object reference. 

var myDoc = app.activeDocument;
var myArr = [1,5,7,14];
var rect;

//Attempt 2
for ( i=0; i < myArr.length; i++){
    rect = myDoc.pages[myArr[i]].rectangles.add();
    rect.geometricBounds =[0,0,200,200];
    rect.fillColor = "Red";
}
TOPICS
Scripting

Views

126

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more

Trouble understanding looping through an array

Dec 17, 2020

Copy link to clipboard

Copied

I'm trying to understand how to loop through an arrays. I have tried a few variations with no luck, I think it may be specific to extendscripts version of javascript but I'm not totally sure. Anyhow, I have a twenty page document and I want to put a red rectangle on each page in the array and both these attempts do not work. How do I do this?

var myDoc = app.activeDocument;
var myArr = [1,5,7,14];

//Attempt 1
for (name of myArr){
    myDoc.pages[name].rectangles.add().properties = {
    geometricBounds:[0,0,200,200], 
    fillColor: "Red"}; */
}

//Attempt 2
for ( i=0; i < myArr.length; i++){
    myDoc.pages[myArr[i]].rectangles.add().properties = {
    geometricBounds:[0,0,200,200], 
    fillColor: "Red"};
}

Attempt 2 is closer to correct. I would eschew 'properties' in general; you can see that rectangles.add() returns a rectangle object. So, you can set a variable to your returned rectangle object and set individual properties after you have that object reference. 

var myDoc = app.activeDocument;
var myArr = [1,5,7,14];
var rect;

//Attempt 2
for ( i=0; i < myArr.length; i++){
    rect = myDoc.pages[myArr[i]].rectangles.add();
    rect.geometricBounds =[0,0,200,200];
    rect.fillColor = "Red";
}
TOPICS
Scripting

Views

127

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Dec 17, 2020 0
Dec 17, 2020

Copy link to clipboard

Copied

Attempt 2 is closer to correct. I would eschew 'properties' in general; you can see that rectangles.add() returns a rectangle object. So, you can set a variable to your returned rectangle object and set individual properties after you have that object reference. 

var myDoc = app.activeDocument;
var myArr = [1,5,7,14];
var rect;

//Attempt 2
for ( i=0; i < myArr.length; i++){
    rect = myDoc.pages[myArr[i]].rectangles.add();
    rect.geometricBounds =[0,0,200,200];
    rect.fillColor = "Red";
}

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Dec 17, 2020 0
Dec 17, 2020

Copy link to clipboard

Copied

So it was the properties that was messing it up? This works thank you, one question, the boxes are off as the array starts with 0, is there anyway to use the actual numbers in the array? Something like [myArr[i-1] or something?

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Dec 17, 2020 0
Dec 17, 2020

Copy link to clipboard

Copied

It strange I'm getting a box on page 2, 6, 8, 14. That makes no sense.

 

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Dec 17, 2020 0
Dec 17, 2020

Copy link to clipboard

Copied

Hi David,

2, 6, 8 make perfect sense.

I'd expect 15 and not 14 as last value for the page position in your document.

Counting in ExtendScript starts with 0.

So: pages[1] is the second page in your document.

 

Regards,
Uwe Laubender

( ACP )

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Dec 17, 2020 0
Dec 17, 2020

Copy link to clipboard

Copied

Ok I see now, the reason was because I had the pages as spreads and whenever I used a even number  it would allways place it at [0,0,200,200]. But I still don't understand how you adjust the array to use the actual numbers. It seems like it would be i-1 but I can't get that to work. 

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Dec 17, 2020 0
Dec 17, 2020

Copy link to clipboard

Copied

It would be myArr[i]-1. The way you have it would lead to an out of bounds error when i = 0

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Dec 17, 2020 1
Dec 17, 2020

Copy link to clipboard

Copied

hi, you should check ViewPreference, because "ruler per spread" and "ruler per spread" are different things

 

function test(){
    var rules_align = Number(app.activeDocument.viewPreferences.rulerOrigin)
    app.activeDocument.viewPreferences.rulerOrigin = 1380143215
    var myDoc = app.activeDocument;
    var myArr = [1,5,7,14];
    var rect;
    //Attempt 2
    for ( var i=0; i < myArr.length; i++){
        rect = myDoc.pages[myArr[i]-1].rectangles.add();
        rect.properties = {
            geometricBounds:[0,0,200,200], 
            fillColor: "Red"};
    }
    app.activeDocument.viewPreferences.rulerOrigin = rules_align
}

app.doScript('test()', ScriptLanguage.JAVASCRIPT, undefined, UndoModes.ENTIRE_SCRIPT, 'test')

 

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Dec 17, 2020 1
Dec 17, 2020

Copy link to clipboard

Copied

"ruler per spread" and "ruler per page" of course ))

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Dec 17, 2020 0
Dec 17, 2020

Copy link to clipboard

Copied

The rectangle's properties can be set as a parameter of the add() function:

myDoc.pages[myArr[i]-1].rectangles.add({
   geometricBounds:[0,0,200,200], 
   fillColor: "Red",
});

 which is more efficient.

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Dec 17, 2020 2