Skip to main content
Participant
May 31, 2016
Answered

onclick-button name or ID

  • May 31, 2016
  • 1 reply
  • 528 views

Good morning all,

i have a little project that i would like to realize in javascript to use with photoshop.

I want to create a window containing buttons  triggering scripts files (.jsx)

The project is stopped because i can't get the name or ID of the clicked button.

I built this script (see below) from a mix of different sources i found in the documentation ..

The window is create and the onclick event works, i tried many things to get properties of "This" , but i always have  "undifined" in return..

Who can show me the proper method to reach the goal?

Thank you...

*english is not my native language

//-------------

var sSamplesFolderName = localize( "myBibScript" ); // folder containing the files

var folderSamples = new Folder("/Volumes/myTools/scripts - macros/" + sSamplesFolderName); // path

var files = folderSamples.getFiles("*.*");

var ui = // dialog resource object

    "dialog { \

        gButtons: Group { \

            orientation: 'row', alignment: 'right'\

        } \

    }";

// create and position the window

var win = new Window(ui);

win.frameLocation = [0,0];

win.opacity=0.85

//color background

g = win.graphics; myBrush = g.newBrush(g.BrushType.SOLID_COLOR, [0.75, 0.75, 0.75, 1]);

g.backgroundColor = myBrush;

win.cbFiles = new Array();  //  items

var counter = 0;

    for (var loop=0; loop < files.length; loop++) {  // Add items dynamically

        var f = files[loop];

        if (typeof f.open == "undefined")    continue;    // Skip folder

        if ( -1 != f.fsName.indexOf(".DS_Store"))    continue;    // Skip Mac's hidden file

    var fName=f.fsName.substring(folderSamples.fsName.length+1); //extract filename only

    var fNameShort =fName.replace(".jsx", ""); // supprime l'extension

   

   win.cbFiles[counter++] = win.gButtons.add('button', undefined, fNameShort,{name:fNameShort});

   

};

win.gButtons.addEventListener("click",gButtonsOnClick);

function gButtonsOnClick() {

/*(i just display this alert for debug)  i try to determine which button has been clicked  and display the name or the index here */

alert(this.index); //returns "undifined" in the alert box

 

  }

win.show();

//-----------------

This topic has been closed for replies.
Correct answer c.pfaffenbichler

Does this work?

var ui = // dialog resource object

    "dialog { \

        gButtons: Group { \

            orientation: 'row', alignment: 'right'\

        } \

    }";

// create and position the window

var win = new Window(ui);

win.frameLocation = [0,0];

win.opacity=0.85

//color background

g = win.graphics; myBrush = g.newBrush(g.BrushType.SOLID_COLOR, [0.75, 0.75, 0.75, 1]);

g.backgroundColor = myBrush;

win.cbFiles = new Array();  //  items

var counter = 0;

for (var loop=0; loop < 10; loop++) {  // Add items dynamically

win.cbFiles[counter++] = win.gButtons.add('button', undefined, loop,{name:loop});

win.gButtons.children[loop].onClick = gButtonsOnClick

};

function gButtonsOnClick() {

alert(this.text);

}

win.show();

1 reply

c.pfaffenbichler
Community Expert
c.pfaffenbichlerCommunity ExpertCorrect answer
Community Expert
May 31, 2016

Does this work?

var ui = // dialog resource object

    "dialog { \

        gButtons: Group { \

            orientation: 'row', alignment: 'right'\

        } \

    }";

// create and position the window

var win = new Window(ui);

win.frameLocation = [0,0];

win.opacity=0.85

//color background

g = win.graphics; myBrush = g.newBrush(g.BrushType.SOLID_COLOR, [0.75, 0.75, 0.75, 1]);

g.backgroundColor = myBrush;

win.cbFiles = new Array();  //  items

var counter = 0;

for (var loop=0; loop < 10; loop++) {  // Add items dynamically

win.cbFiles[counter++] = win.gButtons.add('button', undefined, loop,{name:loop});

win.gButtons.children[loop].onClick = gButtonsOnClick

};

function gButtonsOnClick() {

alert(this.text);

}

win.show();

Participant
May 31, 2016

it works perfectly !

Thank you !!