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

时间:2016-09-09 16:52:26

标签: javascript adobe-illustrator extendscript

我有一个javascript,它遍历Adobe Illustrator中的图层列表,并根据用户的输入更改名称。

有2个'edittext'文本框用于输入,第一个是您要搜索的名称,第二个是您想要替换它的名称。该脚本有效,但是我希望在启动脚本时将第一个文本框聚焦,这样用户就可以在第一个文本框中启动,并可以选择第二个文本框。

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

我在Adobe Illustrator CS6中使用Adobe ExtendScript TK。

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

1 个答案:

答案 0 :(得分:1)

这应该让你去。编辑文本具有属性active。如果设置为true,则它具有焦点。关键是通过Eventlistener完成的。

但是:

  

- 严格来说不是ScriptUI问题:在After Effects和Illustrator中,不能再使用eventlisteners:.addEventListener()不起作用。
  来自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