Skip to main content
Participant
October 10, 2024
Answered

Script to create new layer called "reg" and move all items with spot colour "VELDOTS" to that layer

  • October 10, 2024
  • 2 replies
  • 412 views

Hi, I am very new to scripting and am unsure how to even start.

Is anyone able to assist with creating a script for the below.

 

Starting with a single layer file

 

I need to select and delete all transparent objects,

rename current layer as "CUT"

create a new layer called "reg" and move all items with spot colour "VELDOTS" to that layer

 

Pretty simple hopefully.

 

Thank you in advance, Brent.

This topic has been closed for replies.
Correct answer Brent330994821lg5

I managed to solve it over the weekend with the assistance of a friend and ChatGPT.

 

var doc = app.activeDocument;

    // Rename the original layer
    var originalLayer = doc.layers[0];
    originalLayer.name = "reg";

var newLayer = app.activeDocument.layers.add();
newLayer.name = "CUT"; // Optional: Assign a name to the new layer

function deleteNoFillNoStrokePaths() {
    if (app.documents.length > 0) {
        var doc = app.activeDocument;
        var noFillNoStrokeItems = [];
        // Function to check for PathItems with no fill and no stroke, excluding CompoundPathItems
        function checkPathItems(item) {
            if (item.typename == "PathItem") {
                // Check if the path item has no fill and no stroke
                if ((item.filled == false || item.fillColor == null) &&
                    (item.stroked == false || item.strokeColor == null)) {
                    noFillNoStrokeItems.push(item);
                }
            } else if (item.typename == "GroupItem") {
                // Recursively check for path items inside groups, but skip compound paths
                for (var j = 0; j < item.pageItems.length; j++) {
                    checkPathItems(item.pageItems[j]);
                }
            }
        }
        // Loop through all layers and items, ignoring locked layers
        for (var i = 0; i < doc.layers.length; i++) {
            var layer = doc.layers[i];
            if (!layer.locked) { // Only process items on unlocked layers
                for (var j = 0; j < layer.pageItems.length; j++) {
                    checkPathItems(layer.pageItems[j]);
                }
            }
        }
        // Deselect everything first
        doc.selection = null;
        // Select the PathItems with no fill and no stroke
        for (var l = 0; l < noFillNoStrokeItems.length; l++) {
            noFillNoStrokeItems[l].selected = true;
        }
        if (noFillNoStrokeItems.length > 0) {
            // Delete selected items
            for (var m = noFillNoStrokeItems.length - 1; m >= 0; m--) {
                noFillNoStrokeItems[m].remove();
            }
            //alert(noFillNoStrokeItems.length + " path objects with no fill and no stroke were deleted.");
        }
    }
}
// Run the function
deleteNoFillNoStrokePaths();

// Define the spot color name to search for
    var targetColorName = "VELCUT";
    // Initialize selection array
    var selectedItems = [];
    // Loop through all path items in the document
    for (var i = 0; i < doc.pathItems.length; i++) {
        var pathItem = doc.pathItems[i];
        // Check if the path item has a stroke
        if (pathItem.stroked) {
            var strokeColor = pathItem.strokeColor;
            // Check if the stroke color is a SpotColor and matches the target color name
            if (strokeColor.typename === "SpotColor" && strokeColor.spot.name === targetColorName) {
                // Add item to selection array
                selectedItems.push(pathItem);
            }
        }
    }
    // Select all items with the target stroke color
    if (selectedItems.length > 0) {
        doc.selection = selectedItems;
    }

    // If there are VELCUT objects, proceed with the copy and paste steps
    if (doc.selection.length > 0) {
        app.cut(); // Cut selected VELCUT objects
    }
        // select the CUT layer
        app.activeDocument.activeLayer = app.activeDocument.layers[0];

        // Paste the copied white objects onto the Base layer
        app.executeMenuCommand("pasteInPlace");

        // Select all paths in the document (only applies to unlocked layers)
        doc.selection = null;
        for (var i = 0; i < doc.pathItems.length; i++) {
            var item = doc.pathItems[i];
            if (!item.layer.locked) {
                item.selected = true;
            }
        }

2 replies

Brent330994821lg5AuthorCorrect answer
Participant
October 14, 2024

I managed to solve it over the weekend with the assistance of a friend and ChatGPT.

 

var doc = app.activeDocument;

    // Rename the original layer
    var originalLayer = doc.layers[0];
    originalLayer.name = "reg";

var newLayer = app.activeDocument.layers.add();
newLayer.name = "CUT"; // Optional: Assign a name to the new layer

function deleteNoFillNoStrokePaths() {
    if (app.documents.length > 0) {
        var doc = app.activeDocument;
        var noFillNoStrokeItems = [];
        // Function to check for PathItems with no fill and no stroke, excluding CompoundPathItems
        function checkPathItems(item) {
            if (item.typename == "PathItem") {
                // Check if the path item has no fill and no stroke
                if ((item.filled == false || item.fillColor == null) &&
                    (item.stroked == false || item.strokeColor == null)) {
                    noFillNoStrokeItems.push(item);
                }
            } else if (item.typename == "GroupItem") {
                // Recursively check for path items inside groups, but skip compound paths
                for (var j = 0; j < item.pageItems.length; j++) {
                    checkPathItems(item.pageItems[j]);
                }
            }
        }
        // Loop through all layers and items, ignoring locked layers
        for (var i = 0; i < doc.layers.length; i++) {
            var layer = doc.layers[i];
            if (!layer.locked) { // Only process items on unlocked layers
                for (var j = 0; j < layer.pageItems.length; j++) {
                    checkPathItems(layer.pageItems[j]);
                }
            }
        }
        // Deselect everything first
        doc.selection = null;
        // Select the PathItems with no fill and no stroke
        for (var l = 0; l < noFillNoStrokeItems.length; l++) {
            noFillNoStrokeItems[l].selected = true;
        }
        if (noFillNoStrokeItems.length > 0) {
            // Delete selected items
            for (var m = noFillNoStrokeItems.length - 1; m >= 0; m--) {
                noFillNoStrokeItems[m].remove();
            }
            //alert(noFillNoStrokeItems.length + " path objects with no fill and no stroke were deleted.");
        }
    }
}
// Run the function
deleteNoFillNoStrokePaths();

// Define the spot color name to search for
    var targetColorName = "VELCUT";
    // Initialize selection array
    var selectedItems = [];
    // Loop through all path items in the document
    for (var i = 0; i < doc.pathItems.length; i++) {
        var pathItem = doc.pathItems[i];
        // Check if the path item has a stroke
        if (pathItem.stroked) {
            var strokeColor = pathItem.strokeColor;
            // Check if the stroke color is a SpotColor and matches the target color name
            if (strokeColor.typename === "SpotColor" && strokeColor.spot.name === targetColorName) {
                // Add item to selection array
                selectedItems.push(pathItem);
            }
        }
    }
    // Select all items with the target stroke color
    if (selectedItems.length > 0) {
        doc.selection = selectedItems;
    }

    // If there are VELCUT objects, proceed with the copy and paste steps
    if (doc.selection.length > 0) {
        app.cut(); // Cut selected VELCUT objects
    }
        // select the CUT layer
        app.activeDocument.activeLayer = app.activeDocument.layers[0];

        // Paste the copied white objects onto the Base layer
        app.executeMenuCommand("pasteInPlace");

        // Select all paths in the document (only applies to unlocked layers)
        doc.selection = null;
        for (var i = 0; i < doc.pathItems.length; i++) {
            var item = doc.pathItems[i];
            if (!item.layer.locked) {
                item.selected = true;
            }
        }
Participating Frequently
October 11, 2024

Hi,

I asked copilot to write the following script.
It doesn't include things like speeding up the selection process or processing when the line color is a spot color, but I was able to get this far in about 10 minutes.

function main (){
    // Remove Transparent Objects
    var doc = app.activeDocument;
    var allPaths = doc.pathItems;
    for (var i = 0; i < allPaths.length; i++) {
        var currentPath = allPaths[i];
        if (currentPath.opacity < 100) {
            currentPath.selected = true;
        }
    }
    var selectedItems = doc.selection;
    for (var i = 0; i < selectedItems.length; i++) {
        selectedItems[i].remove();
    }

    // Change the name of the current layer to "CUT"
    var currentLayer = doc.activeLayer;
    currentLayer.name = "CUT";

    // Add new layer "reg"
    var newLayer = doc.layers.add();
    newLayer.name = "reg";

    // Move objects with "VELDOTS" spot color applied to the "reg" layer
    var regLayer = null;
    for (var i = 0; i < doc.layers.length; i++) {
        if (doc.layers[i].name == "reg") {
            regLayer = doc.layers[i];
            break;
        }
    }

    if (regLayer == null) {
        alert("Layer 'reg' not found.");
        return;
    }

    var allPaths = doc.pathItems;

    var spotColor = null;
    for (var i = 0; i < doc.spots.length; i++) {
        if (doc.spots[i].name == "VELDOTS") {
            spotColor = doc.spots[i];
            break;
        }
    }

    if (spotColor == null) {
        alert("Spot color 'VELDOTS' not found.");
        return;
    }

    for (var i = 0; i < allPaths.length; i++) {
        var currentPath = allPaths[i];
        if (currentPath.filled && currentPath.fillColor.typename == "SpotColor" && currentPath.fillColor.spot == spotColor) {
            currentPath.move(regLayer, ElementPlacement.PLACEATEND);
        }
    }
}

main ();