Skip to main content
Obi-wan Kenobi
Legend
February 5, 2017
Question

"everyItem().getElements()"! … What use?

  • February 5, 2017
  • 2 replies
  • 2861 views

Hi,

In this code sample:

function main()    

    {

        var myImages = app.activeDocument.rectangles,  G = myImages.length,  // Not necessary!?  .everyItem().getElements()

        myValue = 0,  myIncrementation = -2;

        while (G--) {

            var myRotation = app.transformationMatrices.add({counterclockwiseRotationAngle: myValue});

            myImages.transform(CoordinateSpaces.pasteboardCoordinates, AnchorPoint.centerAnchor, myRotation);

            myValue += myIncrementation;

            }

    }

… use or not use "everyItem().getElements()" seems to not matter!

Does somebody explain this syntax behavior?

Thanks in advance! 

(^/)

This topic has been closed for replies.

2 replies

Kasyan Servetsky
Legend
February 5, 2017

Let's illustrate it with example: say, I want to get the list of all the fonts available in InDesign and write it to a text file on the desktop.

var scriptName = "Test";

Main();

function Main() {

    var arr = [],

    startTime = new Date();

   

    var fonts = app.fonts.everyItem().getElements();

   

    for (var i = 0; i < fonts.length; i++) {

        arr.push(fonts.name);

    }

   

    var text = arr.join(",");

    WriteToFile(text);

   

    var endTime = new Date();

    var duration = GetDuration(startTime, endTime);

    alert("Elapsed " + duration, scriptName, false);

}

function GetDuration(startTime, endTime) {

    var str;

    var duration = (endTime - startTime)/1000;

    duration = Math.round(duration);

    if (duration >= 60) {

        var minutes = Math.floor(duration/60);

        var seconds = duration - (minutes * 60);

        str = minutes + ((minutes != 1) ? " minutes, " :  " minute, ") + seconds + ((seconds != 1) ? " seconds" : " second");

        if (minutes >= 60) {

            var hours = Math.floor(minutes/60);

            minutes = minutes - (hours * 60);

            str = hours + ((hours != 1) ? " hours, " : " hour, ") + minutes + ((minutes != 1) ? " minutes, " :  " minute, ") + seconds + ((seconds != 1) ? " seconds" : " second");

        }

    }

    else {

        str = duration + ((duration != 1) ? " seconds" : " second");

    }

    return str;

}

function WriteToFile(text) {

    var file = new File("~/Desktop/" + scriptName + ".txt");

    file.encoding = "UTF-8";

    file.open("w");

    file.write(text);

    file.close();

}

function ErrorExit(error, icon) {

    alert(error, scriptName, icon);

    exit();

}

With .everyItem().getElements() -- using static array -- it takes less than a sec.

if I remove it (use live collection) it takes 9 secs.

— Kas

Obi-wan Kenobi
Legend
February 5, 2017

Hi Kas ( ),

Thanks for this instructive comment!

… So, now, I will systematically use "everyItem().getElements()".

(^/)

Kasyan Servetsky
Legend
February 5, 2017

Obi,

But it doesn't mean that you should always use "everyItem().getElements()". In some cases you have to deal with a live collection. For example, recently I wrote this script. It's not the final version, of course, but it will give you an idea. The script dynamically adds new rows (duplicates the product name) at the top of a new page so it should deal with dynamic collection which is updated after a row is added. If you add "everyItem().getElements()", the script will run a little bit faster (maybe), but it's functionality will be totally broken .

— Kas

Kasyan Servetsky
Legend
February 5, 2017

Hi Obi,

I asked the same question a few years ago:

What is the difference between the following two lines?

var myFrame = app.activeDocument.textFrames;

and

var myFrame = app.activeDocument.textFrames.everyItem().getElements();

In both cases the script works as expected. Why do you add .everyItem().getElements()?

Here's what Harbs answered me:

Because everyItem().getElements() builds a static Array of the items.

Without it, it's a dynamic collection.

When you have a collection, there has to be interaction with InDesign

every time you access a member of the collection and InDesign rebuilds

the collection each time it's accessed. That means a lot of extra

overhead.

Bottom line: using a static array is almost always faster than using

collections.

Regards,
Kas