1

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?

  • @matt are you sure it's the right link? I don't see any mention about PATH there. The problem comes with executables outside of `/usr/bin`. If I pass in `/usr/bin/ls` for example everything is fine. But if I instead pass `/Users/John/go/bin/myclitool` that doesn't work – gallivantingitalian Apr 07 '22 at 19:47
  • Are you sandboxed? – matt Apr 07 '22 at 20:05
  • Yes, I am running sandboxed. I want to bundle the executable into the app – gallivantingitalian Apr 08 '22 at 07:54
  • You do? Then why are we talking about /usr/bin and things like that??? – matt Apr 08 '22 at 11:36
  • Because if I execute commands from `/usr/bin` or for instance `/bin/pwd`, no complains. But if I point to my local executable `/Users/John/bin/myclitool` it complains – gallivantingitalian Apr 08 '22 at 20:15
  • But if you're going to include the tool in your app bundle, it won't be in _any_ of those places. So who cares which of them it works in and which of them it doesn't? – matt Apr 08 '22 at 22:50

2 Answers2

1

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

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
mcritz
  • 709
  • 7
  • 14