Skip to main content
Known Participant
September 1, 2020
Question

Button onClick, not functioning

  • September 1, 2020
  • 2 replies
  • 825 views

Greetings.

 

I have a button defined as such

 

var execBtn = appWindow.add('button',undefined,'Export');

 

I then click on it thusly:

 

execBtn.onClick = function(){

someFunction();

 

}

 

The code in someFunction() runs fine outside of the button but will not execute when wrapped within the button. I triple checked that variables passed to someFunction were either global or defined within the scope of the button click itself. Not sure why it will not run. Is there some other way to do this like onButtonDown or something?

 

I appreciate the community's time and any assistance you can provide.

 

This topic has been closed for replies.

2 replies

Community Expert
September 2, 2020

Try using addEventListener method instead of onClick. Something like the following

execBtn.addEventListener("click", function(){

       someFunction()

})

-Manan

 

-Manan
Known Participant
September 2, 2020

This is also a great idea. Thank you!

Legend
September 1, 2020

Use try catch.

For tests use this

execBtn.onClick = function()
   {
    try {
        alert(1);
        someFunction();
        alert(2);
        }
    catch(e) { alert(e); }
    }

 

Known Participant
September 1, 2020

Thank you very much! I was wondering how I could do a "console.log()" type debug to catch where it was going wrong. Thanks again!

Legend
September 2, 2020

good to know. Here is the complete working code

#target 'photoshop'

//Array 
	var languages = ['en','tr','sv','da','nb','ru','ja','zh-hant','zh-hans','pt-br','ko','it','fr','de','es'];

//Reference to the document
	var doc = app.activeDocument;
	
//Counter for progress
	var counter = 0;
	
//*************************************************************************
//Create the GUI window
	var appWindow = new Window('dialog', 'CRM Exporter');
//Styling the window
	//Size (must use an window.onShow(){}; callback
	appWindow.onShow = function(){appWindow.size = 'width: 230, height: 230';}
//The Create Directory input
	var createDir = appWindow.add('statictext',undefined,'Create Folder Name:');
//Input textfield
	var dirNameInpt = appWindow.add('edittext',undefined,undefined);
	dirNameInpt.characters = 20;
	dirNameInpt.active = true;
//Progress bar
    var numFiles = languages.length;
    var progBar = appWindow.add('statictext',undefined, 'Progress:');
    var progFiles = appWindow.add('statictext',undefined, counter + ' of ' + numFiles + ' ');
//The execute button
	var execBtn = appWindow.add('button',undefined,'Export');
//On execute button press
	execBtn.onClick = function()
	{
		
	//Loop through the language groups, making each one visible and exporting 
	//and then hiding the group in turn
		for(var i=0; i<doc.layerSets.length; i++)
		{
			//Temp current layer
			var currentLayer = doc.layerSets[i];
			
			//File name uses the language abbreviations from the languages list as file name
			var fileName = languages[i];
			
			//Step 1. make the current group visible
			currentLayer.visible = true;
			
			//Step 2. export the image as PNG
			exPNG24(fileName, dirNameInpt.text);
			
			//Step 3. make the current group invisible
			currentLayer.visible = false;
			
			//Update the counter
          counter++;
          progFiles.text = counter + ' of ' + numFiles + ' ';
		}
		
		//Close after completeing the loop
		appWindow.close();
	
	};
//Close button
    var closeBtn = appWindow.add('button',undefined,'Close');
    closeBtn.onClick = function()
    {
        appWindow.close();
    };
//Display the GUI
	appWindow.show();
//*************************************************************************

//Functions

function exPNG24(n, d)
    {
		var name = n;
		var directory = d;
		
       //Create a new directory and file name
       var folder = new Folder(doc.path + '/' + directory);
	   //If the directory doesn't currently exist then create it
		if(!folder.exists){
			folder.create();
		}

        var file = new File(folder + '/' + name + '.png');
        
        var options = new ExportOptionsSaveForWeb();
        
        options.format = SaveDocumentType.PNG;
        //Use PNG-24
        options.PNG8 = false;
        //Preserve transparency
        options.transparency = true;
        options.interlaced = false;
        options.embedColorProfile = false;
        //Maximum quality and medium compression to keep file size low
        options.quality = 100;
        options.compression = 5;
        
        //export the image
       doc.exportDocument(file, ExportType.SAVEFORWEB, options);
            
    }

It would be better to Show the code that didn't work