Skip to main content
Participating Frequently
February 7, 2022
Question

Script to Rename Paths

  • February 7, 2022
  • 2 replies
  • 1376 views

I am attempting to select a number of paths that have not been named and batch name them with a single name for example sake "<name>" without the quotes but with the brackets.  I have been trying code from these two forums with little success.

https://community.adobe.com/t5/illustrator-discussions/help-with-script-to-rename-selected-paths-not-layers/m-p/11952033#M271201

https://community.adobe.com/t5/illustrator-discussions/how-do-i-batch-rename-objects-paths-not-just-layers/m-p/3858433

any help would be appreciated!

 

var doc = activeDocument;
var sel = doc.selection;
var suffix="<name>";

for (var i=0; i < sel.length; i++)
sel[i].name = (sel[i].name == "") ? sel[i].typename+suffix : sel[i].name+suffix;

2 replies

New Participant
April 20, 2025

How to Use:

Copy the script code from the artifact
In Illustrator, go to File > Scripts > Other Script...
Paste the code into a new .jsx file and save it
Select your "rock facade group" in Illustrator
Run the script (File > Scripts > [Your saved script])
Adjust the naming options in the dialog box that appears
Click OK to apply the renaming

 

// Illustrator Path Batch Renaming Script
// This script allows you to batch rename paths within a selected group

// Check if a document is open
if (app.documents.length > 0) {
    var doc = app.activeDocument;
    
    // Check if there's a selection
    if (doc.selection.length > 0) {
        // Create dialog window
        var dialog = new Window("dialog", "Batch Rename Paths");
        dialog.orientation = "column";
        dialog.alignChildren = ["left", "top"];
        
        // Group for base name input
        var baseNameGroup = dialog.add("group");
        baseNameGroup.orientation = "row";
        baseNameGroup.add("statictext", undefined, "Base Name:");
        var baseNameInput = baseNameGroup.add("edittext", undefined, "Rock_");
        baseNameInput.characters = 20;
        
        // Group for starting number
        var startNumGroup = dialog.add("group");
        startNumGroup.orientation = "row";
        startNumGroup.add("statictext", undefined, "Start Number:");
        var startNumInput = startNumGroup.add("edittext", undefined, "1");
        startNumInput.characters = 5;
        
        // Group for number padding
        var paddingGroup = dialog.add("group");
        paddingGroup.orientation = "row";
        paddingGroup.add("statictext", undefined, "Number Padding:");
        var paddingInput = paddingGroup.add("edittext", undefined, "2");
        paddingInput.characters = 5;
        
        // Group for sorting options
        var sortGroup = dialog.add("group");
        sortGroup.orientation = "row";
        sortGroup.add("statictext", undefined, "Sort By:");
        var sortDropdown = sortGroup.add("dropdownlist", undefined, ["Position (Top to Bottom)", "Position (Left to Right)", "Layer Order", "Current Order"]);
        sortDropdown.selection = 0;
        
        // Group for buttons
        var buttonGroup = dialog.add("group");
        buttonGroup.orientation = "row";
        var cancelButton = buttonGroup.add("button", undefined, "Cancel", {name:"cancel"});
        var okButton = buttonGroup.add("button", undefined, "OK", {name:"ok"});
        
        // Preview area
        dialog.add("statictext", undefined, "Preview:");
        var previewText = dialog.add("edittext", [0, 0, 300, 100], "", {multiline: true, readonly: true});
        
        // Function to update preview
        function updatePreview() {
            var baseName = baseNameInput.text;
            var startNum = parseInt(startNumInput.text, 10);
            var padding = parseInt(paddingInput.text, 10);
            
            var previewContent = "";
            for (var i = 0; i < Math.min(5, doc.selection.length); i++) {
                var num = startNum + i;
                var paddedNum = padNumber(num, padding);
                previewContent += baseName + paddedNum + "\n";
            }
            
            if (doc.selection.length > 5) {
                previewContent += "...\n";
                var lastNum = startNum + doc.selection.length - 1;
                var lastPaddedNum = padNumber(lastNum, padding);
                previewContent += baseName + lastPaddedNum;
            }
            
            previewText.text = previewContent;
        }
        
        // Add change listeners to update preview
        baseNameInput.onChanging = updatePreview;
        startNumInput.onChanging = updatePreview;
        paddingInput.onChanging = updatePreview;
        
        // Initialize preview
        updatePreview();
        
        // Function to pad number with leading zeros
        function padNumber(num, padding) {
            var str = num.toString();
            while (str.length < padding) {
                str = "0" + str;
            }
            return str;
        }
        
        // Function to sort items by vertical position (top to bottom)
        function sortByVerticalPosition(items) {
            var sortedItems = [];
            for (var i = 0; i < items.length; i++) {
                sortedItems.push(items[i]);
            }
            
            sortedItems.sort(function(a, b) {
                // Get bounds for comparison
                var boundsA = a.geometricBounds;
                var boundsB = b.geometricBounds;
                
                // Compare top positions (Y coordinate)
                // Note: In Illustrator, Y increases downward
                var topA = boundsA[1]; // Y-coordinate of top edge
                var topB = boundsB[1];
                
                return topB - topA; // Sort top to bottom
            });
            
            return sortedItems;
        }
        
        // Function to sort items by horizontal position (left to right)
        function sortByHorizontalPosition(items) {
            var sortedItems = [];
            for (var i = 0; i < items.length; i++) {
                sortedItems.push(items[i]);
            }
            
            sortedItems.sort(function(a, b) {
                // Get bounds for comparison
                var boundsA = a.geometricBounds;
                var boundsB = b.geometricBounds;
                
                // Compare left positions (X coordinate)
                var leftA = boundsA[0]; // X-coordinate of left edge
                var leftB = boundsB[0];
                
                return leftA - leftB; // Sort left to right
            });
            
            return sortedItems;
        }
        
        // OK button click handler
        okButton.onClick = function() {
            var baseName = baseNameInput.text;
            var startNum = parseInt(startNumInput.text, 10);
            var padding = parseInt(paddingInput.text, 10);
            var selection = doc.selection;
            var paths = [];
            
            // Get all paths from selection
            // If selected item is a group, get all paths from the group
            for (var i = 0; i < selection.length; i++) {
                if (selection[i].typename === "GroupItem") {
                    collectPathsFromGroup(selection[i], paths);
                } else if (selection[i].typename === "PathItem") {
                    paths.push(selection[i]);
                }
            }
            
            // Sort paths based on selected option
            var sortedPaths = paths;
            switch (sortDropdown.selection.index) {
                case 0: // Top to Bottom
                    sortedPaths = sortByVerticalPosition(paths);
                    break;
                case 1: // Left to Right
                    sortedPaths = sortByHorizontalPosition(paths);
                    break;
                case 2: // Layer Order
                    // Paths are already in layer order
                    break;
                case 3: // Current Order
                    // Use current order
                    break;
            }
            
            // Rename paths
            for (var i = 0; i < sortedPaths.length; i++) {
                var num = startNum + i;
                var paddedNum = padNumber(num, padding);
                sortedPaths[i].name = baseName + paddedNum;
            }
            
            alert("Renamed " + sortedPaths.length + " paths successfully!");
            dialog.close();
        };
        
        // Function to collect all paths from a group recursively
        function collectPathsFromGroup(group, pathsArray) {
            for (var i = 0; i < group.pageItems.length; i++) {
                var item = group.pageItems[i];
                if (item.typename === "PathItem") {
                    pathsArray.push(item);
                } else if (item.typename === "GroupItem") {
                    collectPathsFromGroup(item, pathsArray);
                }
            }
        }
        
        // Show dialog
        dialog.show();
    } else {
        alert("Please select a group or paths to rename.");
    }
} else {
    alert("Please open a document first.");
}
m1b
Brainiac
February 7, 2022

What result are you getting? I tried your code and it worked as expected.

- Mark

 

 

Celery72Author
Participating Frequently
February 7, 2022

Huh, It is renaming my layers, not my paths.

m1b
Brainiac
February 7, 2022

Yes, they are in a group!  That would be where the error is stemming from.  Is it possible to batch rename these inside a group?


Yes you can look inside groups, but it is more difficult to understand because it utilises recursion, which means a function that calls itself. Here's code to do it in your case:

 

 

 

var doc = activeDocument;
var sel = doc.selection;
var suffix = "<name>";

renameItems(sel, suffix);

function renameItems(items, suffix) {
    for (var i = 0; i < items.length; i++) {
        var item = items[i];
        if (item.typename == 'GroupItem') {
            renameItems(item.pageItems, suffix);
        } else {
            item.name = item.name + suffix;
        }
    }
}

 

 

Edit: typos.