Swift script to create a Cocoa window

Datetime:2016-08-23 01:38:34         Topic: Swift  Cocoa          Share        Original >>
Here to See The Original Article!!!

Swift script to create a Cocoa window

Create a Cocoa window with a functioning button from a command line Swift script.

You probably know that you can run a swift program from the command line like a script.


Can you create an OSX (Cocoa) Window from that “script”?Yes. Yes you can.

You can go ahead and create an NSWindow and pop it up. Other “tutorials” on the net do this. But it’s really not going to run like a program.

A better way is to create a class that implements the NSApplicationDelegate protocol. Xcode project templates always creates these for you, so you’ve seen and used them. So create your own.

One strategy is to have your appdelegate create and configure an NSWindow . So that’s what this example will show. I’m using the new autolayout constraint syntax, so I’ve shown you how to use the “available” feature to see if your environment can grok them. If not, just use one of the old constraint syntaxes.

To be able to close the app via the red window icon, you need a class that conforms to the NSWindowDelegate protocol. You can observe a window closing notification instead, but setting the window delegate is a bit simpler.

Once you have your appdelegate and its window configured, create an NSApplication , set its delegate to your appdelegate, and run the application.

import Cocoa
// due to the new constraint syntax
@available(OSX 10.11, *)
class AppDelegate: NSObject, NSApplicationDelegate {
    let window = NSWindow()
    let windowDelegate = WindowDelegate()
    func applicationDidFinishLaunching(aNotification: NSNotification) {
        window.setContentSize(NSSize(width:600, height:200))
        window.styleMask = NSTitledWindowMask |
            NSClosableWindowMask |
            NSMiniaturizableWindowMask |
        window.level    = 1
        window.delegate = windowDelegate
        window.opaque  = false
        window.title    = "Window with a button"
        let contentView = window.contentView!
        let button        = NSButton(frame: NSRect(x:0, y:0, width:0, height:0))
        button.bezelStyle = .ThickSquareBezelStyle
        button.title      = "Click Me"
        button.action    = #selector(AppDelegate.myAction(_:))
        // autolayout
        button.translatesAutoresizingMaskIntoConstraints = false
        var c        = button.centerYAnchor.constraintEqualToAnchor(contentView.centerYAnchor)
        c.active    = true
        c.identifier = "to center y"
        c            = button.centerXAnchor.constraintEqualToAnchor(contentView.centerXAnchor)
        c.active    = true
        c.identifier = "to center x"
        c            = button.widthAnchor.constraintEqualToConstant(200)
        c.active    = true
        c.identifier = "width"
        c            = button.heightAnchor.constraintEqualToConstant(50)
        c.active    = true
        c.identifier = "height"
    func myAction(sender: AnyObject) {
        print("my action")
class WindowDelegate: NSObject, NSWindowDelegate {
    func windowWillClose(notification: NSNotification) {
if #available(OSX 10.11, *)  {
    let app = NSApplication.sharedApplication()
    let appDelegate = AppDelegate()
    app.delegate = appDelegate

Create a class that implements NSApplicationDelegate . Have that class create and configure an NSWindow .

Then create an NSApplication , set its delegate to your class, and run.

About List