Cannot be used for the loop variable 'i' twice?

Something weird is happening with a dialog box that I'm doing that dynamically creates buttons based on the number of objects in a table that I pass in a function. I say weird, but it's obviously me not understanding how the variables in a loop. See below:

mrp = {
    one: [new File("/Users/constantincerdan/Creative%20Cloud%20Files/Templater/Iconbuttons/bagspouches.png"), "One"],
    two: [new File("file:///Users/constantincerdan/Creative%20Cloud%20Files/Templater/Iconbuttons/beauty.png"), "Two"],
    three: [new File("file:///Users/constantincerdan/Creative%20Cloud%20Files/Templater/Iconbuttons/bluepocketsquare.png"), "Three"]
}

function Dialog(dialogTitle, buttons) {

    var dlg = new Window("dialog", dialogTitle + " - OU");
    dlg.margins = [20, 15, 20, 17];
    dlg.buttonsGroup = dlg.add("group");
    dlg.buttonsGroup.orientation = "row";
    dlg.buttonsGroup.spacing = 15;

    for(var i = 0; i < buttons.length; i++){
        templaterButton = dlg.buttonsGroup.add("iconbutton", undefined, buttons[i][0])
        templaterButton.onClick = function(){
            open(buttons[i][0])      
            dlg.close()      
        }
        dlg.buttonsGroup.add("panel", [undefined, undefined, 0,120])
    }
    dlg.show()
}
Dialog("Test",[mrp.one, mrp.two, mrp.three])






Execution of this poster dialog very well, and all the icon buttons have their own photos as they should (see photo below).

This is what confuses me: line 16, he is able to use I on the buttons [i] [0] very well get every image and use it, but for some reason any when I also try to use the same thing to line 18 to try to open the real images for each button, I get the error "undefined is not an object" on this line. Change key [i] [0] to line 18 to the key [0] [0] opens the first image very well (on three keys), so I can't understand what I'm doing wrong here?

Screen Shot 2016-02-04 at 22.44.44.png

The problem is that the function that executes the dialog box is not a variable that I exist. It is a function that get-called on the fly. One thing you can do is to use 'this' in the service running, so it will point to the iconbutton that you clicked on, unfortunately there is no default property of this button that can point to the actual file, there is only the property "icon" but that only gives the name of the file and not its full path , but you can create a custom property that can point to the path to the file like this:

MRP = {}

a: [new file ("/Users/constantincerdan/Creative%20Cloud % 20Files/Templater/Iconbuttons/bagspouches program. PNG"),"One"],

two: [new file ("file:///Users/constantincerdan/Creative%20Cloud%20Files/Templater/Iconbuttons/beaut y.png"), "Two"],

three: [new file ("file:///Users/constantincerdan/Creative%20Cloud%20Files/Templater/Iconbuttons/bluep ocketsquare.png"), "Three"]

}

function {Dialog (dialogTitle, buttons)

var DLG = new window ("dialog", dialogTitle + "- OR");

DLG. Margins = [20, 15, 20, 17];

dlg.buttonsGroup = dlg.add ('group');

dlg.buttonsGroup.orientation = "row";

dlg.buttonsGroup.spacing = 15;

for (var i = 0; i)< buttons.length;="">

templaterButton = dlg.buttonsGroup.add ("iconbutton", undefined, buttons [i] [0]);

templaterButton.myFile buttons [i] [0] ;// = my custom property

templaterButton.onClick = function() {}

App.Open (this.) MyFile);

Open (this.) MyFile); / / use the property of the selected object

DLG. Close();

}

dlg.buttonsGroup.add ('panel', [undefined, undefined, 0,120])

}

DLG. Show()

}

Dialog box ("Test" [mrp.one, mrp.two, mrp.three])

Tags: Photoshop

Similar Questions

Maybe you are looking for