• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

Moving objects with spot color to a new layer javascript

Explorer ,
Feb 16, 2023 Feb 16, 2023

Copy link to clipboard

Copied

Hello
I have objects with a certain spot color, I need to move them to a new layer
)which does not exist.)

 

TOPICS
Scripting

Views

1.9K

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
community guidelines

correct answers 2 Correct answers

Community Expert , Feb 16, 2023 Feb 16, 2023
//author: William Dowling
//adobe forum username: Disposition_Dev
//email: illustrator.dev.pro@gmail.com
//linkedin: https://www.linkedin.com/in/william-dowling-4537449a/
//Adobe Discussion Forum Post about this function: https://community.adobe.com/t5/illustrator-discussions/moving-objects-with-spot-color-to-a-new-layer-javascript/td-p/13584610

//*******//

//Did you find this useful? Would you like to buy me a cup of coffee to say thanks?
//paypal.me/illustratordev
//<3

//*******//



//Move
...

Votes

Translate

Translate
Guide , Feb 17, 2023 Feb 17, 2023

How about changing this

    doc.selection = null;
    doc.defaultFillColor = mySpotSwatch.color;
    doc.defaultStrokeColor = mySpotSwatch.color;

    //get filled items matching the spot color
    app.executeMenuCommand( "Find Fill Color menu item" );
    for ( var s = 0; s < doc.selection.length; s++ )
    {
        doc.selection[ s ].moveToBeginning( myLayer );
    }

    //get stroked items matching the spot color
    doc.selection = null;
    app.executeMenuCommand( "Find Stroke Color menu i
...

Votes

Translate

Translate
Adobe
Community Expert ,
Feb 16, 2023 Feb 16, 2023

Copy link to clipboard

Copied

//author: William Dowling
//adobe forum username: Disposition_Dev
//email: illustrator.dev.pro@gmail.com
//linkedin: https://www.linkedin.com/in/william-dowling-4537449a/
//Adobe Discussion Forum Post about this function: https://community.adobe.com/t5/illustrator-discussions/moving-objects-with-spot-color-to-a-new-layer-javascript/td-p/13584610

//*******//

//Did you find this useful? Would you like to buy me a cup of coffee to say thanks?
//paypal.me/illustratordev
//<3

//*******//



//Move Spot Color Items to New Layer
//
// pass the name of the spot color as a string
// and this script will move all objects with that spot color to a new layer

//arguments
//
//mySpotColorName : string. The name of the spot color you want to move to a new layer


//Dependencies
//
//none

function moveSpotColorItemsToNewLayer ( mySpotColorName )
{
    var doc = app.activeDocument;
    var mySpotSwatch;
    for ( var i = 0; i < doc.swatches.length && !mySpotSwatch; i++ )
    {
        if ( doc.swatches[ i ].name.match( mySpotColorName ) )
        {
            mySpotSwatch = doc.swatches[ i ];
        }
    }
    if ( !mySpotSwatch )
    {
        alert( "Spot color \"" + mySpotColorName + "\" not found" );
        return;
    }

    var myLayer = doc.layers.add();
    myLayer.name = mySpotColorName + " Objects";

    doc.selection = null;
    doc.defaultFillColor = mySpotSwatch.color;

    //get filled items matching the spot color
    app.executeMenuCommand( "Find Fill Color menu item" );
    for ( var s = 0; s < doc.selection.length; s++ )
    {
        doc.selection[ s ].moveToBeginning( myLayer );
    }

    doc.selection = null;
    app.redraw();
    doc.defaultStrokeColor = mySpotSwatch.color;

    //get stroked items matching the spot color
    app.executeMenuCommand( "Find Stroke Color menu item" );
    for ( var s = 0; s < doc.selection.length; s++ )
    {
        doc.selection[ s ].moveToBeginning( myLayer );
    }

    doc.selection = null;

    alert( "All Done." );

}
moveSpotColorItemsToNewLayer( "mySpotColorName" );

Votes

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
community guidelines
Explorer ,
Feb 16, 2023 Feb 16, 2023

Copy link to clipboard

Copied

Hello
It doesn't work well
I opened a new document with spot color:
mySpotColorName

There is an error message. Situation.

Votes

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
community guidelines
Explorer ,
Feb 16, 2023 Feb 16, 2023

Copy link to clipboard

Copied

 

Votes

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
community guidelines
Community Expert ,
Feb 16, 2023 Feb 16, 2023

Copy link to clipboard

Copied

Apologies. I made a typo in the code. I've edited my previous reply and tested it on your example file with success.

Votes

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
community guidelines
Explorer ,
Feb 17, 2023 Feb 17, 2023

Copy link to clipboard

Copied

HY

 

DO you remmember me?

Votes

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
community guidelines
Community Expert ,
Feb 17, 2023 Feb 17, 2023

Copy link to clipboard

Copied

From this post, yes. But I don't know if we know each other previously.

 

Are you waiting for something from me? I just edited my other post to fix the typo. Is that updated code not working for you?

 

Votes

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
community guidelines
Mentor ,
Feb 17, 2023 Feb 17, 2023

Copy link to clipboard

Copied

Hey William, this isn't working for me.

The reason seems to be that the stroke is the same color, even though the fills are different...

I'm not sure how to isolate the selection of the stroke so it doesn't pick up the stroke from the fill selection.

Votes

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
community guidelines
Guide ,
Feb 17, 2023 Feb 17, 2023

Copy link to clipboard

Copied

How about changing this

    doc.selection = null;
    doc.defaultFillColor = mySpotSwatch.color;
    doc.defaultStrokeColor = mySpotSwatch.color;

    //get filled items matching the spot color
    app.executeMenuCommand( "Find Fill Color menu item" );
    for ( var s = 0; s < doc.selection.length; s++ )
    {
        doc.selection[ s ].moveToBeginning( myLayer );
    }

    //get stroked items matching the spot color
    doc.selection = null;
    app.executeMenuCommand( "Find Stroke Color menu item" );
    for ( var s = 0; s < doc.selection.length; s++ )
    {
        doc.selection[ s ].moveToBeginning( myLayer );
    }

    doc.selection = null;

to this

    doc.selection = null;
    doc.defaultFillColor = mySpotSwatch.color;
    app.executeMenuCommand( "Find Fill Color menu item" );
    doc.defaultStrokeColor = mySpotSwatch.color;
    app.executeMenuCommand( "Find Stroke Color menu item" );
    for ( var s = 0; s < doc.selection.length; s++ ) {
        doc.selection[ s ].moveToEnd( myLayer );
    }

Votes

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
community guidelines
Mentor ,
Feb 17, 2023 Feb 17, 2023

Copy link to clipboard

Copied

Thanks Femke, that certainly separates the fills, but now the stroke is the same color as the fill, mySpotColorName. I guess that's the work around to separate the strokes from one another.

I'm not asking for a solution, I'm not the OP, I just noticed on William's original that it didn't (necessarily) work.

Votes

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
community guidelines
Community Expert ,
Feb 17, 2023 Feb 17, 2023

Copy link to clipboard

Copied

I'm not really sure what you meant in your first reply. 

 

"I'm not sure how to isolate the selection of the stroke so it doesn't pick up the stroke from the fill selection."

Are you saying that the stroke of the filled items changes? like they either start with no stroke, or a different stroke and then the mySpotSwatch gets applied to the stroke?

 

I didn't see that on my end, but i also didn't test extensively. It probably needs an app.redraw() in between the stroke/fill processing to ensure the selection is what it should be.. Sometimes executeMenuCommands don't update the dom right away so the next loop still includes the previously selected items.

Votes

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
community guidelines
Community Expert ,
Feb 17, 2023 Feb 17, 2023

Copy link to clipboard

Copied

this solution will change the stroke of some items in the document. because it selects the filled items and then sets the default document stroke color, it will change the stroke of all selected items. I processed them separately to avoid changing any of the artwork that's being moved.

Votes

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
community guidelines
Explorer ,
Apr 04, 2023 Apr 04, 2023

Copy link to clipboard

Copied

LATEST

Hello everyone

I need help updating a script they wrote to me:

I have a document with 3 shapes that each have a line with a different spot color:


1. Cut contour
2. Big
3. Pnimi

The script is like this:

I need the computer to recognize the spots
and will turn them into spot colors with the following values:


CutContour = R=255 G=0 B=0
Big = R=0 G=255 B=0
Pnimi = R=0 G=0 B=255


After that, will move all the lines to a new layer.

 

 

 

 

 

The script:

//author: William Dowling
//adobe forum username: Disposition_Dev
//email: illustrator.dev.pro@gmail.com
//linkedin: https://www.linkedin.com/in/william-dowling-4537449a/
//Adobe Discussion Forum Post about this function: https://community.adobe.com/t5/illustrator-discussions/moving-objects-with-spot-color-to-a-new-layer...

//*******//

//Did you find this useful? Would you like to buy me a cup of coffee to say thanks?
//paypal.me/illustratordev
//<3

//*******//

 

//Move Spot Color Items to New Layer
//
// pass the name of the spot color as a string
// and this script will move all objects with that spot color to a new layer

//arguments
//
//mySpotColorName : string. The name of the spot color you want to move to a new layer


//Dependencies
//
//none

function moveSpotColorItemsToNewLayer ( mySpotColorName )
{
var doc = app.activeDocument;
var mySpotSwatch;
for ( var i = 0; i < doc.swatches.length && !mySpotSwatch; i++ )
{
if ( doc.swatches[ i ].name.match( mySpotColorName ) )
{
mySpotSwatch = doc.swatches[ i ];
}
}
if ( !mySpotSwatch )
{
alert( "Spot color \"" + mySpotColorName + "\" not found" );
return;
}

var myLayer = doc.layers.add();
myLayer.name = mySpotColorName;

doc.selection = null;
doc.defaultFillColor = mySpotSwatch.color;

//get filled items matching the spot color
app.executeMenuCommand( "Find Fill Color menu item" );
for ( var s = 0; s < doc.selection.length; s++ )
{
doc.selection[ s ].moveToBeginning( myLayer );
}

doc.selection = null;
doc.defaultFillColor = mySpotSwatch.color;
app.executeMenuCommand( "Find Fill Color menu item" );
doc.defaultStrokeColor = mySpotSwatch.color;
app.executeMenuCommand( "Find Stroke Color menu item" );
for ( var s = 0; s < doc.selection.length; s++ ) {
doc.selection[ s ].moveToEnd( myLayer );
}


}
moveSpotColorItemsToNewLayer( "CutContour" );

 

 

 

//author: William Dowling
//adobe forum username: Disposition_Dev
//email: illustrator.dev.pro@gmail.com
//linkedin: https://www.linkedin.com/in/william-dowling-4537449a/
//Adobe Discussion Forum Post about this function: https://community.adobe.com/t5/illustrator-discussions/moving-objects-with-spot-color-to-a-new-layer...

//*******//

//Did you find this useful? Would you like to buy me a cup of coffee to say thanks?
//paypal.me/illustratordev
//<3

//*******//

 

//Move Spot Color Items to New Layer
//
// pass the name of the spot color as a string
// and this script will move all objects with that spot color to a new layer

//arguments
//
//mySpotColorName : string. The name of the spot color you want to move to a new layer


//Dependencies
//
//none

function moveSpotColorItemsToNewLayer ( mySpotColorName )
{
var doc = app.activeDocument;
var mySpotSwatch;
for ( var i = 0; i < doc.swatches.length && !mySpotSwatch; i++ )
{
if ( doc.swatches[ i ].name.match( mySpotColorName ) )
{
mySpotSwatch = doc.swatches[ i ];
}
}
if ( !mySpotSwatch )
{
alert( "Spot color \"" + mySpotColorName + "\" not found" );
return;
}

var myLayer = doc.layers.add();
myLayer.name = mySpotColorName;

doc.selection = null;
doc.defaultFillColor = mySpotSwatch.color;

//get filled items matching the spot color
app.executeMenuCommand( "Find Fill Color menu item" );
for ( var s = 0; s < doc.selection.length; s++ )
{
doc.selection[ s ].moveToBeginning( myLayer );
}

doc.selection = null;
doc.defaultFillColor = mySpotSwatch.color;
app.executeMenuCommand( "Find Fill Color menu item" );
doc.defaultStrokeColor = mySpotSwatch.color;
app.executeMenuCommand( "Find Stroke Color menu item" );
for ( var s = 0; s < doc.selection.length; s++ ) {
doc.selection[ s ].moveToEnd( myLayer );
}


}
moveSpotColorItemsToNewLayer( "Pnimi" );

 

 

 

 

 

 

 

//author: William Dowling
//adobe forum username: Disposition_Dev
//email: illustrator.dev.pro@gmail.com
//linkedin: https://www.linkedin.com/in/william-dowling-4537449a/
//Adobe Discussion Forum Post about this function: https://community.adobe.com/t5/illustrator-discussions/moving-objects-with-spot-color-to-a-new-layer...

//*******//

//Did you find this useful? Would you like to buy me a cup of coffee to say thanks?
//paypal.me/illustratordev
//<3

//*******//

 

//Move Spot Color Items to New Layer
//
// pass the name of the spot color as a string
// and this script will move all objects with that spot color to a new layer

//arguments
//
//mySpotColorName : string. The name of the spot color you want to move to a new layer


//Dependencies
//
//none

function moveSpotColorItemsToNewLayer ( mySpotColorName )
{
var doc = app.activeDocument;
var mySpotSwatch;
for ( var i = 0; i < doc.swatches.length && !mySpotSwatch; i++ )
{
if ( doc.swatches[ i ].name.match( mySpotColorName ) )
{
mySpotSwatch = doc.swatches[ i ];
}
}
if ( !mySpotSwatch )
{
alert( "Spot color \"" + mySpotColorName + "\" not found" );
return;
}

var myLayer = doc.layers.add();
myLayer.name = mySpotColorName;

doc.selection = null;
doc.defaultFillColor = mySpotSwatch.color;

//get filled items matching the spot color
app.executeMenuCommand( "Find Fill Color menu item" );
for ( var s = 0; s < doc.selection.length; s++ )
{
doc.selection[ s ].moveToBeginning( myLayer );
}

doc.selection = null;
doc.defaultFillColor = mySpotSwatch.color;
app.executeMenuCommand( "Find Fill Color menu item" );
doc.defaultStrokeColor = mySpotSwatch.color;
app.executeMenuCommand( "Find Stroke Color menu item" );
for ( var s = 0; s < doc.selection.length; s++ ) {
doc.selection[ s ].moveToEnd( myLayer );
}


}
moveSpotColorItemsToNewLayer( "Big" );

 

 

 

 

 

Votes

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
community guidelines