add variable to array in a loop

Lukasz picture Lukasz · Oct 3, 2011 · Viewed 43.8k times · Source

using extend script to push a variable into an array it's basically javascript. any idea what I am doing wrong?

if ( app.documents.length > 0 ) {

    for ( i = 0; i< app.activeDocument.textFrames.length; i++) {
         var allSizes = []; //set up empty array

        textArtRange = app.activeDocument.textFrames[i].textRange;
        var fontName =  textFonts.getByName("Nobile");
        alert (fontName);
        textArtRange.characterAttributes.textFont = fontName;
        var fontSizes = textArtRange.characterAttributes.size;

        allSizes.push(fontSizes)
        alert (fontSizes);

    }
        alert (allSizes);
}

the alerts for allSizes only return single values, not the array.

Answer

Rob W picture Rob W · Oct 3, 2011

Move the definition of allSizes = [] outside the loop.

Currently, you're "resetting" the value of allSizes at each loop.