新規スウォッチを追加するスクリプト RGBモードのドキュメントで実行された場合はRGBカラーを、CMYKモードのドキュメントで実行された場合はCMYKカラーを使用するようにします


if (app.documents.length > 0) {
    var docRef = app.activeDocument;

    // Check the document color mode
    if (docRef.documentColorSpace === DocumentColorSpace.CMYK) {
        // CMYK mode
        var color = new CMYKColor();
        color.cyan = 75;
        color.magenta = 50;
        color.yellow = 20;
        color.black = 5;
    } else {
        // RGB mode
        var color = new RGBColor();
        color.red = 100;
        color.green = 150;
        color.blue = 200;
    }

    // Create the new swatch using the determined color
    var swatch = docRef.swatches.add();
    swatch.color = color;
    swatch.name = "CreateSwatch";

    // Apply the swatch to a new path item
    var pathRef = docRef.pathItems.star(300, 300, 100, 30, 4, false);
    pathRef.filled = true;
    pathRef.fillColor = swatch.color;
    pathRef.stroked = true;
    pathRef.strokeColor = swatch.color;
}

指定したスウォッチの色を変更する(RGB)

if (app.documents.length > 0) {
    var docRef = app.activeDocument;

    // Find the first swatch in the document
    // var swatchIndex = 2; // white swatch
    var swatchIndex = docRef.swatches.length-1;
    var currentSwatch = docRef.swatches[swatchIndex];

    // Apply new color to the swatch
    var rgbColor = currentSwatch.color;
    rgbColor.red = 100;
    rgbColor.green = 150;
    rgbColor.blue = 20;

    // Apply new name  to the swatch
    currentSwatch.name = "EditedSwatch";

}