Category

Swift script to create a Cocoa window

Swift Language

Swift script to create a Cocoa window

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

How To

Table of Contents

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

Previously you had to use this incantation: “xcrun swift -i myswiftcode.swift”. If you want to compile your swift to an executable when you’re done “scripting”, then you can do this.

Or, simply start your file with a shebang like this.

Do a chmod +x and simply run it.

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.

Summary

Table of Contents

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.

Resources

Table of Contents

2 thoughts on “Swift script to create a Cocoa window”

  1. Would it be possible to load a xib file with this script? I mean design an interface in Xcode and use that with the script from the command line instead of the code. I tried creating a bundle and loading a xib from that bundle but I can’t get it to work. I am trying to create an AppleScript that calls a swift script (which displays a UI) and when the window closes gives the AppleScript the infos back and automates stuff. I need better/more complex UI for my AppleScripts (than AppleScript provides) but I would love to design them in Interface Builder and not in swift code (with all the autolayout code).
    Thanks for that post, I didn’t know that was possible.

    1. My personal need was for a quick window with only a few subviews. Without trying, I can’t think of a reason an xib wouldn’t work. If I have some time (busy time at work right now) I’ll try it. If you share your code with me (github repo? email?), maybe I can find your specific problem.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.