2

i have listbox item_1 and item_2 and more but for the sake of it this is enough.

this is my code when listbox item_1 is selected and item string is item_1 then fire the code for that only.

listBox.onDoubleClick = function () {

//alert (listBox.selection);

 if (listBox.selection = "item_1") {

    listBox_pressed()
    }
else

    }

issue am having is executing code even when i click other list items.

full code

// ScriptUI Listboxes
    var counter = 0;

    var window = new Window("palette", "Listbox", undefined);
    window.orientation = "column";
    
    var listBox = window.add("listbox", undefined, []);
    listBox.selection = 0;
    listBox.size = [200, 100];

    var buttonGroup = window.add("group", undefined, "buttonGroup");
    buttonGroup.orientation = "row";
    var addButton = buttonGroup.add("button", undefined, "+");
    addButton.size = [30, 30];
    var minusButton = buttonGroup.add("button", undefined, "-");
    minusButton.size = [30, 30];
    
    addButton.onClick = function () {
            counter++;
            listBox.add("item", "Item_"+counter.toString());
       
        }
     

    minusButton.onClick = function() {
            if(listBox.selection != null) {
            counter--;
            listBox.remove(listBox.selection);
                }
        }

/////////////////////////////////////======================================================================================================
listBox.onDoubleClick = function () {



//alert (listBox.selection);

 if (listBox.selection = "item_1") {

    listBox_pressed()
    }
else

    }
    
    

/////////////////////////////////////======================================================================================================



function listBox_pressed(){ // main function
        myComp = app.project.activeItem;
mySolid = myComp.layers.addSolid([0,0,0], "Solid", myComp.width, myComp.height,1);
        }
    
    window.center();
    window.show();

sir when the script starts when i click button it adds items to list like this
item_1
item_2
item_3 and so on
now based on each item string i want to fire my custom code and not by item index.

so if item_1 is double clicked then fire my custom code use listBox_pressed()

can you just show me one seperate code for listbox item_1 when double clicked my custom code should fire using if statement.

if listbox

so this code ##

if (listBox.selection = "item_1") {

    listBox_pressed()
    }
else

    } 

executes on any item rather than just item_1 only

RobC
  • 22,977
  • 20
  • 73
  • 80
hans
  • 129
  • 7
  • Hi, could you please reword your question description to be more clear on what the problem is and also please provide more code to give better context around its nature. – Ovidijus Parsiunas May 28 '22 at 01:37
  • please check sir. – hans May 28 '22 at 01:47
  • You want to execute ```listBox_pressed()``` when the button has been clicked once, and execute other code if it has been double-clicked? – Ovidijus Parsiunas May 28 '22 at 01:58
  • sir no. i want to execute this clistBox_pressed() only when the item_1 is double clicked. now. now i dont want to click by index name as they will be renamed to anything else i just want to understand how its done thanks. you can se the listBox.onDoubleClick event i want to use a if statement in that so if double clicked item_1 then execute my code. – hans May 28 '22 at 02:00

2 Answers2

0

It would appear that just need to capitalise the 'i' in Item_1 and remove the else and it should work. Like so:

listBox.onDoubleClick = function () {
  // alert (listBox.selection);

  if (listBox.selection = "Item_1") {
    listBox_pressed()
  }
}
Clif
  • 98
  • 7
0

You could use

if (listBox.selection.text == "Item_1") {
    listBox_pressed();
}
else {
    // do something else
}

Also note that you used the assignment operator instead of the equality comparison in the if statement.

wraith29A
  • 1
  • 1