Command line "launch path not accessible"

Recently I encounter a similar problem, but my solution is different.

I fix the problem by changing the permission mode of the script. Specifically, chmod 777 xxx. The key is to give the execution permission.

Let's conduct a controlled experiment to verify it:

  1. prepare a script with path /tmp/a.sh and permission 666

    The content of /tmp/a.sh:

#!/bin/sh
echo aaa
  1. prepare a swift script to launch the script:
import Foundation

let fileManager = FileManager.default
let path = fileManager.currentDirectoryPath

func shell(launchPath: String, arguments: [String] = []) -> NSString? {

    let task = Process()
    task.launchPath = launchPath
    task.arguments = arguments

    let pipe = Pipe()
    task.standardOutput = pipe
    task.launch()

    let data = pipe.fileHandleForReading.readDataToEndOfFile()
    let output = NSString(data: data, encoding: String.Encoding.utf8.rawValue)

    return output
}

if let output = shell(launchPath: "/tmp/a.sh", arguments: []) {
    print(output)
}

The output is:

... *** Terminating app due to uncaught exception 'NSInvalidArgumentException', 
reason: 'launch path not accessible'
  1. change the permission of /tmp/a.sh to 777 (by granting the execution permission) and then re-run the swift script:
aaa

I think the problem is that you actually want to execute the shell and have it execute the mdtool, rather than directly execute mdtool

Try passing "/bin/bash" as the launchpath, and then include the path to mdtool as part of the argument string.