无法使用 Extendscript 从列表框中删除 Multiple Comp
Unable to delete Mutiple Comp from listbox with Extendscript
尽管我将列表框设置为多选,但我无法从我的列表框中执行多项删除,为什么??
我需要你的帮助 。你可以在那里看到完整的脚本。
(function(){
$.win = new Window("palette");
var win = $.win;
win.orientation = "column";
win.alignChildren = ["center", "top"];
win.spacing = 10;
win.margins = 16;
var listbox1 = win.add("listbox", undefined, undefined, { name: "listbox1", multiselect: true, columnTitles: "Max", showHeaders: true });
listbox1.preferredSize.width = 136;
listbox1.preferredSize.height = 208;
var button1 = win.add("button", undefined, undefined, { name: "button1" });
button1.text = "Search";
var button2 = win.add("button", undefined, undefined, { name: "button2" });
button2.text = "Delete";
win.show();
var myNewArray = [];
button1.onClick = function Search() {
var compsArray = new Array();
var myProj = app.project;
myNewArray = [];
listbox1.removeAll();
for (var i = 1; i <= myProj.numItems; i++) {
if (myProj.item(i) instanceof CompItem) {
myNewArray = compsArray[compsArray.length] = myProj.item(i);
listbox1.add("item", myNewArray.name);
}
}
}
button2.onClick = function deletecomps() {
for (var s = 1; s <= app.project.numItems; s ++) {
if ((app.project.item(s) instanceof CompItem) && (app.project.item(s).name.match(listbox1.selection))) {
myComp = app.project.item(s);
break;
}
}
app.project.item(s).remove ();
}
})();
You can see an image to clarify the script in AE
你的问题是第 34 行 listbox1.selection
if ((app.project.item(s) instanceof CompItem) && (app.project.item(s).name.match(listbox1.selection))) {
是一个数组,您正试图将它与 app.project.item(s).name
返回的字符串匹配,而该字符串永远不会匹配。
另外,你想用这些线达到什么目的
myComp = app.project.item(s);
break;
这是 onClick 函数,但它有效。它遍历选择,并根据与 comp 名称匹配的列表框文本查找匹配的项目项。这很危险,因为相同的公司名称会产生误报。我强烈建议您不要在生产代码中使用这种技术,因为它肯定会给您的用户带来问题。
另外我会把你填充列表的部分变成一个单独的函数,并在你点击删除后调用它,这样列表就会刷新,因为此时列表保持不变,即使在comp 已删除。
button2.onClick = function deletecomps() {
for (var b= 0; b < listbox1.selection.length; b++){
for (var s = 1; s <= app.project.numItems; s ++) {
if ((app.project.item(s) instanceof CompItem) && (app.project.item(s).name.match(listbox1.selection[b].text))) {
app.project.item(s).remove ();
}
}
}
}
尽管我将列表框设置为多选,但我无法从我的列表框中执行多项删除,为什么?? 我需要你的帮助 。你可以在那里看到完整的脚本。
(function(){
$.win = new Window("palette");
var win = $.win;
win.orientation = "column";
win.alignChildren = ["center", "top"];
win.spacing = 10;
win.margins = 16;
var listbox1 = win.add("listbox", undefined, undefined, { name: "listbox1", multiselect: true, columnTitles: "Max", showHeaders: true });
listbox1.preferredSize.width = 136;
listbox1.preferredSize.height = 208;
var button1 = win.add("button", undefined, undefined, { name: "button1" });
button1.text = "Search";
var button2 = win.add("button", undefined, undefined, { name: "button2" });
button2.text = "Delete";
win.show();
var myNewArray = [];
button1.onClick = function Search() {
var compsArray = new Array();
var myProj = app.project;
myNewArray = [];
listbox1.removeAll();
for (var i = 1; i <= myProj.numItems; i++) {
if (myProj.item(i) instanceof CompItem) {
myNewArray = compsArray[compsArray.length] = myProj.item(i);
listbox1.add("item", myNewArray.name);
}
}
}
button2.onClick = function deletecomps() {
for (var s = 1; s <= app.project.numItems; s ++) {
if ((app.project.item(s) instanceof CompItem) && (app.project.item(s).name.match(listbox1.selection))) {
myComp = app.project.item(s);
break;
}
}
app.project.item(s).remove ();
}
})();
You can see an image to clarify the script in AE
你的问题是第 34 行 listbox1.selection
if ((app.project.item(s) instanceof CompItem) && (app.project.item(s).name.match(listbox1.selection))) {
是一个数组,您正试图将它与 app.project.item(s).name
返回的字符串匹配,而该字符串永远不会匹配。
另外,你想用这些线达到什么目的
myComp = app.project.item(s);
break;
这是 onClick 函数,但它有效。它遍历选择,并根据与 comp 名称匹配的列表框文本查找匹配的项目项。这很危险,因为相同的公司名称会产生误报。我强烈建议您不要在生产代码中使用这种技术,因为它肯定会给您的用户带来问题。
另外我会把你填充列表的部分变成一个单独的函数,并在你点击删除后调用它,这样列表就会刷新,因为此时列表保持不变,即使在comp 已删除。
button2.onClick = function deletecomps() {
for (var b= 0; b < listbox1.selection.length; b++){
for (var s = 1; s <= app.project.numItems; s ++) {
if ((app.project.item(s) instanceof CompItem) && (app.project.item(s).name.match(listbox1.selection[b].text))) {
app.project.item(s).remove ();
}
}
}
}