Copy link to clipboard
Copied
Hi
I am working on a script to ( find CompItems, then delete them all with listbox ) but the problem is that, I don't now how to delete the selected comps by listbox from my project ? Please Help :-((
var win = new Window("palette");
win.text = "Link";
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();
button1.onClick = function Search () {
var compsArray = new Array();
var myProj = app.project;
for (var i=1; i <= myProj.numItems; i++) {
if (myProj.item(i) instanceof CompItem) {
var myNewArray = compsArray[compsArray.length] = myProj.item(i);
listbox1.add("item", myNewArray.name);
}
}
}
button2.onClick = function deletecomps () {
if (listbox1.selection.text = myNewArray.name) {
myNewArray.remove();
}
}Copy link to clipboard
Copied
myNewArray isn't in scope of deletecomps(), you need to move it in the global scope. And while you're at it, always a good idea to wrap your scripts in an IIFE to avoid polluting the global namespace.
(function(){
var win = new Window("palette");
win.text = "Link";
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;
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() {
if (listbox1.selection.text = myNewArray.name) {
myNewArray.remove();
}
}
})();
Copy link to clipboard
Copied
Yea ,,, it is working --- Thanks for your time and effort .
Copy link to clipboard
Copied
justintaylor fix the scope of deletecomps but there 3 problems, first it doesn't delete the selected item from the list but delete the last one in the array ( myNewArray.remove(); ) . Second problem is multiselection for delete is not working, althought it work in the listbox and finally it work or delete only onetime ..... Please help ... I search and experiment alot and I find nothing >>>
Copy link to clipboard
Copied
Well, for starters you need to overwrite your array and list each time you refresh like this:
myNewArray = [];
listbox1.removeAll();
which would make your script like this:
(function(){
$.win = new Window("palette");
var win = $.win;
win.text = "Link";
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() {
alert(listbox1.selection)
if (listbox1.selection.text = myNewArray.name) {
myNewArray.remove();
}
}
})();
A few other things to watch out for. You're only refreshing your list with the refresh button, which means it might get out of sync with your comps, so I'd recommend updating when you launch the script and after each delete operation.
Also, you're selecting comps by name, which can run into issues since comps can share names. Storing and deleting by id is a more stable approach.
Copy link to clipboard
Copied
While you respond I solve 2 problems but still one is multiselection ....
(function(){
var win = new Window("palette");
win.text = "Link";
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;
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 ();
}
})();
I will do it according to ID but I need to solve removing according to multiselect items
Copy link to clipboard
Copied
listbox1.selection is an array, but you're treating it like a string. Store the ID in your array, and then loop through your selection and get each comp by index and delete. Clear your array and listbox when done.
Copy link to clipboard
Copied
Any way, the above code needs to be corrected to remove multiple comp but it not working ... I hope you can help me here
Copy link to clipboard
Copied
Single = means you're overwriting, which you never want to use in an if statement. What you want is double == or triple === to compare values.
Copy link to clipboard
Copied
It is not working also, But I learn alot and I am very greatfull for all professional who helped here. I will figure another way to code my script 😉
Find more inspiration, events, and resources on the new Adobe Community
Explore Now