Copy link to clipboard
Copied
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?
1 Correct answer
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
...Explore related tutorials & articles
Copy link to clipboard
Copied
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];
}
}
}
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
If iterating backwards, the counter should start with aDoc.pageItems.length - 1. It won't make a difference with regards to time in this case though.
Copy link to clipboard
Copied
Thanks @femkeblanco . I'm still learning about iterating backwards. Your comment is very helpful.
Copy link to clipboard
Copied
Is that what causes the multiples of commas?
You're right, this runs much faster. Just did a test with 167 objects and it was done in a blink.
Copy link to clipboard
Copied
The array entries are separated by commas. Copy and paste the result to any text editor and auto-replace the commas with returns. I used Text Edit on the Mac to create a list.
Copy link to clipboard
Copied
"Kind of works"? I tested it with 750 items. It took 4.5 minutes to create the text block. I'm sure there are ways to speed it up, but that's a whole lot faster than "copy/paste". 😉
@Larry G. Schneider , I was not able to get your suggestion to work. Not sure what I'm doing wrong.
The script is updating the text frame content + the new text each time. I don't know if there is a more efficient way to update a text item.
EDIT:
This replaces the last "Edit".
The solution was to save the "pageItems" array as a list of names and display it in an Alert at the end of the script. The result can be copied as a "Comma-delemited" text file and used in what ever way you wish. The time savings is substantial:
nameArray();
function nameArray() {
var aDoc = app.activeDocument;
var myObj, newName;
var data = [];
for (var i = 0; i < aDoc.pageItems.length; i++) {
myObj = aDoc.pageItems[i];
newName = myObj.name;
data.push(newName);
}
alert(data);
}
This example took less than 2 sec to complete:
Copy link to clipboard
Copied
Let me give that a try. I'd tried it with 40 some odd objects and after 20 minutes I shut it down.
And belatedly, thanks! I was not looking forward to copy & pasting.
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
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?
Copy link to clipboard
Copied
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

