Skip to main content
Mugen777
Inspiring
June 4, 2021
Answered

[Scripting] How to disable a button from another window?

  • June 4, 2021
  • 1 reply
  • 1998 views

This is the layout I was fallowing while creating my GUI.

https://www.goodboy.ninja/snippets/dockable-scriptui-panel-template

I can disable buttons easily with buttonName.enabled = false; but I couldn't make it work from another window. 

By the way, the "another window" I am speaking about still part of the script. It is opening when I click "Help" button. 

This topic has been closed for replies.
Correct answer Paul Tuersley

So here's an example where, if you were trying to stop multiple help windows from being created, that's no longer a problem as this will only create one if necessary.

But just for the hell of it, I also stuck in an example that turns the help button on the main panel on/off. Don't really know what you're trying to do here but hopefully this will give you all the info you need.

 

function myAwesomeScript(thisObj) {
	
	var helpWindow;	// define in main script scope so it's accessible throughout
	
	function buildUI(thisObj) {
		var pal = (thisObj instanceof Panel) ? thisObj : new Window("palette", undefined, undefined, {resizeable: false});
		//myPanel.text = "Expression Controller";
		pal.orientation = "row";
		pal.alignChildren = ["left", "top"];
		pal.spacing = 10;
		pal.margins = 10;

		pal.help = pal.add("button", undefined, undefined);
		pal.help.helpTip = "Help";
		pal.help.text = "?";
		pal.help.preferredSize.width = 27;
		pal.help.preferredSize.height = 27;
		pal.help.alignment = ["left", "center"];

		pal.help.onClick = function () {
			if (helpWindow == null || helpWindow.visible == false) buildHelp();	// only create helpWindow if it isn't already visible
			else helpWindow.show();
		};
	
		return pal	// return panel object
	}

	function buildHelp() {
		helpWindow = new Window("palette","Help - About",undefined,{resizeable:false});
		helpWindow.orientation = "column"; 
		helpWindow.alignChildren = ["center","top"]; 
		helpWindow.spacing = 12; 
		helpWindow.margins = 15;
		
		var test = helpWindow.add("button", undefined, "Try Me!");
		test.onClick = function() {
			if (myPanel.help.enabled == true) myPanel.help.enabled = false;	// here's an example of now being able to access the UI from outside the scope of the buildUI function
			else myPanel.help.enabled = true;
		}
	
		helpWindow.onClose = function() {
			myPanel.help.enabled = true; // just in case you left it turned off
		}

		helpWindow.show();
	}

	var myPanel = buildUI(thisObj);  // receive panel object. define in main script scope so it's accessible throughout
	if (myPanel != null) {
		if (myPanel instanceof Window) {
			myPanel.center();
			myPanel.show();								
		}
	}	
}
myAwesomeScript(this);

1 reply

Inspiring
June 4, 2021

I guess the problem is that any variables you define inside the buildUI function are only accessible within that function. That is how variable scope works with functions. So one solution might be to just put...

var myPanel;

...at the top inside your main function, then remove the 'var' from where you assign 'var myPanel' inside the buildUI function. And now the myPanel variable will be accessible throughout the scope of the outer function which will presumably include all your code.

 

Typically I'll do it more like....

var thePanel = buildUI(thisobject);

...and then right at the end inside of the buildUI function I'd put...

return myPanel;

 

so the panel object is returned from the buildUI function and assigned to 'thePanel' variable.

 

In theory you should be able to access all the child elements of the panel object like thePanel.myGroup.myButton but that may depend on how you're creating your UI. At the very least if you're doing something like myButton = myPanel.add(... then again you just need to initially define 'var myButton' in the outer function scope and not use var inside the buildUI function. It's hard to give you anything other than a general answer without seeing the code you're using to create your UI elements.

 

Typically I create my UIs this way:

https://extendscript.docsforadobe.dev/user-interface-tools/resource-specifications.html

as opposed to using the line by line myPanel.add(

 

 

Mugen777
Mugen777Author
Inspiring
June 4, 2021

Hey Paul, thanks for the help again 🙂 

    // GUI

    buildUI(thisObj);
    function buildUI(thisObj) {
        var myPanel = (thisObj instanceof Panel) ? thisObj : new Window("window", undefined, undefined, {resizeable: false});
        //myPanel.text = "Expression Controller";
        myPanel.orientation = "row";
        myPanel.alignChildren = ["left", "top"];
        myPanel.spacing = 10;
        myPanel.margins = 10;

...

        var help = myPanel.add("button", undefined, undefined);
        help.helpTip = "Help";
        help.text = "?";
        help.preferredSize.width = 27;
        help.preferredSize.height = 27;
        help.alignment = ["left", "center"];

...

        help.onClick = function () {
            var helpWindow = new Window("dialog","Help - About",undefined,{resizeable:false});
            about(helpWindow);
        };

...

    function about(helpWindow){
        helpWindow.orientation = "column"; 
        helpWindow.alignChildren = ["center","top"]; 
        helpWindow.spacing = 12; 
        helpWindow.margins = 15;

        helpWindow.show();
    }


})(this);  

 
This is how I built it. I can't say I understand what do you mean to be honest, can you give another example? 

Inspiring
June 4, 2021

Are you saying you want to stop the user from being able to click the help button again once the help window is open?