0

I am trying to assign random colors to vector shapes in Illustrator 2021 on my Mac (running macOS BigSur). With some research, I found the script below.

Each time I run it, however, I get an error message: Error 8. Syntax error./Line: 1/-> {\rtf1\ansi\ansicpg1252\cocoartff2578

I don't do much coding in Javascript, so I am at sea when it comes to figuring out what to fix...

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;
                
            }
        }
}

Any help is gratefully accepted!

3
  • Error indicates that you havent saved a plain text file but rather a rich text file – joojaa Mar 30 at 5:32
  • If you’ve just copied the text from the Internet, opened TextEdit and pasted, then saved, there’s a good chance the formatting from the web page you copied from has been carried over, and TextEdit will then save as an RTF file. Scripts need to be plain text, with no formatting of any kind. See this question on how to change it to plain text in TextEdit. – Janus Bahs Jacquet Mar 30 at 7:42
  • Thank you! This fixed it! – Brian Watson Mar 30 at 17:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.