替换图层窗格中的名称不会更新

Replacing name in layers pane doesn't update

我有一个脚本可以搜索图层名称并替换它们,到目前为止它工作正常。但是我有 运行 个问题,我不明白发生了什么。除了图层之外,我还更改了脚本以包含某些对象(pathItems 等)。但是,当它找到并替换对象名称(它确实找到了它们)时,它不会在图层窗格中更新。我调试了该程序,它确实找到并替换了它,但没有更新 Adob​​e Illustrator 图层窗格中的名称。

#target illustrator
#targetengine main

// JavaScript Document
if (app.documents.length > 0) {
    var doc = app.activeDocument;
    var docLayers = doc.layers;
    var searchText = "";
    var replaceText = "";
    var found = false;
    var match = false;

    function replace(layers) {
        match = false;
        recurseLayers(layers);
        app.redraw();

        // When match is found, show dialog
        if (match) {
            alert("Found match!");

            match = false;
        } else {
            alert("No match found.");
            match = false;
        }
    }


    function recurseLayers(layers) {
        var length = layers.length;
        var currentLayer = null;
        var searchtext = searchText;
        var replacetext = replaceText;


        try {
            for (var i = length; i--;) {
                currentLayer = layers[i];
                var visible = checkLayerVisibility(currentLayer);
                var locked = checkLayerLocked(currentLayer);

                if (visible == true || locked == false) {
                    replaceName(currentLayer, searchText, replaceText);

                    if (currentLayer.layers) {
                        recurseLayers(currentLayer.layers);
                        recurseLayers(currentLayer.groupItems);
                        recurseLayers(currentLayer.pathItems);
                        recurseLayers(currentLayer.compoundPathItems);
                        recurseLayers(currentLayer.symbolItems);
                        recurseLayers(currentLayer.textFrames);
                    }
                }
            }
        } catch (e) {
            logger (e);
        }
    }

    function replaceName(currentLayer, searchText, replaceText) {
        try {
            currentLayer.name = currentLayer.name.replace(searchText, replaceText);
            match = true;

        } catch (e) {
            logger(e);
        }
    }

    function checkLayerLocked(layer) {
        if(!layer.locked) 
            for(var parent = layer.parent; parent.typename=='Layer'; parent = parent.parent) {
                 if(parent.locked)
                    return true;
             }
        return layer.locked;
    } 

    function checkLayerVisibility(layer) {
        if(layer.visible) 
        for(var parent = layer.parent; parent.typename=='Layer'; parent = parent.parent) {
             if(!parent.visible)
                return false;
         }
        return layer.visible;
     }

    startGUI();

    function startGUI() {

        var win = new Window("dialog", "Replace Layer name", undefined);

        win.orientation = "column";
        win.alignChildren = ["fill", "fill"];

        // Search
        var searchGrp = win.add("panel", undefined, "Search and Replace");
        searchGrp.orientation = "column";
        searchGrp.alignChildren = ["fill", "fill"];

        var titleMsgS = searchGrp.add("statictext", undefined, "Layer name to search:");
        var txt_searchText = searchGrp.add("edittext { characters: 1, justify: 'center', active: true }");

        txt_searchText.helpTip = "Input layer name to replace";

        var titleMsgR = searchGrp.add("statictext", undefined, "Layer name to replace with:");
        var txt_replaceText = searchGrp.add("edittext { characters: 1, justify: 'center', active: true }");
        txt_replaceText.helpTip = "Input layer name to replace with";

        // Set first text box to active
        txt_searchText.active = true;

        win.addEventListener ("keydown", function(kd) {enter(kd) });

        // Replace button
        var replaceBtn = searchGrp.add("button", undefined, "Replace");
        replaceBtn.helpTip = "Replace layer name";
        replaceBtn.onClick = function() {
            searchText = txt_searchText.text;
            replaceText = txt_replaceText.text;

            replace(docLayers);
            app.redraw();
        }

        function enter(k) {
            if (k.keyName == "Enter") {
                searchText = txt_searchText.text;
                replaceText = txt_replaceText.text;

                replace(docLayers);
                app.redraw();
            }
        }

        // Close button
        var quitBtn = win.add("button", undefined, "Close");
        quitBtn.helpTip = "Press Esc to Close";

        // Event listener for the quit button
        quitBtn.onClick = function() {   
            win.close();   
        }  

        // Centering & Show Window
        win.center();
        win.show(); 
    }


    // Prints stack trace
    function logger(e) {
        var errorMsg = "";

        errorMsg = errorMsg.concat("An error has occured:\n", e.line, "\n", e.message, "\n", e.stack);
        $.writeln(errorMsg);
    }

} else {
        alert("You do not have any document opened!");
}

这些是我正在使用的图层,线条是我需要替换的,只是它们的显示名称(全名或部分名称)。

从第二张图可以看到,名字确实改了(把'A8900'改成'1'),但是只有在双击名字,或者隐藏和取消隐藏的时候才会出现层(然后更新)。我不明白为什么会这样,也许与对象在 AI 中的工作方式有关?似乎也没有任何方法可以调试它,因为在我的代码中它工作正常,但它在 AI 中没有正确更新。我还使用了 'app.redraw()' 希望它会刷新它,但它没有。感谢任何帮助

我设法让它工作,我向图层 select 添加了一行代码,它更新了图层面板中的名称。

如果有人知道为什么会这样,我还是很想知道。

#target illustrator
#targetengine main

// JavaScript Document
if (app.documents.length > 0) {
    var doc = app.activeDocument;
    var docLayers = doc.layers;
    var searchText = "";
    var replaceText = "";
    var found = false;
    var match = false;

    function replace(layers) {
        match = false;
        recurseLayers(layers);

        // When match is found, show dialog
        if (match) {
            alert("Found match!");

            match = false;
        } else {
            alert("No match found.");
            match = false;
        }
    }


    function recurseLayers(layers) {
        var length = layers.length;
        var currentLayer = null;
        var searchtext = searchText;
        var replacetext = replaceText;


        try {
            for (var i = length; i--;) {
                currentLayer = layers[i];
                var visible = checkLayerVisibility(currentLayer);
                var locked = checkLayerLocked(currentLayer);

                if (visible == true || locked == false) {
                    replaceName(currentLayer, searchText, replaceText);

                    if (currentLayer.layers) {
                        recurseLayers(currentLayer.layers);
                        recurseLayers(currentLayer.groupItems);
                        recurseLayers(currentLayer.pathItems);
                        recurseLayers(currentLayer.compoundPathItems);
                        recurseLayers(currentLayer.symbolItems);
                        recurseLayers(currentLayer.textFrames);
                    }
                }
            }
        } catch (e) {
            logger (e);
        }
    }

    function replaceName(currentLayer, searchText, replaceText) {
        try {
            var name = currentLayer.name;
            var searchIndex = name.toLowerCase().indexOf(searchText.toLowerCase());

            if( searchIndex != -1 ) {

                currentLayer.name = currentLayer.name.replace(searchText, replaceText);
                currentLayer.selected = true;
                match = true;
            }
        } catch (e) {
            logger(e);
        }
    }

    function checkLayerLocked(layer) {
        if(!layer.locked) 
            for(var parent = layer.parent; parent.typename=='Layer'; parent = parent.parent) {
                 if(parent.locked)
                    return true;
             }
        return layer.locked;
    } 

    function checkLayerVisibility(layer) {
        if(layer.visible) 
        for(var parent = layer.parent; parent.typename=='Layer'; parent = parent.parent) {
             if(!parent.visible)
                return false;
         }
        return layer.visible;
     }

    startGUI();

    function startGUI() {

        var win = new Window("dialog", "Replace Layer name", undefined);

        win.orientation = "column";
        win.alignChildren = ["fill", "fill"];

        // Search
        var searchGrp = win.add("panel", undefined, "Search and Replace");
        searchGrp.orientation = "column";
        searchGrp.alignChildren = ["fill", "fill"];

        var titleMsgS = searchGrp.add("statictext", undefined, "Layer name to search:");
        var txt_searchText = searchGrp.add("edittext { characters: 1, justify: 'center', active: true }");

        txt_searchText.helpTip = "Input layer name to replace";

        var titleMsgR = searchGrp.add("statictext", undefined, "Layer name to replace with:");
        var txt_replaceText = searchGrp.add("edittext { characters: 1, justify: 'center', active: true }");
        txt_replaceText.helpTip = "Input layer name to replace with";

        // Set first text box to active
        txt_searchText.active = true;

        win.addEventListener ("keydown", function(kd) {enter(kd) });

        // Replace button
        var replaceBtn = searchGrp.add("button", undefined, "Replace");
        replaceBtn.helpTip = "Replace layer name";
        replaceBtn.onClick = function() {
            searchText = txt_searchText.text;
            replaceText = txt_replaceText.text;

            replace(docLayers);
            app.redraw();
        }

        function enter(k) {
            if (k.keyName == "Enter") {
                searchText = txt_searchText.text;
                replaceText = txt_replaceText.text;

                replace(docLayers);
                app.redraw();
            }
        }

        // Close button
        var quitBtn = win.add("button", undefined, "Close");
        quitBtn.helpTip = "Press Esc to Close";

        // Event listener for the quit button
        quitBtn.onClick = function() {   
            win.close();   
        }  

        // Centering & Show Window
        win.center();
        win.show(); 
    }


    // Prints stack trace
    function logger(e) {
        var errorMsg = "";

        errorMsg = errorMsg.concat("An error has occured:\n", e.line, "\n", e.message, "\n", e.stack);
        //$.writeln(errorMsg);
    }

} else {
        alert("You do not have any document opened!");
}