Tech Reflect
  • Home
  • About This Site
  • Contact me
  • Search Icon
Creating command-line tool in Swift

Creating command-line tool in Swift

2017-03-28

This question comes up often on Swift mailing lists. Once you learn Swift, you’ll get addicted and want to use it everywhere. It’s not quite as straightforward writing a script in Swift as it is in python or bash. However, there are lots of benefits.

  • It’s a great entry point into learning Swift for those that typically write scripts rather than applications
  • You want to execute a bunch of shell commands, but you can use Swift’s rich data structures, straightforward programming language, and the huge Cocoa API.

Here’s what you can do:

  • Launch Xcode
  • File > New > ProjectChoose macOS
  • Click on Command Line Tool
  • Name it and save the project

  • Click on main.swift in the sidebar
  • Replace the Hello, World line with this code.
import Foundation

struct Shell {
func stripWhitespace(_ inString: String) -> String {
return inString.trimmingCharacters(in: CharacterSet.whitespacesAndNewlines)
}

func executeCommand(_ command:String, echo: Bool = true) -> String {
let process = Process(); let pipe = Pipe()
(process.standardError, process.standardOutput) = (pipe, pipe)
process.launchPath = "/bin/sh"
process.arguments = ["-c", command]
process.launch()
let handle = pipe.fileHandleForReading
var availableData: Data
var returnString = ""
while true {
availableData = handle.availableData
if availableData.count == 0 { break }
if let string = String(data: availableData, encoding: .utf8) {
if echo == true { print(string, terminator: "") }
returnString = returnString + string
}
}
return stripWhitespace(returnString)
}
}

// example code
var result = Shell().executeCommand("ls")
result = Shell().executeCommand("ls ~", echo: false)

At the bottom are two example lines of code to show how to call a shell command. This is similar to how things work in python with the major exception being that it’s not python.

Extra credit

Create a command-line project in the same way described above. Save this project as a template to be used later. You can then just duplicate the project and then easily just start writing a script.


geeky, macOS tips

Post navigation

NEXT
Doing something with the “find” command
PREVIOUS
How to use dogfood testing
Comments are closed.

Get Monthly Updates

Recent Posts

  • Inserting random email sigs in Mail on iOS
  • Keep Instagram open to finish posting…
  • How I predicted the rise of Twitter, barely used it, and amassed 35,000 followers
  • Apple Books 2022, in pictures
  • Killing one bird with two-and-a-half stones in Mac OS X Mail

Categories

  • analog (1)
  • apple career (12)
  • apple inside (19)
  • apple stories (20)
  • bertrand serlet (3)
  • bugs (3)
  • essays (15)
  • geeky (21)
  • interviews (4)
  • iOS tips (4)
  • Mac OS X (7)
  • macOS tips (36)
  • personal (8)
  • predictions (1)
  • products (5)
  • prototypes (6)
  • scott forstall (7)
  • scripting (2)
  • siri (2)
  • steve jobs (16)
  • tim cook (1)
  • workplace (15)

Get Monthly Updates

About cricket


Me with Guiness the owl

25 years in tech. I like to write manifestos. I like to offer interesting tips. I like making fun of things. Everyone copes differently.

My Other Blogs

  • Free Range Parrots
  • Plucky Tree (personal)
© 2025   All Rights Reserved.