comparison mac/dw.m @ 2350:5ddc447fb367

Mac: Partial rewrite of dw_exect(). Implement DW_EXEC_CON with Terminal.app. Previously we had been using xterm, which hasn't been included MacOS since 10.7 (Lion). Also, rewrite the DW_EXEC_GUI to allow more than one file to open.
author bsmith@81767d24-ef19-dc11-ae90-00e081727c95
date Thu, 04 Mar 2021 16:33:55 +0000
parents 46e60dca8285
children 0c8d769df97e
comparison
equal deleted inserted replaced
2349:123e22827a82 2350:5ddc447fb367
12754 NSString *nsprogram = [NSString stringWithUTF8String:program]; 12754 NSString *nsprogram = [NSString stringWithUTF8String:program];
12755 NSWorkspace *ws = [NSWorkspace sharedWorkspace]; 12755 NSWorkspace *ws = [NSWorkspace sharedWorkspace];
12756 12756
12757 if(params && params[0] && params[1]) 12757 if(params && params[0] && params[1])
12758 { 12758 {
12759 NSString *file = [NSString stringWithUTF8String:params[1]];
12760
12761 #ifdef BUILDING_FOR_CATALINA 12759 #ifdef BUILDING_FOR_CATALINA
12762 if(@available(macOS 10.15, *)) 12760 if(@available(macOS 10.15, *))
12763 { 12761 {
12764 NSURL *url = _dw_url_from_program(nsprogram, ws); 12762 NSURL *url = _dw_url_from_program(nsprogram, ws);
12765 NSURL *nsfile = [NSURL fileURLWithPath:file]; 12763 NSMutableArray *array = [[NSMutableArray alloc] init];
12766 12764 int z = 1;
12767 [ws openURLs:[NSArray arrayWithObjects:nsfile, nil] 12765
12768 withApplicationAtURL:url 12766 while(params[z])
12769 configuration:[NSWorkspaceOpenConfiguration configuration] 12767 {
12770 completionHandler:^(NSRunningApplication *app, NSError *error) { 12768 NSString *thisfile = [NSString stringWithUTF8String:params[z]];
12769 NSURL *nsfile = [NSURL fileURLWithPath:thisfile];
12770
12771 [array addObject:nsfile];
12772 z++;
12773 }
12774
12775 [ws openURLs:array withApplicationAtURL:url
12776 configuration:[NSWorkspaceOpenConfiguration configuration]
12777 completionHandler:^(NSRunningApplication *app, NSError *error) {
12771 if(error) 12778 if(error)
12772 NSLog(@"openURLs: %@", [error localizedDescription]); 12779 NSLog(@"openURLs: %@", [error localizedDescription]);
12773 }]; 12780 }];
12774 } 12781 }
12775 else 12782 else
12777 { 12784 {
12778 SEL sofwa = NSSelectorFromString(@"openFile:withApplication:"); 12785 SEL sofwa = NSSelectorFromString(@"openFile:withApplication:");
12779 12786
12780 if([ws respondsToSelector:sofwa]) 12787 if([ws respondsToSelector:sofwa])
12781 { 12788 {
12782 DWIMP iofwa = (DWIMP)[ws methodForSelector:sofwa]; 12789 int z = 1;
12783 iofwa(ws, sofwa, file, nsprogram); 12790
12791 while(params[z])
12792 {
12793 NSString *file = [NSString stringWithUTF8String:params[z]];
12794 DWIMP iofwa = (DWIMP)[ws methodForSelector:sofwa];
12795
12796 iofwa(ws, sofwa, file, nsprogram);
12797 z++;
12798 }
12784 } 12799 }
12785 } 12800 }
12786 } 12801 }
12787 else 12802 else
12788 { 12803 {
12808 DWIMP ila = (DWIMP)[ws methodForSelector:sla]; 12823 DWIMP ila = (DWIMP)[ws methodForSelector:sla];
12809 ila(ws, sla, nsprogram); 12824 ila(ws, sla, nsprogram);
12810 } 12825 }
12811 } 12826 }
12812 } 12827 }
12813 return DW_ERROR_NONE; 12828 ret = DW_ERROR_NONE;
12814 } 12829 }
12815 12830 else
12816 if((ret = fork()) == 0) 12831 {
12817 { 12832 /* Use AppleScript to Open Terminal.app or contact an existing copy,
12818 int i; 12833 * and execute the command passed in params[].
12819 12834 */
12820 for (i = 3; i < 256; i++) 12835 char *commandline;
12821 close(i); 12836 char *format = "osascript -e \'tell app \"Terminal\" to do script \"";
12822 setsid(); 12837 int len = (int)strlen(format) + 3;
12823 12838
12824 if(type == DW_EXEC_CON) 12839 /* Generate a command line from the parameters */
12825 { 12840 if(params && *params)
12826 char **tmpargs; 12841 {
12827 12842 int z = 0;
12828 if(!params) 12843
12844 while(params[z])
12829 { 12845 {
12830 tmpargs = malloc(sizeof(char *)); 12846 len+=strlen(params[z]) + 1;
12831 tmpargs[0] = NULL; 12847 z++;
12832 } 12848 }
12833 else 12849 z=1;
12850 commandline = calloc(1, len);
12851 strcpy(commandline, format);
12852 strcat(commandline, params[0]);
12853 while(params[z])
12834 { 12854 {
12835 int z = 0; 12855 strcat(commandline, " ");
12836 12856 strcat(commandline, params[z]);
12837 while(params[z]) 12857 z++;
12838 {
12839 z++;
12840 }
12841 tmpargs = malloc(sizeof(char *)*(z+3));
12842 z=0;
12843 tmpargs[0] = "xterm";
12844 tmpargs[1] = "-e";
12845 while(params[z])
12846 {
12847 tmpargs[z+2] = params[z];
12848 z++;
12849 }
12850 tmpargs[z+2] = NULL;
12851 } 12858 }
12852 execvp("xterm", tmpargs); 12859 }
12853 free(tmpargs); 12860 else
12854 } 12861 {
12855 /* If we got here exec failed */ 12862 len += strlen(program);
12856 _exit(-1); 12863 commandline = calloc(1, len);
12864 strcpy(commandline, format);
12865 strcat(commandline, program);
12866 }
12867 strcat(commandline, "\"\'");
12868
12869 /* Attempt to execute the commmand, DW_ERROR_NONE on success */
12870 if(system(commandline) != -1)
12871 ret = DW_ERROR_NONE;
12857 } 12872 }
12858 return ret; 12873 return ret;
12859 } 12874 }
12860 12875
12861 /* 12876 /*