Cannot hide/show windows programmatically

Hello everyone,

I’m transitioning some scripts I have written from AppleScript to AppleScriptObjC and I’m having trouble working with showing/hiding windows.
One of the scripts I’m trying to move over is really long and I’m replacing a few of the dialogs with more complex windows created in IB however I’m having trouble. Basically I need to click a button on one window (mainWindow) that will trigger a new window to show (or a dialog) and the mainWindow should hide and another function should be called. I can get this to work by connecting the window to the button in the IB and the orderOut but this doesn’t run my code, it just hides the window. I also managed a function to be called when the button is clicked and show a dialog programmatically but not a window, and cannot hide the main window.
The error I get is:

[AppDelegate displayButton:]: missing value doesn’t understand the “orderOut_” message. (error -1708)

The example below (if it worked) would help me understand how it works and the syntax used however I haven’t managed to make it work.

use Framework "Foundation"
use scripting additions

script AppDelegate
    property parent : class "NSObject"
    property mainWindow : missing value
    property isShowing : 0
    
    on applicationWillFinishLaunching_(aNotification)
        -- Insert code here to initialize your application before any files are opened
    end applicationWillFinishLaunching_
    
    on applicationShouldTerminate_(sender)
        -- Insert code here to do any housekeeping before your application quits
        return current application's NSTerminateNow
    end applicationShouldTerminate_

    on displayButton_(sender)
        if (isShowing = 0) then
            set isShowing to 1
            mainWindow's orderFront:sender
        else
            set isShowing to 0
            mainWindow's orderOut:sender
        end
    end displayButton_
end script

Can anyone point me in the right direction?
Shane already told me that the syntax to orderOut is orderOut:sender instead of orderOut_(sender)

The error you’re getting — missing value doesn’t understand the “orderOut_” message — tells me that the property mainWIndow is never getting set with a reference to your window object. Either the window is not loaded when the displayButton routine is called, or the window is not connected to the property in the xib.

Though I am a bit confused: if mainWindow is the window being referenced by the button, what window is the button in?

Hello TedW,

Thank you very much for your reply, it was what I needed, I checked the Delegate and I hadn’t connected things correctly. After doing so, it all worked out, so it is now resolved.

Regarding your confusion, before posting the code I had the first window with the button in it as mainWindow and the other window as just “window”, I went and changed that to “mainWindow” while posting the question to see if it can hide itself like I want to do in my actual script (and the function opens a new window which has a back button that brings it back).

I’m used to working with window handles (in Windows and AutoIt) so I didn’t know that lots of things take place in the IB.

Now I know :slight_smile: