Create arrays of textboxes etc

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

Moderators: xbytor, Kukurykus, Tom


Create arrays of textboxes etc

Post by michaelb16561 »

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 = [];


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 = dlg.add('panel', undefined, 'Panel ' + (i + 1) + ':');
      grpGroup = pnlPanel.add('group');
      txtTextbox = grpGroup.add('edittext', undefined, '0');
      txtTextbox.preferredSize = [50,20];
      txtTextbox.justify = 'right';
      txtTextbox.text = 0;
      sldSlider = grpGroup.add('slider', undefined, 0, 0, 100);

   // Show the window;;

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 alltxtTextbox[i].onChange = function () { sldSlider[i].value = Number(txtTextbox[i].text); };

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

Hope someone may find this helpful,

- Michael B

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

Re: Create arrays of textboxes etc

Post by michalt »

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
Posts: 301
Joined: Mon Jul 25, 2016 12:36 pm

Re: Create arrays of textboxes etc

Post by Kukurykus »

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!