← Back to list

App.selection limit

In ExtendScript for Adobe Illustrator, you can select all objects in an array in a document by assigning an app.selection collection.

AiScripts · 2025-01-20 04:46 · 0 claps · 2.3 min read
#adobe-illustrator #extendscript #javascript #graphic-design
Open on Medium ↗
Wiki topics: VIS · Visual & Graphic Design TLS · Design Tools & Workflow 🌐 · Web Development 🖊️ · Illustration & Drawing 🏛️ · Politics

App.selection limit

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) {
  if (Object.prototype.toString.call(arr) !== '[object Array]') return;
  if (!arr.length) return;

  var tempLayer = app.activeDocument.layers.add(); // Get a layer for the group
  tempLayer.name = '__tempSelectionLayer__';
  var tempGroup = tempLayer.groupItems.add(); // Create a temporary group for objects
  var tempPairs = []; // Create temporary array for objects

  // Add temporary paths before each source in layer
  // And we move the original objects to the group
  for (var i = 0; i < arr.length; i++) {
    try {
      var tempItem = tempLayer.pathItems.add();
      tempItem.move(arr[i], ElementPlacement.PLACEBEFORE);
      arr[i].move(tempGroup, ElementPlacement.PLACEATEND);
      tempPairs.push({ 'placeholder': tempItem, 'original': arr[i] });
    } catch (err) {
      if (tempItem) tempItem.remove();
    }
  }

  tempGroup.selected = true;

  // Move the original objects from the group to their positions
  // Remove the temporary paths
  for (var j = tempPairs.length - 1; j >= 0; j--) {
    var placeholder = tempPairs[j].placeholder;
    var original = tempPairs[j].original;
    try {
      original.move(placeholder, ElementPlacement.PLACEBEFORE);
      placeholder.remove();
    } catch (err) {
      try { placeholder.remove(); } catch (err) {}
    }
  }

  try { tempGroup.remove(); } catch (err) {}
  try { tempLayer.remove(); } catch (err) {}
}

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.


메타데이터
post_id
03e0b441ebe3
slug
app-selection-limit-03e0b441ebe3
url
https://medium.com/@aiscripts/app-selection-limit-03e0b441ebe3
canonical_url
https://medium.com/@aiscripts/app-selection-limit-03e0b441ebe3
author_url
https://medium.com/@aiscripts
status
ok
fetched_at
2026-06-09 15:37:30