Write SwiftUI command line scripts!

I figured this might be interesting to some folks on the forums: I updated an old script I had demonstrating how to make a command line tool with a SwiftUI UI. It is now much easier with the new SwiftUI App Lifecycle support (its a whopping 20 lines shorter)!

If you create an executable script from the following gist (chmod +x <path-to-script>), you can invoke it like a script to see it in action (this requires your command line tools to be set to Xcode 12).


Instead of using App.main(), could you use the new @main annotation?

No, because command line scripts treat the body of the script as if it were “main.swift”, so synthesizing a main.swift using the annotation creates a conflict. Either way, it would just be one line of code versus another, so not a ton of utility.
That said, we are, benefitting indirectly from the introduction of the @main attribute, because it encouraged the simplification of the architecture to just having a static main method rather than @NSApplicationMain doing a bunch of magic.

Terms of Service

Privacy Policy

Cookie Policy