Skip to main content
Participant
October 20, 2023
Answered

Can't assign a name to a swatch color with script

  • October 20, 2023
  • 2 replies
  • 406 views

Hello!
With the following script, I want to rename a range of colors in my swatch from 1 to x.

var doc = app.activeDocument;
var swatches = doc.swatches;
var firstColorID = 16;
var lastColorIDfromEnd = 4;

RenameSwatchesColors(firstColorID, lastColorIDfromEnd);

function RenameSwatchesColors(firstID, lastID) {
    var swatchesLength = swatches.length;
    var newName = 1;
    for (var i = firstColorID; i < swatchesLength - lastColorIDfromEnd; i++) {
        alert(swatches[i].name);
        swatches[i].name = newName.toString();
        newName++;
    }
}

If I only left the alert part that display the color name, everything is ok! The range is good, all colors within it are properly called.
When I assigned the name with "swatches[i].name = ", for some reason that I don't understand I got an error : "Exception has occured 8701. The name was not found".

But it was displayed with the alert! So it's basically an assignation problem, but why? I don't see any differences from others colors in the swatch. I have even tried to delete and recreate the problematic colors, but nothing changed. 
Thank you if you have any clue... 

This topic has been closed for replies.
Correct answer Sergey Osokin

An error may occur when changing the name if your swatch range contains the [Registration] color. I got error 8701 when I tried to assign a numeric name to [Registration]. But the letter name was assigned. Then I tried to undo the history and already [Registration] changed to a numeric name without any error.

 

But the [Registration] default color is at the position with index 1, and the test code renames colors starting at index 16This is an interesting case to study.

 

2 replies

Sergey Osokin
Sergey OsokinCorrect answer
Inspiring
October 20, 2023

An error may occur when changing the name if your swatch range contains the [Registration] color. I got error 8701 when I tried to assign a numeric name to [Registration]. But the letter name was assigned. Then I tried to undo the history and already [Registration] changed to a numeric name without any error.

 

But the [Registration] default color is at the position with index 1, and the test code renames colors starting at index 16This is an interesting case to study.

 

Participant
October 21, 2023

Yes you got a point, thank you for your answer. I still don't understand why some colors return an error (and not the others) but it's clearly due to the numeric values. I have noticed that I also get the error when 2 colors have the same name in the swatch. But at this point i'm 100% sure that all my colors have a unique name but still get this error.
Anyway, I change my assignation line by : 

"swatches[i].name = "_" + newName.toString();"
And that works perfectly ! So it's best to avoid associating color names in swatch with numerical values 🙂
CarlosCanto
Community Expert
Community Expert
October 20, 2023

your script worked fine here, using cc2024 on Windows