changeset 2152:d299b5acc507

Mac: Additional dw_exec() code to locate the full path to application URLs. On Catalina and later dw_exec() now supports specifying the program by application bundle identifier, in addition to the application name. Check if the selectors are available and call them directly to support their eventual removal and to avoid deprecation warnings.
author bsmith@81767d24-ef19-dc11-ae90-00e081727c95
date Wed, 16 Sep 2020 10:19:58 +0000
parents 467401a1ca69
children 9c1a64ebb686
files mac/dw.m
diffstat 1 files changed, 55 insertions(+), 10 deletions(-) [+]
line wrap: on
line diff
--- a/mac/dw.m	Fri Sep 11 02:56:41 2020 +0000
+++ b/mac/dw.m	Wed Sep 16 10:19:58 2020 +0000
@@ -12408,6 +12408,30 @@
    return (DWTID)pthread_self();
 }
 
+#ifdef BUILDING_FOR_CATALINA
+NSURL *_dw_url_from_program(NSString *nsprogram, NSWorkspace *ws)
+{
+    NSURL *retval = [ws URLForApplicationWithBundleIdentifier:nsprogram];
+    
+    if(!retval)
+    {
+        SEL sfpfa = NSSelectorFromString(@"fullPathForApplication");
+
+        if([ws respondsToSelector:sfpfa])
+        {
+            DWIMP ifpfa = (DWIMP)[ws methodForSelector:sfpfa];
+            NSString *apppath = ifpfa(ws, sfpfa, nsprogram);
+            
+            if(apppath)
+                retval = [NSURL fileURLWithPath:apppath];
+        }
+        if(!retval)
+            retval = [NSURL fileURLWithPath:nsprogram];
+    }
+    return retval;
+}
+#endif
+
 /*
  * Execute and external program in a seperate session.
  * Parameters:
@@ -12424,7 +12448,8 @@
     if(type == DW_EXEC_GUI)
     {
         NSString *nsprogram = [NSString stringWithUTF8String:program];
-        
+        NSWorkspace *ws = [NSWorkspace sharedWorkspace];
+
         if(params && params[0] && params[1])
         {
             NSString *file = [NSString stringWithUTF8String:params[1]];
@@ -12432,34 +12457,54 @@
 #ifdef BUILDING_FOR_CATALINA
             if(@available(macOS 10.15, *))
             {
-                NSURL *url = [NSURL fileURLWithPath:nsprogram];
+                NSURL *url = _dw_url_from_program(nsprogram, ws);
                 NSURL *nsfile = [NSURL fileURLWithPath:file];
                 
-                [[NSWorkspace sharedWorkspace] openURLs:[[NSArray alloc] initWithObjects:nsfile, nil]
+                [ws openURLs:[NSArray arrayWithObjects:nsfile, nil]
                                     withApplicationAtURL:url
                                            configuration:[NSWorkspaceOpenConfiguration configuration]
                                        completionHandler:^(NSRunningApplication *app, NSError *error) {
+                    if(error)
+                        NSLog(@"openURLs: %@", [error localizedDescription]);
                 }];
             }
             else
 #endif
-                [[NSWorkspace sharedWorkspace] openFile:file withApplication:nsprogram];
+            {
+                SEL sofwa = NSSelectorFromString(@"openFile:withApplication:");
+
+                if([ws respondsToSelector:sofwa])
+                {
+                    DWIMP iofwa = (DWIMP)[ws methodForSelector:sofwa];
+                    iofwa(ws, sofwa, file, nsprogram);
+                }
+            }
         }
         else
         {
 #ifdef BUILDING_FOR_CATALINA
             if(@available(macOS 10.15, *))
             {
-                NSURL *url = [NSURL fileURLWithPath:nsprogram];
-
-                [[NSWorkspace sharedWorkspace] openApplicationAtURL:url
-                                                      configuration:[NSWorkspaceOpenConfiguration configuration]
-                                                  completionHandler:^(NSRunningApplication *app, NSError *error) {
+                NSURL *url = _dw_url_from_program(nsprogram, ws);
+
+                [ws openApplicationAtURL:url
+                           configuration:[NSWorkspaceOpenConfiguration configuration]
+                       completionHandler:^(NSRunningApplication *app, NSError *error) {
+                    if(error)
+                        NSLog(@"openApplicationAtURL: %@", [error localizedDescription]);
                 }];
             }
             else
 #endif
-                [[NSWorkspace sharedWorkspace] launchApplication:nsprogram];
+            {
+                SEL sla = NSSelectorFromString(@"launchApplication");
+
+                if([ws respondsToSelector:sla])
+                {
+                    DWIMP ila = (DWIMP)[ws methodForSelector:sla];
+                    ila(ws, sla, nsprogram);
+                }
+            }
         }
         return DW_ERROR_NONE;
     }