Skip to main content
Known Participant
January 3, 2021
Answered

Objects names to a text block

  • January 3, 2021
  • 3 replies
  • 1417 views

I've got a few thousand object names I need added to a text block. Copy & pasting will take forever. Even exporting them to a text document would work. Is there anything out there to heklp with this?

This topic has been closed for replies.
Correct answer femkeblanco

Hi @rcraighead . Sorry, I just noticed that if iterating backwards, the loop condition should be i >= 0 or i > -1, i.e. the line should be

for (var i = aDoc.pageItems.length - 1; i > -1; i--) {

This is extraneous to the subject of this thread, but forward vs. backward iteration can be summarised like this: When removing items from, or moving items to, the beginning of a collection, forward iteration will cause problems because of indices changing. For example, when removing items, every other item will be skipped. So, say you want to remove all items from a collection (here the array and splice(i, 1) substitute for a collection and remove() in illustrator):

// forward iteration
var a = ["A", "B", "C"];
for (var i = 0; i < a.length; i++) {
    a.splice(i, 1);
}
alert( a );  // B

"B" was skipped.  This breaks down like this

- At iteration 0
         a[0] = "A"
         a[1] = "B"
         a[2] = "C"
         The value of a[0], i.e. "A", is removed; "B" becomes a[0]
         a[0] = "B"
         a[1] = "C"

- At iteration 1

         a[0] = "B"
         a[1] = "C"

         The value of a[1], i.e. "C", is removed; notice that "B" was skipped
         a[0] = "B"

- At iteration 2, the condition (i < a.length) is not met and the loop ends

 

This is solved thus

// backward iteration
var a = ["A", "B", "C", "D", "E", "F", "G"];
for(var i = a.length - 1; i > -1; i--) {
    a.splice(i, 1);
}
alert( a );  // empty array

 

3 replies

femkeblanco
femkeblancoCorrect answer
Legend
January 4, 2021

Hi @rcraighead . Sorry, I just noticed that if iterating backwards, the loop condition should be i >= 0 or i > -1, i.e. the line should be

for (var i = aDoc.pageItems.length - 1; i > -1; i--) {

This is extraneous to the subject of this thread, but forward vs. backward iteration can be summarised like this: When removing items from, or moving items to, the beginning of a collection, forward iteration will cause problems because of indices changing. For example, when removing items, every other item will be skipped. So, say you want to remove all items from a collection (here the array and splice(i, 1) substitute for a collection and remove() in illustrator):

// forward iteration
var a = ["A", "B", "C"];
for (var i = 0; i < a.length; i++) {
    a.splice(i, 1);
}
alert( a );  // B

"B" was skipped.  This breaks down like this

- At iteration 0
         a[0] = "A"
         a[1] = "B"
         a[2] = "C"
         The value of a[0], i.e. "A", is removed; "B" becomes a[0]
         a[0] = "B"
         a[1] = "C"

- At iteration 1

         a[0] = "B"
         a[1] = "C"

         The value of a[1], i.e. "C", is removed; notice that "B" was skipped
         a[0] = "B"

- At iteration 2, the condition (i < a.length) is not met and the loop ends

 

This is solved thus

// backward iteration
var a = ["A", "B", "C", "D", "E", "F", "G"];
for(var i = a.length - 1; i > -1; i--) {
    a.splice(i, 1);
}
alert( a );  // empty array

 

pixxxelschubser
Community Expert
Community Expert
January 4, 2021



@kylwell  wrote:
"… Is that what causes the multiples of commas? …"


An unnamed object does not have a name. Thereby empty elements are passed into the array, separated by commas.

rcraighead
Legend
January 4, 2021

Thank you @pixxxelschubser ! I'm in over my head as usual. My test file had all named objects so I missed that fact.

 

I did notice if I changed a Symbol Instance to the SAME name as the original Symbol the object was skipped. Guess AI cannot tell the difference between a manually typed name and the original generic Symbol name.

 

Edit:
So, if one wished to capture the "default" names given to objects in a document is that possible?

rcraighead
Legend
January 3, 2021

Can you share a sample file? Maybe someone can write a custom script for you.

 

This very rudimentary script will parse the document objects and list their names in a single text block in the top, left corner of the current document:

nameList();
function nameList() {
    var aDoc = app.activeDocument;
    var myObj, tFrame;
    aDoc.textFrames.add();
    var tFrame = aDoc.textFrames[0];
    tFrame.name = "myTextBlock";

    for (var i = 0; i < aDoc.pageItems.length; i++) {
        myObj = aDoc.pageItems[i];
        if (myObj.name != 'myTextBlock') {
            tFrame.textRange.characterAttributes.size = 10;
            tFrame.contents = (tFrame.contents + "\n" + myObj.name);
            tFrame.position = [5, -1];
        }
    }
}

 

 

 

 

kylwellAuthor
Known Participant
January 3, 2021

Well that kind of works. Anything more than a dozen objects and it gets very, very.... very slow. I'll have to see if I can do batches.

Larry G. Schneider
Community Expert
Community Expert
January 4, 2021

Does it work better if you replace the lines

for (var i = 0; i < aDoc.pageItems.length; i++; )

with 

for (var i = aDoc.pageItems.length; i <= 0; i--; )

 and see if it runs better.

Any time you work with a list you should work backwards.