我有一个 javascript,它通过 Adobe Illustrator 中的图层列表并根据用户的输入更改名称。
有 2 个用于输入的“edittext”文本框,第一个是您要搜索的名称,第二个是您要替换的名称。该脚本有效,但是我希望在启动脚本时聚焦第一个文本框,因此用户从第一个文本框开始并可以切换到第二个文本框。
此外,我希望在按下“输入”按钮时运行我的提交按钮,以使其更快。
我将 Adobe ExtendScript TK 与 Adobe 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);
}