In ExtendScript for Adobe Illustrator, you can select all objects in an array in a document by assigning an app.selection
collection.
var doc = app.activeDocument;
var arr = [doc.pathItems[0], doc.pathItems[1]];
app.selection = arr;
However, Adobe’s documentation does not mention an important limitation that can cause scripts to work incorrectly. Assigning an array to app.selection
will select a maximum of 1000 objects — the first in order in the user’s array. Other objects are ignored and not selected by the script.
Solution #1
With the same speed, you can select any object in the array through a for loop by toggling the selected
boolean property.
var counter = arr.length;
for (var i = 0; i < counter; i++) {
arr[i].selected = true;
}
All objects in the array are selected, but in this example, waiting 38 seconds is too long. With a larger number of objects, the loop will run longer: minutes, tens of minutes. Performance is especially bad on low-spec PC and Mac.
Solution #2
Assume that an object is selected quickly in Illustrator. If all the objects in the array are in the same group, they will be selected quickly.
Let’s create a temporary group in the script and place the objects in it. Taking advantage of the fact that objects stay selected in Illustrator after ungrouping, we can ungroup our temporary group and return the objects to their places in the layers.
function selectItems(arr) {
var tmpArr = []; // Create temporary array for objects
var lay = arr[0].layer; // Get a layer for the group
var tmpGroup = lay.groupItems.add(); // Create a temporary group for objects
var counter = arr.length;
// Add temporary paths before each source in layer
// And we move the original objects to the group
for (var i = 0; i < counter; i++) {
var tmpItem = lay.pathItems.add();
tmpItem.move(arr[i], ElementPlacement.PLACEBEFORE);
tmpArr.push(tmpItem);
arr[i].move(tmpGroup, ElementPlacement.PLACEATEND);
}
tmpGroup.selected = true;
// Move the original objects from the group to their positions
// Move the temporary paths to the group
var groupItems = tmpGroup.pageItems;
for (var j = 0; j < counter; j++) {
groupItems[j].move(tmpArr[j], ElementPlacement.PLACEBEFORE);
tmpArr[j].move(tmpGroup, ElementPlacement.PLACEATBEGINNING);
}
// Remove a temporary group with temporary paths
tmpGroup.remove();
}
The time difference is impressive, and on tens of thousands of objects it is measured in seconds, not minutes, even on old computers. In various tests, after moving to and from a group of objects, the hierarchy in layers is preserved.