Swift 5.2: Run external program from your SwiftUI application on macOS

Swift for the Slow
Mac O’Clock
Published in
1 min readMay 8, 2020

So, you want to run an external program (e.g., a shell program or script) from your SwiftUI macOS app, and a lot of the posts you’re seeing are from 30 B.C. So here’s something that worked for me on macOS, using Swift 5.2 and SwiftUI whatever the latest version is in 2020.

There are three key steps here:

  1. Use the Process Object to get your own task class
  2. Set its properties to prepare for launch
  3. Run it, and then capture the output of the program so you can, for example, show on your screen

Well, hope that brightened your day.

You know what’s awesome about Apple’s dev docs regarding executableURL — it’s wonderful, there’s no documentation!

Tagged: swift for the slow

--

--

Swift for the Slow
Mac O’Clock

Simple Swift 5+ / Swiftui 2.0 / macOS tips, utilities, sample code, some of which is used in https://powermanuscript.com