I'm attempting to run a Swift script using the command line from my Swift-based Mac app.
I have the following class method, which takes in arguments, and runs the commands:
func shell(_ args: String...) -> Int32 { let task = Process() task.launchPath = "/usr/bin/env" task.arguments = args task.launch() task.waitUntilExit() return task.terminationStatus } I'm able to execute commands such as the following successfully:
shell("pwd") shell("ls") shell("swift") pwd returns all the files in my app's build directory, as expected. This includes a hello.swift file that I manually added, which just prints "Hello, world!". Additionally, running swift does grant access to the Swift environment.
What I'm not having luck with is running commands such as:
shell("swiftc hello.swift")
Instead, I get the following error:
env: swiftc hello.swift: No such file or directory
It looks as though I'm facing a similar situation as these posts:
Swift Process - execute command error
Running shell commands in Swift
But, I'm not sure I fully understand all the implications therein for my specific situation.