Skip to content

Instantly share code, notes, and snippets.

@scriptingosx
Last active August 14, 2023 07:41
Show Gist options
  • Save scriptingosx/99b947f0ca233bea400cafe8385c944d to your computer and use it in GitHub Desktop.
Save scriptingosx/99b947f0ca233bea400cafe8385c944d to your computer and use it in GitHub Desktop.
Process Sample Code
import Foundation
let process = Process()
process.launchPath = "/usr/bin/say"
process.arguments = ["-v", "?"]
let outPipe = Pipe()
process.standardOutput = outPipe
process.terminationHandler = { process in
let outData = outPipe.fileHandleForReading.readDataToEndOfFile()
let output = String(data: outData, encoding: .utf8) ?? ""
print(output)
let voices = parseVoices(output)
print(voices)
}
try? process.run()
process.waitUntilExit()
import Foundation
extension Process {
@discardableResult
static func launch(
path: String,
arguments: [String] = [],
terminationHandler: @escaping (Int, Data, Data) -> Void
) throws -> Process {
let process = Process()
let outPipe = Pipe()
let errorPipe = Pipe()
process.standardOutput = outPipe
process.standardError = errorPipe
process.arguments = arguments
process.launchPath = path
process.terminationHandler = { process in
let outData = outPipe.fileHandleForReading.readDataToEndOfFile()
let errorData = errorPipe.fileHandleForReading.readDataToEndOfFile()
let exitCode = Int(process.terminationStatus)
terminationHandler(exitCode, outData, errorData)
}
try process.run()
return process
}
}
let process = try? Process.launch(
path: "/usr/bin/say",
arguments: ["-v", "?"]
) { exitCode, outData, errData in
let output = String(data: outData, encoding: .utf8) ?? ""
print(output)
}
process?.waitUntilExit()
import Foundation
extension Process {
@discardableResult
static func launch(
path: String,
arguments: [String] = [],
terminationHandler: @escaping (Int, Data, Data) -> Void
) throws -> Process {
let process = Process()
let outPipe = Pipe()
let errorPipe = Pipe()
process.standardOutput = outPipe
process.standardError = errorPipe
process.arguments = arguments
process.launchPath = path
process.terminationHandler = { process in
let outData = outPipe.fileHandleForReading.readDataToEndOfFile()
let errorData = errorPipe.fileHandleForReading.readDataToEndOfFile()
let exitCode = Int(process.terminationStatus)
terminationHandler(exitCode, outData, errorData)
}
try process.run()
return process
}
static func launch(
path: String,
arguments: [String] = []
) async throws -> (exitCode: Int, standardOutput: Data, standardError: Data) {
try await withCheckedThrowingContinuation { continuation in
do {
try launch(path: path, arguments: arguments) { exitCode, outData, errData in
continuation.resume(returning: (exitCode, outData, errData))
}
} catch let error {
continuation.resume(throwing: error)
}
}
}
}
guard let (exitCode, outData, errData) = try? await Process.launch(
path: "/usr/bin/say",
arguments: ["-v", "?"]
) else { exit(0) }
let output = String(data: outData, encoding: .utf8) ?? ""
print(output)
func parseVoices(_ output: String) -> [String] {
output
.components(separatedBy: "\n")
.map {
$0
.components(separatedBy: "#")
.first?
.trimmingCharacters(in: .whitespaces)
.components(separatedBy: CharacterSet.whitespaces)
.dropLast()
.filter { !$0.isEmpty }
.joined(separator: " ")
?? ""
}
.filter { !$0.isEmpty }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment