How to include an external executable into a MacOS app?

933 Views Asked by At

I am building a simple app with a TextField input. I want to pass the input of the TextField to a cli tool I built with Golang.

This cli-tool is not sitting in /usr/bin but under ~/go/bin in my user directory. So, when I try something like the answer to this question it's not able to find the executable.

I tried copying the executable in every single folder of the Application but I am still getting the same error.

Where exactly am I supposed to copy the executable? What does the code from this looks like after I copied it over?

2

There are 2 best solutions below

0
gallivantingitalian On

For future reference this worked:

Step 1: embed the binary in the build (Adding resource files to xcode). To open the project tab just double click on your app root in XCode

Step 2: You will find the path to your executable like this:

let bundle = Bundle.main
let pathToExecutable = bundle.url(forResource: "myclitool", withExtension: "")

and then you can use Process to run it (e.g. How to launch an external Process?)

0
mcritz On

Apple provides documentation for this very thing.

Bouncing off @gallivantingitalian 's answer you may find the Bundle.main.url(forAuxiliaryExecutable:) command helpful if following Apple’s documentation.

let process = Process()
guard let url = Bundle.main.url(forAuxiliaryExecutable: "my_awesome_tool") else {
    throw AppErrors.appConfigError(reason: "my_awesome_tool tool not found")
}
process.executableURL = url