Use Photoshop Script to create a panel with 3 buttons

1.6k views Asked by At

I would like to create a panel with 3 buttons:

  1. button - add image 1
  2. button - add image 2
  3. Cancel

var dlg = new Window( "dialog", "Alert Box Builder" );
dlg.btnPnl = dlg.add( "panel", undefined, "Build it" );
dlg.btnPnl.testBtn = dlg.btnPnl.add( "button", undefined, "Test" );
dlg.btnPnl.buildBtn = dlg.btnPnl.add( "button", undefined, "Build", {name: "ok" } );
dlg.btnPnl.cancelBtn = dlg.btnPnl.add( "button", undefined, "Cancel", { name: "cancel" } );
dlg.show();
1

There are 1 answers

0
Dominic Fox On

You are on the right track! Doing this just requires adding one more button and then adding a special onClick function for the buttons. That function will open up the operating system's file browser and then assign a file to the pic1File and pic2File variables.

#target photoshop
var pic1File;
var pic2File;

var dlg = new Window( "dialog", "Alert Box Builder" );
btnPnl = dlg.add( "panel", undefined, "Build it" );
pic1Btn = btnPnl.add( "button", undefined, "Image 1" );
pic2Btn = btnPnl.add( "button", undefined, "Image 2" );
buildBtn = btnPnl.add( "button", undefined, "Build", {name: "ok" } );
cancelBtn = btnPnl.add( "button", undefined, "Cancel", { name: "cancel" } );

pic1Btn.onClick = function() {
    pic1File = new File;
    pic1File = pic1File.openDlg ( "Select Background Image", "Images: *.png; *.jpeg; *.jpg" )
    if( pic1File != null ) { pic1Btn.text = File.decode ( pic1File.name ) }
    else { 
        pic1File = new File;
        pic1Btn.text = "No file selected";
    }
}

pic2Btn.onClick = function() {
    pic2File = new File;
    pic2File = pic2File.openDlg ( "Select Background Image", "Images: *.png; *.jpeg; *.jpg" )
    if( pic2File != null ) { pic2Btn.text = File.decode ( pic2File.name ) }
    else { 
        pic2File = new File;
        pic2Btn.text = "No file selected";
    }
}

dlg.show();

Hope that helps!