Launch application with arguments with NSWorkspace not working
Asked Answered
F

1

6

I would like to open FileZilla (ftp application) with an argument.

It is working with NSTask (aka. Process):

let process:Process = Process()
process.launchPath = appURL.path
process.arguments = ["ftp://1.2.3.4"]
process.launch()

It is not working with NSWorkspace:

try! NSWorkspace.shared().launchApplication(
   at: appURL,
   options: .default,
   configuration: [NSWorkspaceLaunchConfigurationArguments:["ftp://1.2.3.4"]]
)

With not working I mean, that FileZilla launches, but the argument is not passed over.

The API seems simple, but I can not figure out what I am doing wrong here.

The reason why I want to use NSWorkspace is, that NSTask the application will not launch in foreground.

Formica answered 3/10, 2016 at 7:24 Comment(3)
Is the app sandboxed? NSWorkspaceLaunchConfigrationArguments isn't available to sandboxed apps developer.apple.com/reference/appkit/…Rarefy
Thanks, you are right. But why NSTask is not blocked, remains a mystery . :/ My workaround for now is to use NSTask to open the application with an argument and then use NWorkspace to bring the application the front. Does anyone know a better solution for a sandboxed app?Formica
I found the solution: #61618143Logjam
S
0

I am using a launchAgent to run a script that runs my app, I found that if we need to pass a configuration, we need to wait a little bit after executing the open function:

NSWorkspace.shared.open(appURL, configuration: configuration)
Thread.sleep(forTimeInterval: 1)
Sloat answered 13/1 at 22:17 Comment(2)
Hi Hadi, can you confirm that this works with a sandboxed app for the Mac App Store?Formica
I can't confirm that, my app isn't sandboxed.Sloat

© 2022 - 2024 — McMap. All rights reserved.