Skip to main content
Known Participant
June 11, 2022
Answered

Add black color to the color list - only if it does not exist in the color list

  • June 11, 2022
  • 1 reply
  • 522 views

 

Hello I am looking for a script that will add black color to the color list
) black = 100)


Only if it does not exist in the color list. not spot color!

This topic has been closed for replies.
Correct answer Charu Rajput

var _doc = app.documents[0];
var _isBlackExists = false;
var _swatches = _doc.swatches;
for (var i = 0; i < _swatches.length; i++) {
var _swatch = _swatches[i];
if (_swatch.color.cyan == 0 && _swatch.color.yellow == 0 && _swatch.color.magenta == 0 && _swatch.color.black == 100) {
_isBlackExists = false;
break;
} else if (_swatch.name.toLowerCase() == 'black') {
_isBlackExists = false;
break;
}
}
if (!_isBlackExists) {
var _blackSwatch = _doc.swatches.add();
var _color = new CMYKColor();
_color.cyan = 0;
_color.yellow = 0;
_color.magenta = 0;
_color.black = 100;
_blackSwatch.color = _color;
_blackSwatch.name = 'Black';
}


If you set this to false, then Balck color will always be created even if it exists

1 reply

Charu Rajput
Community Expert
Community Expert
June 11, 2022

Hello,

You can try the following

var _doc = app.documents[0];
var _isBlackExists = false;
var _swatches = _doc.swatches;
for (var i = 0; i < _swatches.length; i++) {
    var _swatch = _swatches[i];
    if (_swatch.color.cyan == 0 && _swatch.color.yellow == 0 && _swatch.color.magenta == 0 && _swatch.color.black == 100) {
        _isBlackExists = true;
        break;
    } else if (_swatch.name.toLowerCase() == 'black') {
        _isBlackExists = true;
        break;
    }
}
if (!_isBlackExists) {
    var _blackSwatch = _doc.swatches.add();
    var _color = new CMYKColor();
    _color.cyan = 0;
    _color.yellow = 0;
    _color.magenta = 0;
    _color.black = 100;
    _blackSwatch.color = _color;
    _blackSwatch.name = 'Black';
}
Best regards
aviel222Author
Known Participant
June 11, 2022

Hey
That I changed to false
It works well. Thanks

Charu Rajput
Community Expert
Community Expert
June 11, 2022

What you have changed to false? Please specify.

Best regards