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
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 "
...
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();
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?
Copy link to clipboard
Copied
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 🙂 😛
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?
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,
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
Copy link to clipboard
Copied
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.