Highlighted

Button onClick, not functioning

Explorer ,
Sep 01, 2020

Copy link to clipboard

Copied

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.

 

TOPICS
Actions and scripting, Bug, Problem or error, Windows

Views

79

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more

Button onClick, not functioning

Explorer ,
Sep 01, 2020

Copy link to clipboard

Copied

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.

 

TOPICS
Actions and scripting, Bug, Problem or error, Windows

Views

80

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Sep 01, 2020 0
Adobe Community Professional ,
Sep 01, 2020

Copy link to clipboard

Copied

Use try catch.

For tests use this

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

 

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Sep 01, 2020 1
Explorer ,
Sep 01, 2020

Copy link to clipboard

Copied

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!

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Sep 01, 2020 0
Adobe Community Professional ,
Sep 01, 2020

Copy link to clipboard

Copied

Hmm ..
Do not know what to say.
Try to use
$ .write ()
But for this, the script must be run from the ExtendScript Toolkit

I don't use it.
 

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Sep 01, 2020 0
Explorer ,
Sep 01, 2020

Copy link to clipboard

Copied

So the try script yielded no results, however apparently all I had to do was move the onClick call to right below where I defined the button because now it works. That makes no sense. With a globally defined variable I should be able to call it anywhere especially since the onClick call is ALSO taking place in the global scope. Anyone know why the built in listener is expecting the button call right after you define the button?

What clued me in was the fact that I had a close button which was working perfectly. I noticed that the call for the button click for the close button what right after the defining of the close button.

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Sep 01, 2020 0
Adobe Community Professional ,
Sep 01, 2020

Copy link to clipboard

Copied

It's hard for me to understand what you write.
If you would give a more complete code that does not work for you, then it will be possible to explain the reason for your failure.

P.S. If you use addEventListener ("click"), your button will only respond to the mouse.
 

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Sep 01, 2020 1
Explorer ,
Sep 01, 2020

Copy link to clipboard

Copied

My issue has been resolved, here is how I fixed it. (all I did was literally move the button call directly underneath the button definition. This makes no sense to me but whatever it works.

 

//Old code that wasn't working

var execBtn = appWindow.add('button',undefined,'Export');
//some other code here
execBtn.onClick = function()
{
someFunction();
}

//New code that is working

var execBtn = appWindow.add('button',undefined,'Export');
execBtn.onClick = function()
{
someFunction();
}
//some other code here

 

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Sep 01, 2020 0
Adobe Community Professional ,
Sep 01, 2020

Copy link to clipboard

Copied

Both options should work.
Will not work if in
"some other code here" you are redefining execBtn or you are out of scope.
That's why I asked for the complete code.
 
UPD
Also in "some other code here" an exception may occur and the code execution will stop.
That's why I advised to wrap both the entire code and the code inside ANY function in try-catch
 

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Sep 01, 2020 1
Explorer ,
Sep 01, 2020

Copy link to clipboard

Copied

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);
            
    }

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Sep 01, 2020 0
Adobe Community Professional ,
Sep 01, 2020

Copy link to clipboard

Copied

Also, "some other code here" shouldn't have appWindow.show ()
 

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Sep 01, 2020 1
r-bin LATEST
Adobe Community Professional ,
Sep 01, 2020

Copy link to clipboard

Copied

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

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Sep 01, 2020 0
Adobe Community Professional ,
Sep 01, 2020

Copy link to clipboard

Copied

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

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

       someFunction()

})

-Manan

 

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Sep 01, 2020 1
Explorer ,
Sep 01, 2020

Copy link to clipboard

Copied

This is also a great idea. Thank you!

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Sep 01, 2020 0