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

时间:2017-01-13 16:23:08

标签: javascript adobe-illustrator

我有一个搜索图层名称并替换它们的脚本,到目前为止它工作正常。但是我遇到了一个问题,我不明白发生了什么。除了图层之外,我已经将脚本更改为包含某些对象(pathItems等)。但是,当它找到并替换对象名称(它确实找到它们)时,它不会在图层窗格中更新。我调试了程序,它确实找到并替换它,但不会更新Adobe 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!");
}

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

Layers

正如您在第二张图片中看到的那样,名称确实已被更改(将'A8900'更改为'1'),但仅在我双击名称时显示,或隐藏和取消隐藏图层(然后它更新)。我无法弄清楚为什么会发生这种情况,也许与对象如何在AI中工作有关?似乎没有任何方法可以调试它,因为在我的代码中它工作正常,但它没有在AI中正确更新。我还使用'app.redraw()'希望它能刷新它,但事实并非如此。任何帮助表示赞赏

Replaced name

1 个答案:

答案 0 :(得分:0)

我设法让它工作,我添加了一行代码来选择图层,并在图层面板中更新名称。

如果有人知道为什么会这样,我仍然想知道。

#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!");
}