A script to move specific stroke color to Layer

Explorer ,
May 03, 2021 May 03, 2021

Copy link to clipboard

Copied

Dear fellows,

 

Can you please help me with the script that moves objects with a specified stroke color to the specified layer?

For example objects with a stroke color r255g0b0 to the layer "Cut"

 

With the best regards

TOPICS
Draw and design, Print and publish, Scripting, Tools

Views

682

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

correct answers 1 Correct Answer

Adobe Community Professional , May 05, 2021 May 05, 2021
Hi @syntanjey  The above script that I have shared only works with the path items. Here is another version of the script which will be much faster and a better approach 1. Case 1: When you don't want to group all items that are move to the new layer. function main() { var doc = app.activeDocument; var R = 255; var G = 128; var B = 0 var layerName = "+Doors"; try { var _layer = doc.layers[layerName]; } catch (e) { alert("No layer exists with name " ...

Likes

Translate

Translate
Adobe Community Professional ,
May 03, 2021 May 03, 2021

Copy link to clipboard

Copied

Hi,

Try following snippet

 

function main() {
  var doc = app.activeDocument;
  var layerName = "Cut";
  try {
    var _layer = doc.layers[layerName];
  } catch (e) {
    alert("No layer exists with name " + layerName);
  }
  app.executeMenuCommand("deselectall");
  var _pageItems = doc.pageItems;
  for (var i = 0; i < _pageItems.length; i++) {
    if (
      _pageItems[i].stroked &&
      _pageItems[i].strokeColor.red == 255 &&
      _pageItems[i].strokeColor.blue == 0 &&
      _pageItems[i].strokeColor.green == 0
    ) {
      _pageItems.selected = true;
      break;
    }
  }
  app.executeMenuCommand("Find Stroke Color menu item");
  var _allItems = app.selection;
  for (var i = 0; i < _allItems.length; i++) {
    _allItems[i].move(_layer, ElementPlacement.PLACEATEND);
  }
}

main();

 

 

 

Best regards

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
community guidelines
Adobe Community Professional ,
May 04, 2021 May 04, 2021

Copy link to clipboard

Copied

@Charu Rajput , Thanks for this lesson, Charu.

Question: Why did you choose to move each item to the named layer separately? Would it be any faster to group the selection and move the group to the named layer?

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
community guidelines
Adobe Community Professional ,
May 04, 2021 May 04, 2021

Copy link to clipboard

Copied

@rcraighead 

Yes grouping all together and moving that group will be much faster. This will save looping. I mean if these is no need of grouping an item then we can't group but yes the way you said will be faster.

I did not do that because I did not think of at that momenet and even not asked in the question 🙂 😛

 

 

Best regards

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
community guidelines
Explorer ,
May 04, 2021 May 04, 2021

Copy link to clipboard

Copied

Hello Charu!

 

How i can define not only a one color, but a list of colours to move to the layer?

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
community guidelines
Explorer ,
May 04, 2021 May 04, 2021

Copy link to clipboard

Copied

Actually it do not work properly

It works one time

When you try to run next script for the second color, script starts to select random items,

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
community guidelines
Adobe Community Professional ,
May 04, 2021 May 04, 2021

Copy link to clipboard

Copied

@syntanjey 

Did you change the value of the color (R, G, B)while executing for the second color. 

Could you please show some relevant examples so that script wil be modified as per you requirements.

It will be better if you attach some sample files.

Best regards

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
community guidelines
Explorer ,
May 05, 2021 May 05, 2021

Copy link to clipboard

Copied

Charu,

 

Please find attached an example as well as the modified scripts

Change ext to zip

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
community guidelines
Adobe Community Professional ,
May 05, 2021 May 05, 2021

Copy link to clipboard

Copied

LATEST

Hi @syntanjey 

The above script that I have shared only works with the path items. Here is another version of the script which will be much faster and a better approach

 

1. Case 1: When you don't want to group all items that are move to the new layer.

function main() {

    var doc = app.activeDocument;
    var R = 255;
    var G = 128;
    var B = 0
    var layerName = "+Doors";

    try {
        var _layer = doc.layers[layerName];
    } catch (e) {
        alert("No layer exists with name " + layerName);
    }

    app.executeMenuCommand("deselectall");

    //Create temp pathitem to find out the all items with same stroke color
    var _tempItem = doc.pathItems.add();
    _tempItem.stroked = true;
    _tempItem.strokeColor.red = R;
    _tempItem.strokeColor.green = G;
    _tempItem.strokeColor.blue = B;
    _tempItem.selected = true;

    app.executeMenuCommand("Find Stroke Color menu item");

    var _allItems = app.selection;
    for (var i = 0; i < _allItems.length; i++) {
        _allItems[i].move(_layer, ElementPlacement.PLACEATEND);
    }

    _tempItem.remove();
}

main();

 

Case 2: If it is ok to have all items to be grouped.

function main() {

    var doc = app.activeDocument;
    var R = 255;
    var G = 128;
    var B = 0
    var layerName = "+Doors";

    try {
        var _layer = doc.layers[layerName];
    } catch (e) {
        alert("No layer exists with name " + layerName);
    }

    app.executeMenuCommand("deselectall");

    //Create temp pathitem to find out the all items with same stroke color
    var _tempItem = doc.pathItems.add();
    _tempItem.stroked = true;
    _tempItem.strokeColor.red = R;
    _tempItem.strokeColor.green = G;
    _tempItem.strokeColor.blue = B;
    _tempItem.selected = true;

    app.executeMenuCommand("Find Stroke Color menu item");

    //Use Below code if it is ok to be group
    app.executeMenuCommand("group");
    app.selection[0].move(_layer, ElementPlacement.PLACEATEND);

    _tempItem.remove();
}

main();

 

The approach that I am using in the updated versions of the script is :

 

1. Create a temporary path item with required stroke color

2. Select the item. By this we are not traversing all pageitems and no need to check type of pageitem too.

3. Run the command to select all items with same stroke color

 

4. Group all selected items and then move to the required layer.

OR

4. Move all selected items one by one to the required layer.

(Choose step 4 depending upon requirements)

 

5. After that we can remove the temporray item.

 

I hope this will work in your all scenarios.

 

Best regards

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