A few years ago, I published a post that described how to build a Mac application in Swift that would run a shell command. Surprisingly, this post still gets a lot of traffic. Xcode and Swift have progressed over the last three years and that old example is mostly useless now. It is time for an update!
In this post, I will describe how to build a simple Mac app which runs a shell command using SwiftUI.
SwiftUI is the new framework from Apple to build user interfaces across platforms. Applications built with Swift UI will require 10.15 Catalina and higher. As you will see, SwiftUI does simplify a lot of the work of creating the user interface. I believe that learning SwiftUI now will be a good investment into the future.
I have written this post with Xcode 11.2.1 on macOS Catalina 10.15.1. That means we are using Swift 5.1. You can download Xcode from the Mac App Store or Apple’s developer page.
In this post, we will be using the say
command to make the Mac speak. You can test the command by opening the Terminal and entering
> say "Hello World"
The say
command is a simple and fun stand in for other command line tools. It also provides instant feedback, so you know whether it is working or not. That said, when you want to provide text-to-speech functionality in a Swift app, you should probably use the text-to-speech frameworks, rather than sending out a shell command.
Nevertheless, for Mac Admin tasks, there are some that are only possible, or at least much easier, with shell tools.
First: Swift UI hello world
Before we get to more complicated things, let’s honor the classics and build a “Hello, World” application using Swift UI.
Open Xcode and from the project picker select “New Project.”
In the template chooser select ‘macOS’ and ‘Application’. Then click ‘Next.’
In the next pane, enter a name for the project: “SayThis.” Verify the other data and make sure the choice for ‘User Interface’ is ‘SwiftUI’. Then click ‘Next.’
The window with the new project will have four vertical panes. You can use the controller element in the top right of the toolbar to hide the right most “Inspector” pane as we will not need it.
Click it the “Play” button in the top left to build and run the template code, a window should open which show the place holder text “Hello World!”
When you return to Xcode the preview pane on the right should now be active and also display the “Hello World!” text. If it does not there should be a “Resume” button at the top of the preview pane (also called “Canvas”) which you can click to make Xcode resume live updating the preview. There are many situations that will stop Xcode from continuously updating the preview and you can click this button to resume.
The left column shows a list of files in this project. Select the ContentView.swift
file. This file contains the Swift UI code that sets up the interface. You will see the code in the center pane. The relevant part of the code is:
struct ContentView: View {
var body: some View {
Text("Hello, World!")
.frame(maxWidth: .infinity, maxHeight: .infinity)
}
}
The ContentView
contains a body
which contains a Text
element with the text Hello World!
. at the end of the Text
object, you a ‘modifier’ that sets the frame of the Text object to use all available space.
Hello, me!
In the code pane, change World
to something else:
Text("Hello, Armin!")
You will see that the text in the preview pane updates as you change the code.
Note: there are several reasons Xcode might stop updating and when it does, you will have to hit the ‘Resume’ button above the preview pane.
The preview pane or canvas allows you to edit as well. When you (command) ⌘-click on the “Hello, …” text in the canvas, you will get an “Action menu.”
The first item in the action menu: “Show SwiftUI Inspector” will an inspector with the attributes or “modifiers” of the text object. Note that, even though there is no visual indication, the inspector view can be scrolled to reveal more options.
Change the font of the text to Body
. The preview in the canvas will update, as well as the code. The code will now look like:
Text("Hello, Armin!")
.font(.body)
.frame(maxWidth: .infinity, maxHeight: .infinity)
I have broken the modifiers into their own lines for clarity.
Stacking it up… or down… or sideways…
The SwiftUI body can only contain one object. But there are SwiftUI objects that you can use to group multiple objects together. Command-Click on the text and choose “Embed in VStack” from the action menu.
The code will change to:
VStack {
Text("Hello, Armin!")
.font(.body)
.frame(maxWidth: .infinity, maxHeight: .infinity)
}
The VStack
stands for ‘vertical stack.’ The VStack
and its sibling the HStack
(horizontal) can contain multiple objects. So, you can add another Text
over our “Hello” text:
VStack {
Text("SayThis")
.font(.largeTitle)
Text("Hello, Armin!")
.font(.body)
}.frame(maxWidth: .infinity, maxHeight: .infinity)
This adds the "SayThis"
text above the "Hello, ..."
text in a larger font. I have also moved the .frame(...)
modifier to the VStack
because that leads to a nicer output. Feel free to apply the .frame
modifer to different objects to see how that affects the layout.
The text is a little bit close together, so we can add some .padding()
modifiers:
VStack {
Text("SayThis")
.font(.largeTitle)
.padding()
Text("Hello, Armin!")
.font(.body)
.padding()
}.frame(maxWidth: .infinity, maxHeight: .infinity)
You can choose to change the properties using the inspector UI or in the code. Either way the changes should be reflected in the canvas and the code.
Adding some interaction…
Now we want to add some interaction. Eventually, we want the user to be able to enter some text, which will be sent to the say
command. Before we get, we will add a field, to enter some text.
To add a TextField
where the user can enter text to the layout, click on the +
icon in the top right of the tool bar. A window will appear with SwiftUI objects. Enter TextField
in the search area to find the TextField
object and drag it between the two text items.
You will want to add a .padding()
modifier to the text field as well.
We also need a local variable in our ContentView
to store the value of the TextField
. Add this variable declaration under the struct ContentView
definition and before the body
declaration:
@State var message = "Hello, World!"
This adds and initializes a variable named message
. The @State
marker tells SwiftUI this ‘State’ variable will be used with the view. The variable is part of the ‘state’ of the view. Changes to the variable will immediately update the UI and vice versa.
Use the message
variable for the TextField
and Text
objects:
@State var message = "Hello, World!"
var body: some View {
VStack {
Text("SayThis")
.font(.largeTitle)
.padding()
TextField("Message", text: $message)
.padding()
Text(message)
.font(.body)
.padding()
}.frame(maxWidth: .infinity, maxHeight: .infinity)
}
When you look at this interface in the preview pane, you will see that the contents of the message
variable are reflected in the TextField
and Text
. To test the interactivity, you need to either ‘build and run’ the application or hit the ‘Play’ button in the preview. Then you can change the text in the text field and immediately see the changes displayed in the Text below.
Declaring the message
variable as a @State
will automatically set up all the notifications for these seamless updates.
Buttons and Actions
While updating another object is quite cool, it is not what we set out to do. Let’s get back on track.
- Remove the last
Text(message)
and all its modifiers. - Command-click on the TextField and use the action menu to embed it in an
HStack
(horizontal stack) - Drag a
Button
from the object Library (you can open this with the ‘+’ button in the top right of the window) next to the text field
The code you generated should look like this:
HStack {
TextField("Message", text: $message)
.padding()
Button(action: {}) {
Text("Button")
}
}
You can change the label of the button by changing the Text
object inside of it. Change "Button"
to "Say"
. You also want to add a .padding()
modifier to the button.
The preview should now look like this:
The action
of the Button
is a code block that will be run when the button is clicked. Add the code between the action closure brackets:
Button(action: {
let executableURL = URL(fileURLWithPath: "/usr/bin/say")
try! Process.run(executableURL,
arguments: [self.message],
terminationHandler: nil)
}) {
Text("Say")
}.padding(.trailing)
This uses the run()
convenience method of the Process
class. You need to provide the full path to the command line tool. You can determine the full path in Terminal with the which
command:
> which say
/usr/bin/say
The arguments to the say
command need to be provided as an Array
of String
s. The process is launched asynchronously, so the main thread continues immediately. We can also provide a code block that will be executed when the Process
terminates. For now, we have no code that needs to be run on termination, so we set the terminationHandler
to nil
.
Note: using the
try!
statement to crash when this method throws an exception is lazy. I use it here to simplify the code drastically.Process.run()
willthrow
an error when the file atexectableURL
does not exist or is not executable. We can be fairly certain the/usr/bin/say
executable exists, so thetry!
statement is justifiable. You should probably add proper error prevention and handling in production code.
Updating the Interface
The say
command is executed asynchronously. This means that it will be running in the background and our user interface remains responsive. However, you may want to provide some feedback to the user while the process is running. One way of doing that, is to disable the ‘Say’ button while the process is running. Then you can re-enable it when the process is done.
Add a second @State
variable to track whether our process is running:
@State var isRunning = false
Add a .disabled(isRunning)
modifier to the Button
. This will disable the button when the value of the isRunning
variable changes to true
.
Then we add a line in the Button
action code to set that variable to true
. We also add a code block to the terminationHandler
which sets its value back to false
:
Button(action: {
let executableURL = URL(fileURLWithPath: "/usr/bin/say")
self.isRunning = true
try! Process.run(executableURL,
arguments: [self.message],
terminationHandler: { _ in self.isRunning = false })
}) {
Text("Say")
}.disabled(isRunning)
.padding(.trailing)
Now, when you press the button, it will disable until the say
process is done speaking.
Sample Code
For your convenience, here is the finished code for the ContentView.swift
class:
//
// ContentView.swift
// SayThis
//
import SwiftUI
struct ContentView: View {
@State var message = "Hello, World!"
@State var isRunning = false
var body: some View {
VStack {
Text("SayThis")
.font(.largeTitle)
.padding()
HStack {
TextField("Message", text: $message)
.padding(.leading)
Button(action: {
let executableURL = URL(fileURLWithPath: "/usr/bin/say")
self.isRunning = true
try! Process.run(executableURL,
arguments: [self.message],
terminationHandler: { _ in self.isRunning = false })
}) {
Text("Say")
}.disabled(isRunning)
.padding(.trailing)
}
}.frame(maxWidth: .infinity, maxHeight: .infinity)
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}
More Tutorials
If you want to learn more about SwiftUI and how it works, Apple has some excellent Tutorials on their developer page:
- Apple Developer: SwiftUI Tutorials
Update 2023-08-16: nearly five years later, I wrote a second part to this tutorial.