In our program, there is a function that allows the user to select an other application to open our files.
We use openURLs to call other application and pass the file paths to it.
Since we don't know which way the user selected to receive the open file paths,
we are using the following method to call the user’s application.
NSDictionary *config = @{NSWorkspaceLaunchConfigurationArguments: filePaths};
NSMutableArray *urls = fileURLs of filePaths ;
NSRunningApplication *app = [[NSWorkspace sharedWorkspace] openURLs:urls
withApplicationAtURL:[NSURL fileURLWithPath:appPath]
options:NSWorkspaceLaunchDefault
configuration:config
error:nil];
In the receiving application, as we know, there are three main receiving methods to obtain the open file paths.
In the above calling way, the passed params(files) can be received by following methods.
Method 1.NSApplication delegate method
- (void)application:(NSApplication *)sender openFiles:(NSArray *)filenames
——————————————————————————
Method 2. AppleEvent
[[NSAppleEventManager sharedAppleEventManager] setEventHandler:self
andSelector:@selector(handleAppleEvent:withReplyEvent:)
forEventClass:(AEEventClass)kCoreEventClass
andEventID:(AEEventID)kAEOpenDocuments];
——————————————————————————
Method 3. Launch arguments of ProcessInfo
ProcessInfo.processInfo.arguments
However, if the receiving program use the NSApplication delegate(Method 1) to receive open file paths,
on MacOS 11.0, the delegate method will be called twice (the previous system only called once),
and some exceptions will occur(Method 2 and Method 3 is only called once, and no problem).
We have tried the following way to open the files, by not passing the ‘configuration' parameter(pass an empty dictionary).
And Method1 was called only once, problem is solved. method 2 is also OK. But, method 3 can not receive the open paths.
It seems that ‘configuration' must also be passed?
//NSDictionary *config = @{NSWorkspaceLaunchConfigurationArguments: filePaths};
NSMutableArray *urls = filePaths对应的fileURLs;
NSRunningApplication *app = [[NSWorkspace sharedWorkspace] openURLs:urls
withApplicationAtURL:[NSURL fileURLWithPath:appPath]
options:NSWorkspaceLaunchDefault
configuration:@{} <—— pass an empty dic
error:nil];
Because the user may select any applications (including some application of the system, preview, etc.),
we can not determine how some programs are received or modify the receiving methods.
So NSApplication delegate method( openFiles) called twice is a new OS problem, isn’t it?
Any other advice for us?
We use openURLs to call other application and pass the file paths to it.
Since we don't know which way the user selected to receive the open file paths,
we are using the following method to call the user’s application.
NSDictionary *config = @{NSWorkspaceLaunchConfigurationArguments: filePaths};
NSMutableArray *urls = fileURLs of filePaths ;
NSRunningApplication *app = [[NSWorkspace sharedWorkspace] openURLs:urls
withApplicationAtURL:[NSURL fileURLWithPath:appPath]
options:NSWorkspaceLaunchDefault
configuration:config
error:nil];
In the receiving application, as we know, there are three main receiving methods to obtain the open file paths.
In the above calling way, the passed params(files) can be received by following methods.
Method 1.NSApplication delegate method
- (void)application:(NSApplication *)sender openFiles:(NSArray *)filenames
——————————————————————————
Method 2. AppleEvent
[[NSAppleEventManager sharedAppleEventManager] setEventHandler:self
andSelector:@selector(handleAppleEvent:withReplyEvent:)
forEventClass:(AEEventClass)kCoreEventClass
andEventID:(AEEventID)kAEOpenDocuments];
——————————————————————————
Method 3. Launch arguments of ProcessInfo
ProcessInfo.processInfo.arguments
However, if the receiving program use the NSApplication delegate(Method 1) to receive open file paths,
on MacOS 11.0, the delegate method will be called twice (the previous system only called once),
and some exceptions will occur(Method 2 and Method 3 is only called once, and no problem).
We have tried the following way to open the files, by not passing the ‘configuration' parameter(pass an empty dictionary).
And Method1 was called only once, problem is solved. method 2 is also OK. But, method 3 can not receive the open paths.
It seems that ‘configuration' must also be passed?
//NSDictionary *config = @{NSWorkspaceLaunchConfigurationArguments: filePaths};
NSMutableArray *urls = filePaths对应的fileURLs;
NSRunningApplication *app = [[NSWorkspace sharedWorkspace] openURLs:urls
withApplicationAtURL:[NSURL fileURLWithPath:appPath]
options:NSWorkspaceLaunchDefault
configuration:@{} <—— pass an empty dic
error:nil];
Because the user may select any applications (including some application of the system, preview, etc.),
we can not determine how some programs are received or modify the receiving methods.
So NSApplication delegate method( openFiles) called twice is a new OS problem, isn’t it?
Any other advice for us?