Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

Unable to delete Comp from listbox with Extendscript

Explorer ,
Apr 01, 2020 Apr 01, 2020

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

Test.jpg

TOPICS
How to , Scripting
1.6K
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Apr 01, 2020 Apr 01, 2020

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();
        }
    }
})();
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Apr 02, 2020 Apr 02, 2020

Yea ,,, it is working --- Thanks for your time and effort .

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Apr 02, 2020 Apr 02, 2020

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 >>>

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Apr 02, 2020 Apr 02, 2020

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.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Apr 02, 2020 Apr 02, 2020

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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Apr 02, 2020 Apr 02, 2020

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.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Apr 03, 2020 Apr 03, 2020

Any way, the above code needs to be corrected to remove multiple comp but it not working ... I hope you can help me here 

Untitled-1.jpg

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Apr 03, 2020 Apr 03, 2020

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.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Apr 03, 2020 Apr 03, 2020
LATEST

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  😉

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines