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

Trouble understanding looping through an array

Engaged ,
Dec 17, 2020 Dec 17, 2020

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"};
}
TOPICS
Scripting
1.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

Community Expert , Dec 17, 2020 Dec 17, 2020

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";
}
Translate
Community Expert ,
Dec 17, 2020 Dec 17, 2020

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";
}
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
Engaged ,
Dec 17, 2020 Dec 17, 2020

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?

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
Engaged ,
Dec 17, 2020 Dec 17, 2020

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

 

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 ,
Dec 17, 2020 Dec 17, 2020

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 )

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
Engaged ,
Dec 17, 2020 Dec 17, 2020

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. 

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 ,
Dec 17, 2020 Dec 17, 2020

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

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
Participant ,
Dec 17, 2020 Dec 17, 2020

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')

 

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
Participant ,
Dec 17, 2020 Dec 17, 2020

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

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 ,
Dec 17, 2020 Dec 17, 2020
LATEST

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.

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