在 extendscript 中设置编辑文本的焦点

set focus of edit text in extendscript

我有一个 javascript 可以遍历 Adob​​e Illustrator 中的图层列表并根据用户的输入更改名称。

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

另外,我希望在按下 'enter' 按钮时有我的提交按钮 运行,以使其更快。

我正在使用 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);
    }

这应该让你继续。编辑文本有一个 属性 active。如果设置为 true 它有焦点。密钥是通过 Eventlistener 完成的。

但是:

– Not strictly speaking a ScriptUI problem: in After Effects and in Illustrator, eventlisteners can no longer be used: .addEventListener() doesn't work.
from ScriptUI for dummies || kahrel.plus.com/indesign/scriptui.html

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 中测试