Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

Illustrator Script Error at Line 20 with Compound Paths

New Here ,
Sep 24, 2025 Sep 24, 2025

Hi, so I ran into a blocking error today while running RandomSwatchesFill.js, a well-known script for Adobe Illustrator. The goal is simple: fill the selected shapes with a random color from the swatches I have selected. It worked perfectly yesterday, but today Illustrator suddenly stops with an error on line 20. Nothing has changed in my Illustrator setup between then and now, which makes the issue even more puzzling.

Screenshot_50.png

The Script:

 

 
mySelection = app.activeDocument.selection;
myDoc = app.activeDocument;
if (mySelection instanceof Array)
{
    selSwatches = myDoc.swatches.getSelected();
    
    if(selSwatches.length != 0)
        for (i=0; i<mySelection.length; i++)
        {
            if(mySelection[i].typename == "PathItem" || mySelection[i].typename == "CompoundPathItem")
            {
                selItem = mySelection[i];
                selItem.filled = true;

                swatchIndex = Math.round( Math.random() * (selSwatches.length - 1 ));
                
                if(selItem.typename == "PathItem")
                    selItem.fillColor = selSwatches[swatchIndex].color;
                else
                    selItem.pathItems[0].fillColor = selSwatches[swatchIndex].color;
                
            }
        }
}

 

Illustrator reports the error at line 20:

 

 
selItem.pathItems[0].fillColor = selSwatches[swatchIndex].color;

 

Anyway if you can help me resolve this problem that would be great

TOPICS
Bug , Draw and design , Scripting
181
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Adobe
Enthusiast ,
Sep 24, 2025 Sep 24, 2025

How are these objects made? If you call the Release Compound Path, will there be groups of objects? If there are groups, the script cannot get the first path selItem.pathItems[0] inside the compound path.

 

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Oct 14, 2025 Oct 14, 2025

ok so when i Release Compound Path, i have some groups of objects yes, how can color them to have their original shape? i need to color them before? what would be the solution to my first problem? i want to color those shape randomly with their hole in it.

.Screenshot_52.png

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Oct 14, 2025 Oct 14, 2025

can you make the shapes without grouping?

 

show what's in the compound path

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Enthusiast ,
Oct 14, 2025 Oct 14, 2025
LATEST

In the specific case, you have the same objects arranged on a grid. It would be more correct to ungroup one and create a "clean" compound path from it again. Then recreate the grid from the copies and then run the original script.

Another solution is to cyclically ungroup paths into compound paths in the code, which fixes them, but this can take a long time if there are thousands of paths in the document. There are other approaches in scripts for fixing compound paths created from groups, perhaps there is no point in offering many versions of the script here.

main();

function main() {
  var myDoc = app.activeDocument;
  var mySelection = myDoc.selection;

  if (!mySelection.length || mySelection.typename === 'TextRange') {
    return;
  }

  var selSwatches = myDoc.swatches.getSelected();
  if (selSwatches.length === 0) return;

  for (var i = 0; i < mySelection.length; i++) {
    var selItem = mySelection[i];
    if (selItem.typename !== "PathItem" && selItem.typename !== "CompoundPathItem") continue;

    var swatchIndex = Math.round(Math.random() * (selSwatches.length - 1));

    selItem.filled = true;

    if (selItem.typename == "PathItem") {
      selItem.fillColor = selSwatches[swatchIndex].color;
    } else {
      if (selItem.pathItems.length === 0) {
        // Fix compound paths created from group. May work slowly
        var compPath = selItem;
        for (var i = myDoc.pathItems.length - 1; i >= 0; i--) {
          if (getParent(myDoc.pathItems[i]) === compPath) {
            myDoc.pathItems[i].move(compPath, ElementPlacement.PLACEATEND);
          }
        }
      }
      selItem.pathItems[0].fillColor = selSwatches[swatchIndex].color;
    }
  }
}

function getParent(item) {
  var prnt = null;
  var superprnt = item.parent;
  while (superprnt && (superprnt.typename != "Layer")) {
    prnt = superprnt;
    superprnt = superprnt.parent;
  }
  return prnt;
}

 

 

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Sep 24, 2025 Sep 24, 2025

insert 

alert(selItem.typename);

 

before the error line to find out what type of object is causing the error and we go from there

 

[edit] it will always be "compound path" duh! ignore my comment, follow Sergey comments

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines