Highlighted

Script required to find swatches with specific RGB values then rename the swatch

New Here ,
Apr 17, 2019

Copy link to clipboard

Copied

Hi all,

I have a challenge / bug bear that I want to put to bed forever - hopefully with your skills, expertise and help.

I'm using Illustrator 2019 on a windows pc.

Challenge

I'm looking for a script that can select a swatch using a specific RGB attribute, then using that RGB attribute - the scrip would change the "swatch Name" from whatever it has been named as (during import) - to a logical name for that layer. eg: "cut" or "crease" or "Bleed" (printing terms).

Background info:

I receive lots of autocad drawings from the same person (NB: Every AutoCad drawing starts from the same template file)

and the swatch colour names seem to be randomly generated from drawing to drawing - when opened into illustrator.

However the RGB colours for each swatch - in every drawing I receive - remain consistently the same
no matter what the assigned (AUTOCAD colour) name is.

During the import process from Autocad to illustrator - swatches are named randomly (as AUTOCAD Colour 1" 2 etc) -
I want to rename the colours to match the layers again. The following (Ideal) swatch names for the RGB values (below)
are what I'm trying to achieve with this script:

SWATCH NAME        RGB VALUES

Cut layer -                  255,0,0

Crease   -                   0,153,0

Bleed -                       0,127,255

Dimensions -              0,255,0

Unprinted zone -        255,0,255

Lines -                        0,0,0

Construction -            255,255,0

Corner datum -          0,255,255

10mm cut/crease -    0,0,255

2 x 2mm perf -           254,191,16

Image of Swatches in document          For reference only - Image of LAYERS in the document

                                                             (With original Autocad Layer names - intact)

The above Swatch name list can vary sometimes, eg - sometimes there will be more or sometimes less layers.

it all depends upon the print dieline requirements, the layers listed are the main ones used
The Red lines are examples of swatch colours that may be there on occasion.

I don't know if it is possible for a script to do this
(including the chance that some layers may or may not be present) ?

but if it was, this would save me the task of repeatedly doing this routine for every drawing I import.

If I can achieve removal of 90% of this repetition (by your clever scripting)
it may well retain some of my sanity for manually re-naming the remaining swatches that can't be captured.

I'd welcome your thoughts and thank you in anticipation of any help.


PS I'm in Australia so I'm not ignoring you - if there is a delay in my reply.

Daryl

TOPICS
Scripting

Views

534

Likes

Translate

Translate

Report

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

Script required to find swatches with specific RGB values then rename the swatch

New Here ,
Apr 17, 2019

Copy link to clipboard

Copied

Hi all,

I have a challenge / bug bear that I want to put to bed forever - hopefully with your skills, expertise and help.

I'm using Illustrator 2019 on a windows pc.

Challenge

I'm looking for a script that can select a swatch using a specific RGB attribute, then using that RGB attribute - the scrip would change the "swatch Name" from whatever it has been named as (during import) - to a logical name for that layer. eg: "cut" or "crease" or "Bleed" (printing terms).

Background info:

I receive lots of autocad drawings from the same person (NB: Every AutoCad drawing starts from the same template file)

and the swatch colour names seem to be randomly generated from drawing to drawing - when opened into illustrator.

However the RGB colours for each swatch - in every drawing I receive - remain consistently the same
no matter what the assigned (AUTOCAD colour) name is.

During the import process from Autocad to illustrator - swatches are named randomly (as AUTOCAD Colour 1" 2 etc) -
I want to rename the colours to match the layers again. The following (Ideal) swatch names for the RGB values (below)
are what I'm trying to achieve with this script:

SWATCH NAME        RGB VALUES

Cut layer -                  255,0,0

Crease   -                   0,153,0

Bleed -                       0,127,255

Dimensions -              0,255,0

Unprinted zone -        255,0,255

Lines -                        0,0,0

Construction -            255,255,0

Corner datum -          0,255,255

10mm cut/crease -    0,0,255

2 x 2mm perf -           254,191,16

Image of Swatches in document          For reference only - Image of LAYERS in the document

                                                             (With original Autocad Layer names - intact)

The above Swatch name list can vary sometimes, eg - sometimes there will be more or sometimes less layers.

it all depends upon the print dieline requirements, the layers listed are the main ones used
The Red lines are examples of swatch colours that may be there on occasion.

I don't know if it is possible for a script to do this
(including the chance that some layers may or may not be present) ?

but if it was, this would save me the task of repeatedly doing this routine for every drawing I import.

If I can achieve removal of 90% of this repetition (by your clever scripting)
it may well retain some of my sanity for manually re-naming the remaining swatches that can't be captured.

I'd welcome your thoughts and thank you in anticipation of any help.


PS I'm in Australia so I'm not ignoring you - if there is a delay in my reply.

Daryl

TOPICS
Scripting

Views

535

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Apr 17, 2019 0
Engaged ,
Apr 24, 2019

Copy link to clipboard

Copied

Updated to try to help a bit more...

This will look for the swatches per your specs and then rename them. A layer will be created with your naming example.

It will also look at all of the lines and if the line color matches your specs, the line will be moved to the appropriate layer.

Final step is any empty layers will be deleted.

This might not be the most efficient code but it will get done what you are asking. Plus you can add in more specs as in if you change the line weight and formatting for a certain line type.... add it in to the proper section to further automate things.

I tried adding in comments to the code. Hope it helps!

#target illustrator-23

// Get the active document

var doc = app.activeDocument;

// Collect all of the swatches in the active document

var allSwatches = doc.swatches;

// Collect all layers in the active document

var allLayers = doc.layers;

// Collect all paths in the active document

var allPaths = doc.pathItems;

// Function to create a layer name

function createTheLayer() {

    var createLayer = allLayers.add();

    createLayer.name = String(allSwatches.name);

}

// Create a new layer for objects that don't match any of your specs

var notAssigned = allLayers.add();

notAssigned.name = "Not Assigned";

// Look through all of the swatches in the document

for (var i = 0; i < allSwatches.length; i++) {

    // Cut layer

    if (allSwatches.color.red == 255 && allSwatches.color.green == 0 && allSwatches.color.blue == 0) {

        allSwatches.name = "Cut Layer";

        createTheLayer();

    }

    // Crease

    else if (allSwatches.color.red == 0 && allSwatches.color.green == 153 && allSwatches.color.blue == 0) {

        allSwatches.name = "Crease";

        createTheLayer();

    }

    // Bleed

    else if (allSwatches.color.red == 0 && allSwatches.color.green == 127 && allSwatches.color.blue == 255) {

        allSwatches.name = "Bleed";

        createTheLayer();

    }

    // Dimensions

    else if (allSwatches.color.red == 0 && allSwatches.color.green == 255 && allSwatches.color.blue == 0) {

        allSwatches.name = "Dimensions";

        createTheLayer();

    }

    // Unprinted zone

    else if (allSwatches.color.red == 255 && allSwatches.color.green == 0 && allSwatches.color.blue == 255) {

        allSwatches.name = "Unprinted zone";

        createTheLayer();

    }

    // Lines

    else if (allSwatches.color.red == 0 && allSwatches.color.green == 0 && allSwatches.color.blue == 0) {

        allSwatches.name = "Lines";

        createTheLayer();

    }

    // Construction

    else if (allSwatches.color.red == 255 && allSwatches.color.green == 255 && allSwatches.color.blue == 0) {

        allSwatches.name = "Construction";

        createTheLayer();

    }

    // Corner datum

    else if (allSwatches.color.red == 0 && allSwatches.color.green == 255 && allSwatches.color.blue == 255) {

        allSwatches.name = "Corner datum";

        createTheLayer();

    }

    // 10mm cut-crease

    else if (allSwatches.color.red == 0 && allSwatches.color.green == 0 && allSwatches.color.blue == 255) {

        allSwatches.name = "10mm cut-crease";

        createTheLayer();

    }

    // 2 x 2mm perf

    else if (allSwatches.color.red == 254 && allSwatches.color.green == 191 && allSwatches.color.blue == 16) {

        allSwatches.name = "2 x 2mm perf";

        createTheLayer();

    }

}

for (var z = 0; z < allPaths.length; z++) {

    // Cut layer

    if (allPaths.strokeColor.red == 255 && allPaths.strokeColor.green == 0 && allPaths.strokeColor.blue == 0) {

        var targetLayer = allLayers.getByName("Cut Layer");

        allPaths.move(targetLayer, ElementPlacement.PLACEATEND);

    }

    // Crease

    else if (allPaths.strokeColor.red == 0 && allPaths.strokeColor.green == 153 && allPaths.strokeColor.blue == 0) {

        var targetLayer = allLayers.getByName("Crease");

        allPaths.move(targetLayer, ElementPlacement.PLACEATEND);

    }

    // Bleed

    else if (allPaths.strokeColor.red == 0 && allPaths.strokeColor.green == 127 && allPaths.strokeColor.blue == 255) {

        var targetLayer = allLayers.getByName("Bleed");

        allPaths.move(targetLayer, ElementPlacement.PLACEATEND);

    }

    // Dimensions

    else if (allPaths.strokeColor.red == 0 && allPaths.strokeColor.green == 255 && allPaths.strokeColor.blue == 0) {

        var targetLayer = allLayers.getByName("Dimensions");

        allPaths.move(targetLayer, ElementPlacement.PLACEATEND);

    }

    // Unprinted zone

    else if (allPaths.strokeColor.red == 255 && allPaths.strokeColor.green == 0 && allPaths.strokeColor.blue == 255) {

        var targetLayer = allLayers.getByName("Unprinted zone");

        allPaths.move(targetLayer, ElementPlacement.PLACEATEND);

    }

    // Lines

    else if (allPaths.strokeColor.red == 0 && allPaths.strokeColor.green == 0 && allPaths.strokeColor.blue == 0) {

        var targetLayer = allLayers.getByName("Lines");

        allPaths.move(targetLayer, ElementPlacement.PLACEATEND);

    }

    // Construction

    else if (allPaths.strokeColor.red == 255 && allPaths.strokeColor.green == 255 && allPaths.strokeColor.blue == 0) {

        var targetLayer = allLayers.getByName("Construction");

        allPaths.move(targetLayer, ElementPlacement.PLACEATEND);

    }

    // Corner datum

    else if (allPaths.strokeColor.red == 0 && allPaths.strokeColor.green == 255 && allPaths.strokeColor.blue == 255) {

        var targetLayer = allLayers.getByName("Corner datum");

        allPaths.move(targetLayer, ElementPlacement.PLACEATEND);

    }

    // 10mm cut-crease

    else if (allPaths.strokeColor.red == 0 && allPaths.strokeColor.green == 0 && allPaths.strokeColor.blue == 255) {

        var targetLayer = allLayers.getByName("10mm cut-crease");

        allPaths.move(targetLayer, ElementPlacement.PLACEATEND);

    }

    // 2 x 2mm perf

    else if (allPaths.strokeColor.red == 254 && allPaths.strokeColor.green == 191 && allPaths.strokeColor.blue == 16) {

        var targetLayer = allLayers.getByName("2 x 2mm perf");

        allPaths.move(targetLayer, ElementPlacement.PLACEATEND);

    }

    else {

        // No matching properties per your specs

        var targetLayer = allLayers.getByName("Not Assigned");

        allPaths.move(targetLayer, ElementPlacement.PLACEATEND);

    }

}

// Loop backward through layers and delete any empty ones

for (var xy = allLayers.length - 1; xy >= 0; xy--) {

    if (allLayers[xy].pageItems.length == 0 && allLayers[xy].layers.length == 0) {

        allLayers[xy].remove();

    }

}

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Apr 24, 2019 0
Adobe Community Professional ,
Apr 25, 2019

Copy link to clipboard

Copied

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Apr 25, 2019 2