I have an expression like
xattr -p com.apple.metadata:_kMDItemUserTags '/Users/Shared/MeinDesktop/fasttemp/Fotos noch einzusortieren/_ORG/Foto.jpg' | xxd -r -p | plutil -convert json - -o -
I want to execute it in my app. So I tried
func readLabels(_ filePath : String) -> String {
return shell(launchPath: "/usr/bin/xattr", arguments: ["-p", "com.apple.metadata:_kMDItemUserTags", filePath, " | xxd -r -p | plutil -convert json - -o -" ] )
}
with my helping function to execute a command as:
func shell(launchPath: String, arguments: [String]) -> String
{
let task = Process()
task.launchPath = launchPath
task.arguments = arguments
let pipe = Pipe()
task.standardOutput = pipe
task.standardError = pipe
do {
try task.run()
} catch let error as NSError {
print(error.localizedDescription)
return ""
}
let data = pipe.fileHandleForReading.readDataToEndOfFile()
let output: String = String(data: data, encoding: .utf8) ?? ""
return output
}
This works nice with the first parts of arguments until the pipe symbol. With additional params the error is:
xattr: No such file: | xxd -r -p | plutil -convert json - -o -
What do I need to do, to make this an entire statement to be executed? I don't want to create a command file, execute it, and then delete it again. It should work as all "normal" commands and deliver an return text from std io, which will be taken by my shell function.
I'm using Xcode 12.4 and macOS 11.2.