Re: Launching another app with command line arguments
Re: Launching another app with command line arguments
- Subject: Re: Launching another app with command line arguments
- From: Gabriel Zachmann via Cocoa-dev <email@hidden>
- Date: Sun, 21 Jun 2020 12:25:13 +0200
Thanks a lot.
I tried this in my app A , but I keep getting the error message
launch path not accessible
together with a stack trace.
I am getting the url to my app B like this:
url_ = NSWorkspace.shared.urlForApplication( withBundleIdentifier:
"de.zach.AppB" )
which points to /private/tmp/Debug/AppB.app
(which is correct ).
I thought, maybe, because of sandboxing, I have to ask the user to open the
path to app B , so that I get the permission.
So, when launching the sub-process, I tried this:
let process = Process()
process.standardOutput = FileHandle.nullDevice
process.standardError = FileHandle.nullDevice
let oPanel = NSOpenPanel()
oPanel.allowsMultipleSelection = false
oPanel.canCreateDirectories = false
oPanel.canChooseFiles = true
oPanel.directoryURL = url_
let result = oPanel.runModal()
let path_to_exec = oPanel.url!.path
NSLog( "path_to_exec = %@", path_to_exec )
process.launchPath = path_to_exec
process.arguments = [""]
process.launch()
In the OpenPanel, I navigate to the executable in .../Contents/MacOS/BApp.
It prints the correct path_to_exec, but the launch() fails.
I have also tried this variation:
oPanel.directoryURL = url_
let result = oPanel.runModal()
let path_to_exec = oPanel.url!.path + "/Contents/MacOS/ArtSaverApp"
where I don't navigate into the app bundle, but let app A append the sub-path
to the executable. Same result ("not accessible").
I have set the Capability "User Selected File" to read&write in the App Sandbox
in Xcode, but that did not change the outcome.
I also tried to launch app B by executing a shell command in app A, like this:
process.launchPath = "/bin/bash"
process.arguments = ["-c", "open", url_!.path, "-f"]
process.launch()
But that did nothing. No app B appeared, and no error message in app A.
> On 21. Jun 2020, at 02:04, Marco S Hyman <email@hidden> wrote:
>
>
>
>> On Jun 20, 2020, at 4:31 PM, Gabriel Zachmann <email@hidden> wrote:
>>
>>>
>>> If app B can be treated as a sub-process of app A you can use Process. I
>>> know that argument passing works with Process.
>>
>> Sounds good. How can A launch B as its sub-process? (and pass command line
>> arguments?)
>>
>>
>
> let process = Process()
> process.standardOutput = FileHandle.nullDevice
> process.standardError = FileHandle.nullDevice
> process.launchPath = url.path // path to app
> process.arguments = [“arg1”, “arg2”, “arg3”]
> process.launch()
>
> and if you need to
>
> process.waitUntilExit()
>
> Obviously don’t wait on the main thread!
>
> I do this in an app where the URL points to a helper located in the bundle.
> Not sure what extra hoops you’d need to jump through to launch an app outside
> of the sandbox. Probably needs user approval which can be remembered using
> a security scoped bookmark.
Attachment:
smime.p7s
Description: S/MIME cryptographic signature
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden