Skip to main content
Inspiring
May 4, 2021
Answered

A script to move specific stroke color to Layer

  • May 4, 2021
  • 1 reply
  • 1594 views

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

This topic has been closed for replies.
Correct answer Charu Rajput

Charu,

 

Please find attached an example as well as the modified scripts

Change ext to zip


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.

 

1 reply

Charu Rajput
Community Expert
Community Expert
May 4, 2021

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
rcraighead
Legend
May 4, 2021

@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?

Charu Rajput
Community Expert
Community Expert
May 5, 2021

@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