Photoshop CS2 How to create and populate a drop down list

Photoshop Script Snippets - Note: Full Scripts go in the Photoshop Scripts Forum

Moderators: Tom, Kukurykus

Larry Ligon

Photoshop CS2 How to create and populate a drop down list

Post by Larry Ligon »

Creates 2 drop down lists. The first is a created list and the second uses the fonts list from JavaScript. This is a full script.

Code: Select allvar stringOptions = [];
stringOptions[0] = "Document Name";
stringOptions[1] = "document name";
stringOptions[2] = "1 Digit Serial Number";
stringOptions[3] = "2 Digit Serial Number";
stringOptions[4] = "3 Digit Serial Number";

var myStringOptions = "sometext";
var myFontSelection = "none";
var dlg = new Window ('dialog', 'Sample dialog');

dlg.dropdownlist = dlg.add("dropdownlist", undefined,"test");

var item
for (var i=0,len=stringOptions.length;i<len;i++)
  {item = dlg.dropdownlist.add ('item', "" + stringOptions);     
};

dlg.dropdownlist.onChange = function() {
   //Save the selected value in a variable to be used later
   myStringOptions = stringOptions[parseInt(this.selection)];
   };

dlg.btnPnl = dlg.add('panel', undefined, 'Fonts');

dlg.btnPnl.dropdown = dlg.btnPnl.add('dropdownlist', undefined, 'Test');

var item
for (var i=0,len=app.fonts.length;i<len;i++)
  {item = dlg.btnPnl.dropdown.add ('item', "" + app.fonts.name);
     
};
dlg.btnPnl.dropdown.onChange = function(){
   //Save the selected value in a variable to be used later
   myFontSelection = app.fonts[parseInt(this.selection)].name;
   };

var uiButtonRun = "Close";

dlg.btnRun = dlg.add("button", undefined ,uiButtonRun );
dlg.btnRun.onClick = function() {
    alert("String Options is " +  myStringOptions + "\n" +
          "My font selection is " + myFontSelection);   
    this.parent.close(0); };
dlg.orientation = 'column';

dlg.center();
dlg.show();

Use it well.
v.bampton

Photoshop CS2 How to create and populate a drop down list

Post by v.bampton »

Thanks Larry, you answered my question before I got to answer it!

Victoria
Larry Ligon

Photoshop CS2 How to create and populate a drop down list

Post by Larry Ligon »

To have the first item in the drop down list to appear in the drop down box instead of a blank, use this code instead of above:

Code: Select allvar stringOptions = [];
stringOptions[0] = "Document Name";
stringOptions[1] = "document name";
stringOptions[2] = "1 Digit Serial Number";
stringOptions[3] = "2 Digit Serial Number";
stringOptions[4] = "3 Digit Serial Number";

var myStringOptions = "sometext";
var myFontSelection = "none";
var dlg = new Window ('dialog', 'Sample dialog');

dlg.dropdownlist = dlg.add("dropdownlist", undefined,"test");

var item
for (var i=0,len=stringOptions.length;i<len;i++)
  {item = dlg.dropdownlist.add ('item', "" + stringOptions);     
};

dlg.dropdownlist.onChange = function() {
   //Save the selected value in a variable to be used later
   myStringOptions = stringOptions[parseInt(this.selection)];
   };

dlg.btnPnl = dlg.add('panel', undefined, 'Fonts');

dlg.btnPnl.dropdown = dlg.btnPnl.add('dropdownlist', undefined, 'Test');

var item
for (var i=0,len=app.fonts.length;i<len;i++)
  {item = dlg.btnPnl.dropdown.add ('item', "" + app.fonts.name);
     
};
dlg.btnPnl.dropdown.onChange = function(){
   //Save the selected value in a variable to be used later
   myFontSelection = app.fonts[parseInt(this.selection)].name;
   };

var uiButtonRun = "Close";

dlg.btnRun = dlg.add("button", undefined ,uiButtonRun );
dlg.btnRun.onClick = function() {
    alert("String Options is " +  myStringOptions + "\n" +
          "My font selection is " + myFontSelection);   
    this.parent.close(0); };
dlg.orientation = 'column';

//Make the first item in list appear instead of a blank
dlg.dropdownlist.selection = dlg.dropdownlist.items[0] ;
dlg.btnPnl.dropdown.selection = dlg.btnPnl.dropdown.items[0] ;

dlg.center();
dlg.show();
Mr Maze

Photoshop CS2 How to create and populate a drop down list

Post by Mr Maze »

wow it is incredible how much this post has helped me.
mvadu

Photoshop CS2 How to create and populate a drop down list

Post by mvadu »

In the add items function you can assign a name for the item
Code: Select alldlgMain.bodyPnl.saveOpts.add('item', "Item one",0,{name:'item1'});

How do I frame my switch clause? I don't want to hard code the order, or use the array like in this example. In VB like languages, you can access the elements in the collection (similar to items collection of dropdownlist) by index, or key or name. But in PS this is not working
Code: Select alldlgMain.bodyPnl.saveOpts.onChange = function(){      
    if(dlgMain.bodyPnl.saveOpts.selection==dlgMain.bodyPnl.saveOpts.items['item1'])
          alert("asd");
};
Mike Hale

Photoshop CS2 How to create and populate a drop down list

Post by Mike Hale »

I think that name is a standard creation property of the dropdownlist but not of an item element. However as long as you make sure you create a name property for each item element you can do something like this.

Code: Select allif(this.selection.properties.name == 'item1') alert("asd");

Or did I misunderstand what you are asking?
mvadu

Photoshop CS2 How to create and populate a drop down list

Post by mvadu »

Thank Mike, you nailed it. I did not realise that I have to use "selection.properties.name", I was trying "selection.name".

The advantage of adding a unique name to each of the items is that in the switch statement you need not to depend on index, and your code would be much more readable.
Code: Select all
dlgMain.bodyPnl.saveOpts.add('item', "Save As Copy",1,{name:'saveAs'});
dlgMain.bodyPnl.saveOpts.add('item', "Save",0,{name:'save'});

dlgMain.bodyPnl.btnGrp.saveBtn.onClick = function ( ) {
      switch(dlgMain.bodyPnl.saveOpts.selection.properties.name){
         case 'saveAs': alert("Save As copy"); break;
         case 'save': alert("Save Current"); break;
         }
Mike Hale

Photoshop CS2 How to create and populate a drop down list

Post by Mike Hale »

To use selection.name you don't want at add the name property in the creation properties.

Code: Select alldlgMain.bodyPnl.saveOpts.add('item', "Save As Copy",1);
dlgMain.bodyPnl.saveOpts.add('item', "Save",0);
dlgMain.bodyPnl.saveOpts.items[0].name = 'Save';// add property after item creation
dlgMain.bodyPnl.saveOpts.items[1].name = 'SaveAs';