[macOS] argument passing not working on openApplication API(NSWorkspace)


Code Block
// pass arguments Not Working
let url = NSURL(fileURLWithPath:
"/Volumes/DATA/Dev/cppPlayground/cmake-build-debug/cppPlayground"
, isDirectory: false) as URL
let configuration = NSWorkspace.OpenConfiguration()
var aStr = [String]()
aStr.append("1st")
aStr.append("2nd")
configuration.arguments = aStr
NSWorkspace.shared.openApplication(at: url,
configuration: configuration,
completionHandler: nil)

https://developer.apple.com/documentation/appkit/nsworkspace/3172700-openapplication

I try to open another cpp app that has main method ' int main(int argc, char **argv) '
and pass arguments.
but it was failed.
app was open successful but arguments was empty.


Code Block
// Working But deprecated.
let task = Process()
task.arguments = ["test test", "2nd Argument", "3rd"]
task.launchPath = "/Volumes/DATA/Dev/cppPlayground/cmake-build-debug/cppPlayground"
task.launch()

this code was work. but available macOS 10.0–10.13.

is there something wrong?
Answered by OOPer in 627106022
I do not know much about NSWorkspace.
But there are some replacements for the deprecated methods:
Code Block
let task = Process()
task.arguments = ["test test", "2nd Argument", "3rd"]
task.executableURL = URL(fileURLWithPath: "/Volumes/DATA/Dev/cppPlayground/cmake-build-debug/cppPlayground")
do {
try task.run()
} catch {
print(error)
}



Accepted Answer
I do not know much about NSWorkspace.
But there are some replacements for the deprecated methods:
Code Block
let task = Process()
task.arguments = ["test test", "2nd Argument", "3rd"]
task.executableURL = URL(fileURLWithPath: "/Volumes/DATA/Dev/cppPlayground/cmake-build-debug/cppPlayground")
do {
try task.run()
} catch {
print(error)
}



thanks a lot.

could use .executableURL, .run() instead of launch()

Discussion


The default value of this property is an empty array. When launching a new instance of an app, use this property to specify any additional launch arguments. The system inserts the app's path as the first element in the array. 

If the calling process is sandboxed, the system ignores the value of this property.

https://developer.apple.com/documentation/appkit/nsworkspace/openconfiguration/3172708-arguments
[macOS] argument passing not working on openApplication API(NSWorkspace)
 
 
Q