Blog

SwiftUI: Running a Mac App Without an Xcode Project

In 2018, we wrote a post about using AppKit from the command line. We can use exactly the same technique to run a SwiftUI app directly from a Swift file.

When you're writing a quick tool, perhaps to visualize something, or to get user feedback, an Xcode project can feel like overkill. It's often very useful to have a single file that you just run with swift myfile.swift from Terminal. Likewise, when you're inside a Swift Package Manager project, you might not want to create an additional Xcode project when you're just testing things out.

What if you could just do the following?

								NSApplication.shared.run {
    VStack {
        Text("Hello, World")
        	.padding()
            .background(Capsule().fill(Color.blue))
            .padding()
    }
    .frame(maxWidth: .infinity, maxHeight: .infinity)
}

							

Fortunately, you can, by pasting in the boilerplate code from this gist. Simply run swift boilerplate.swift in Terminal and you'll have a macOS app up and running.

If you're building a complex app, you'll almost certainly want to move to a "real" application, but for one-off tools and prototyping we've found this shortcut very useful.

Screenshot

In Swift Talk Episode #145, which is free to watch, we use the same technique to create an AppKit app using Swift Package Manager.

Stay up-to-date with our newsletter or follow us on Twitter .

Back to the Blog

Recent Posts