Skip to main content
Known Participant
February 16, 2023
Answered

Moving objects with spot color to a new layer javascript

  • February 16, 2023
  • 12 replies
  • 3472 views
Hello
I have objects with a certain spot color, I need to move them to a new layer
)which does not exist.)

 

This topic is closed to new replies. Start a new post to keep the conversation going.
Correct answer femkeblanco

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.


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 );
    }

12 replies

aviel222Author
Known Participant
April 4, 2023

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-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;

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-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;

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-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;

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" );

 

 

 

 

 

Disposition_Dev
Legend
February 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 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" );
aviel222Author
Known Participant
February 16, 2023
Hello
It doesn't work well
I opened a new document with spot color:
mySpotColorName

There is an error message. Situation.
aviel222Author
Known Participant
February 16, 2023
test.pdf