Search code examples
xcodeuser-interfaceapplescriptapplescript-objc

How to open a new GUI and close an old GUI in ApplescriptObjC


I'm trying to make it so when a button is clicked, the current GUI closes and a new GUI opens.

I created a second interface to open. So right now, I have 2 xib files, and the AppDelegate file. Do I program both of the GUIs with the same AppDelegate file?

Second of all, How do I get the first xib file to close and the second xib file to open.

on buttonClicked_(sender)

-- Code that closes current GUI and opens new GUI here

end buttonClicked_

Solution

  • Normally you only have one application delegate in AppleScriptObjC. The choice if an window needs to be in another xib file or not totally depends of the purpose of the window. Most developers prefer not to have an window in the MainMenu.xib at all but create a MainWindow.xib or some developers prefer to create UI on the fly. So having 2, 1 or even 3 xib files doesn't say much, anyway I would prefer to have a xib for every window.

    To keep it simple and make it all work:

    • Add new window to your project (and I saved it as MainWindow.xib)
    • Set class of file's owner to NSWindowController
    • Connect window with file's owner and make it the window
    • Add new AppleScript to your project (subclass of NSObject)
    • Write the IBOutLet and actions for the window (see below)
    • Add new NSObject to the xib file
    • Set class of NSObject (blue cube) to new AppleScript object
    • Connect the window with the IBOutlets and IBActions.
    • Connect also the window's delegate to the script (blue cube)

    script for window:

       script mainWindow
           property parent : "NSObject"
           property myWindow : missing value
           property myButton : missing value
    
           on myButtonClicked:sender
              log "I'm clicked"
           end myButtonClicked:
        end script
    

    To load the window we need something like this in our app delegate:

    set theController to current application's class "NSWindowController"'s alloc()'s init()
    current application's class "NSBundle"'s loadNibNamed:"MainWindow" owner:theController
    

    Now when we're in the application delegate and loaded our nib properly we can close our window by doing just theController's close() and show the window by using theController's showWindow:me For the other window just repeat those steps above and you can switch between them.