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.
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); }
}
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!
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
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
Copy link to clipboard
Copied
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);
}
Copy link to clipboard
Copied
Copy link to clipboard
Copied
Copy link to clipboard
Copied
Try using addEventListener method instead of onClick. Something like the following
execBtn.addEventListener("click", function(){
someFunction()
})
-Manan
Copy link to clipboard
Copied
This is also a great idea. Thank you!