0

I'm trying to write a small app to start/stop and display data from a command line "app" someone else wrote. The command executable is installed in '/usr/local/bin'. It outputs status text data to standardOutput while running. I can execute this "command" from the Terminal.app without issue. From swiftUI code I can successfully execute "built-in" commands like ls. However, when (in swiftUI code) I attempt to execute Process.run WITH the new command it throws the exception 'The file “” doesn’t exist.'

Anyone have any ideas? Thanks in advance!

Here's a code snip:

  // NOTE: "installedCommand" is just a placeholder for the actual command.
  let task = Process()
  let connection = Pipe()

  let exeUrl = URL(fileURLWithPath: "/usr/local/bin/installedCommand")
  //let exeUrl = URL(fileURLWithPath: "/bin/ls") <--works fine

  task.executableURL = exeUrl
  task.standardOutput = connection

  do
  {
      try task.run()
  }
  catch
  {
      print("Error: \(error.localizedDescription)")
      return
  }
  • if you are using Xcode for the project then you can see the executable under the folder `Products`. Right click to show in Finder and now you have the location of the executable. Now use this location in your code. Or you could copy the file to `/usr/local/bin/`. – user1046037 Apr 21 '20 at 01:23
  • Thanks for the response. The "installedCommand" is a 3rd party program that was installed in /usr/local/bin by a package installer. My xcode project is attempting to run it. I don't have access to the original project. Or am I misunderstanding? – ErikTheHack Apr 21 '20 at 16:19
  • Instead of setting `task.executableURL` can you type `task.arguments = ["-c", command]` where command is a String that you would like to execute. Let the command string be the same as the command you executed on the terminal – user1046037 Apr 21 '20 at 16:30
  • I'm confused. What command should I be 'run'ing that will take the -c argument? – ErikTheHack Apr 21 '20 at 16:41
  • Oh you mean sh -- Pretty sneaky idea. It works from Terminal. Will try in code. – ErikTheHack Apr 21 '20 at 16:47
  • Well, progress. Now I get '/bin/sh: /usr/local/bin/installedCommand: Operation not permitted'. Again, I can execute '/bin/sh -c /usr/local/bin/installedCommand' successfully in Terminal. Any ideas? Does my app need special permissions or something? – ErikTheHack Apr 21 '20 at 17:00
  • Looks like a permission issue. try this `task.arguments = ["-c", "who"]` This would tell the user that is being used. Check the output to see if it matches the user you are using – user1046037 Apr 21 '20 at 17:01
  • Yes, your app needs permission to access the disk. Refer: https://stackoverflow.com/questions/55856496/how-do-i-enable-full-disk-access-for-a-macos-app-being-built-and-debugged-in-xco. The question contain some details about archiving the app then using granting the app full disk access. Give it a try. Alternatively if it was a command line app you wouldn't have this trouble. – user1046037 Apr 21 '20 at 17:20
  • To execute a command ls, you should do `task.arguments = ["-c", "ls"]` – user1046037 Apr 21 '20 at 17:22
  • Actually, I found the fix for this issue [here](https://stackoverflow.com/questions/49544566/operation-not-permitted-when-executing-killall-with-swift). I just had to update the "App Sandbox" to "NO" in the .entitlements plist in the project. It's all working now! Thanks @user1046037 for your help. – ErikTheHack Apr 21 '20 at 17:35

0 Answers0