1

我有一个 javascript,它通过 Adob​​e Illustrator 中的图层列表并根据用户的输入更改名称。

有 2 个用于输入的“edittext”文本框,第一个是您要搜索的名称,第二个是您要替换的名称。该脚本有效,但是我希望在启动脚本时聚焦第一个文本框,因此用户从第一个文本框开始并可以切换到第二个文本框。

此外,我希望在按下“输入”按钮时运行我的提交按钮,以使其更快。

我将 Adob​​e ExtendScript TK 与 Adob​​e Illustrator CS6 一起使用。

#target illustrator
#targetengine main

// JavaScript Document
function renameText(searchText, replaceText) {
     if (app.documents.length == 0) return;
     var docRef = app.activeDocument;
     recurseLayers(searchText, replaceText, docRef.layers);
}


function recurseLayers(searchText, replaceText, objArray) {
     try {
         $.writeln("Layer length: " + objArray.length);
         for (var i = 0; i < objArray.length; i++) {
             if (objArray[i].visible == true && objArray[i].locked == false) {

                 //var searchtext = "/\s*" + searchText + "\s*\d*/";
                 objArray[i].name = objArray[i].name.replace(searchText, replaceText);

                  if (objArray[i].layers.length > 0) {
                      recurseLayers(searchText, replaceText, objArray[i].layers);
                  }
            }
          }
      } catch (e) {
          logger(e);
     }
}


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 searchText = searchGrp.add("edittext { characters: 1, justify: 'center', active: true }");
        searchTest.setFocus();

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

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

        // Replace button
        var replaceBtn = searchGrp.add("button", undefined, "Replace");
        replaceBtn.helpTip = "Replace layer name";
        replaceBtn.onClick = function() {
                renameText(searchText.text, replaceText.text);
                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);
    }
4

1 回答 1

2

这应该让你继续前进。编辑文本有一个属性active。如果设置为 true,则它具有焦点。密钥是通过Eventlistener.

但:

– 严格来说不是 ScriptUI 问题:在 After Effects 和 Illustrator 中,无法再使用事件监听器:.addEventListener() 不起作用。
来自ScriptUI for dummy || kahrel.plus.com/indesign/scriptui.h​​tml

var win = new Window ("dialog");
var etext1 = win.add ("edittext", undefined, "foo");
var etext2 = win.add ("edittext", undefined, "bah");
win.add ("button", undefined, "OK");
win.add ("button", undefined, "Cancel");
etext1.active = true;
win.addEventListener ("keydown", function (kd) {pressed (kd)});
function pressed (k) {
if(k.keyName === "Enter"){
    $.writeln("You pressed " + k.keyName);
  }
}
win.show ( );

在 macOS 10.11.6 上的 ESTK 4.0.0.1 中测试

于 2016-09-12T07:01:42.650 回答