Create arrays of textboxes etc

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

Moderators: xbytor, DavideBarranca, Paul MR

michaelb16561

Create arrays of textboxes etc

Postby michaelb16561 » Wed Sep 28, 2011 4:04 am

Hi guys,

This is a small snippet of code to show how to create an array of textboxes and sliders etc.

Code: Select all
// Declare the arrays of textboxes and sliders here so they can be accessed globally
var txtTextbox= [];
var sldSlider = [];

main();

function main(){
   var pnlPanel = [];
   var grpGroup = [];

   dlg = new Window ("dialog");
   
   // Add a panel and create the input controls
   
   for (var i = 0; i < 5; i++){
      pnlPanel[i] = dlg.add('panel', undefined, 'Panel ' + (i + 1) + ':');
      
      grpGroup[i] = pnlPanel[i].add('group');
      
      txtTextbox[i] = grpGroup[i].add('edittext', undefined, '0');
      txtTextbox[i].preferredSize = [50,20];
      txtTextbox[i].justify = 'right';
      txtTextbox[i].text = 0;
      
      sldSlider[i] = grpGroup[i].add('slider', undefined, 0, 0, 100);
   }

   // Show the window
   dlg.center();
   dlg.show();
   
   return;
}

Note: If the textboxes and sliders are to be accessed from another function it is required to delcare them as global variables.
For some reason arrays of textboxes, sliders etc don't seem able to be referenced as children of another object (eg. dlg.txtTexbox)

I'm still not quite sure how to assign event handlers to the controls dynamically, within the 'for loop'
This doesn't work:-
Code: Select all
txtTextbox[i].onChange = function () { sldSlider[i].value = Number(txtTextbox[i].text); };


You just have to assign each one individually:-
Code: Select all
txtTextbox[0].onChange = function () { sldSlider[0].value = Number(txtTextbox[0].text); };


Hope someone may find this helpful,

- Michael B



michalt
Posts: 1
Joined: Mon Oct 03, 2016 9:59 pm

Re: Create arrays of textboxes etc

Postby michalt » Mon Oct 03, 2016 10:09 pm

I found the solution for assigning values dynamicly

Code: Select all

for (var i = 0; i < 5; i++){
 txtTextbox[i].onChange = function () {
    for (var k = 0; k < 5; k++){
    sldSlider[k].value = Number(txtTextbox[k].text); }
 }
 }

User avatar
Kukurykus
Posts: 114
Joined: Mon Jul 25, 2016 12:36 pm

Re: Create arrays of textboxes etc

Postby Kukurykus » Tue Oct 04, 2016 7:10 pm

After 5 years and 5 days?! It had to be long travel thru scripting lands :shock:

I'm joking, I'm learning and that is amazing news to me. Could you please show us how to implement that snippet to your earlier code that it really work dinamically, Thank You!


Return to “Photoshop Scripting: Code Snippets”

Who is online

Users browsing this forum: No registered users and 1 guest