comparison mac/dw.m @ 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
comparison
equal deleted inserted replaced
2151:467401a1ca69 2152:d299b5acc507
12406 DWTID dw_thread_id(void) 12406 DWTID dw_thread_id(void)
12407 { 12407 {
12408 return (DWTID)pthread_self(); 12408 return (DWTID)pthread_self();
12409 } 12409 }
12410 12410
12411 #ifdef BUILDING_FOR_CATALINA
12412 NSURL *_dw_url_from_program(NSString *nsprogram, NSWorkspace *ws)
12413 {
12414 NSURL *retval = [ws URLForApplicationWithBundleIdentifier:nsprogram];
12415
12416 if(!retval)
12417 {
12418 SEL sfpfa = NSSelectorFromString(@"fullPathForApplication");
12419
12420 if([ws respondsToSelector:sfpfa])
12421 {
12422 DWIMP ifpfa = (DWIMP)[ws methodForSelector:sfpfa];
12423 NSString *apppath = ifpfa(ws, sfpfa, nsprogram);
12424
12425 if(apppath)
12426 retval = [NSURL fileURLWithPath:apppath];
12427 }
12428 if(!retval)
12429 retval = [NSURL fileURLWithPath:nsprogram];
12430 }
12431 return retval;
12432 }
12433 #endif
12434
12411 /* 12435 /*
12412 * Execute and external program in a seperate session. 12436 * Execute and external program in a seperate session.
12413 * Parameters: 12437 * Parameters:
12414 * program: Program name with optional path. 12438 * program: Program name with optional path.
12415 * type: Either DW_EXEC_CON or DW_EXEC_GUI. 12439 * type: Either DW_EXEC_CON or DW_EXEC_GUI.
12422 int ret = DW_ERROR_UNKNOWN; 12446 int ret = DW_ERROR_UNKNOWN;
12423 12447
12424 if(type == DW_EXEC_GUI) 12448 if(type == DW_EXEC_GUI)
12425 { 12449 {
12426 NSString *nsprogram = [NSString stringWithUTF8String:program]; 12450 NSString *nsprogram = [NSString stringWithUTF8String:program];
12427 12451 NSWorkspace *ws = [NSWorkspace sharedWorkspace];
12452
12428 if(params && params[0] && params[1]) 12453 if(params && params[0] && params[1])
12429 { 12454 {
12430 NSString *file = [NSString stringWithUTF8String:params[1]]; 12455 NSString *file = [NSString stringWithUTF8String:params[1]];
12431 12456
12432 #ifdef BUILDING_FOR_CATALINA 12457 #ifdef BUILDING_FOR_CATALINA
12433 if(@available(macOS 10.15, *)) 12458 if(@available(macOS 10.15, *))
12434 { 12459 {
12435 NSURL *url = [NSURL fileURLWithPath:nsprogram]; 12460 NSURL *url = _dw_url_from_program(nsprogram, ws);
12436 NSURL *nsfile = [NSURL fileURLWithPath:file]; 12461 NSURL *nsfile = [NSURL fileURLWithPath:file];
12437 12462
12438 [[NSWorkspace sharedWorkspace] openURLs:[[NSArray alloc] initWithObjects:nsfile, nil] 12463 [ws openURLs:[NSArray arrayWithObjects:nsfile, nil]
12439 withApplicationAtURL:url 12464 withApplicationAtURL:url
12440 configuration:[NSWorkspaceOpenConfiguration configuration] 12465 configuration:[NSWorkspaceOpenConfiguration configuration]
12441 completionHandler:^(NSRunningApplication *app, NSError *error) { 12466 completionHandler:^(NSRunningApplication *app, NSError *error) {
12467 if(error)
12468 NSLog(@"openURLs: %@", [error localizedDescription]);
12442 }]; 12469 }];
12443 } 12470 }
12444 else 12471 else
12445 #endif 12472 #endif
12446 [[NSWorkspace sharedWorkspace] openFile:file withApplication:nsprogram]; 12473 {
12474 SEL sofwa = NSSelectorFromString(@"openFile:withApplication:");
12475
12476 if([ws respondsToSelector:sofwa])
12477 {
12478 DWIMP iofwa = (DWIMP)[ws methodForSelector:sofwa];
12479 iofwa(ws, sofwa, file, nsprogram);
12480 }
12481 }
12447 } 12482 }
12448 else 12483 else
12449 { 12484 {
12450 #ifdef BUILDING_FOR_CATALINA 12485 #ifdef BUILDING_FOR_CATALINA
12451 if(@available(macOS 10.15, *)) 12486 if(@available(macOS 10.15, *))
12452 { 12487 {
12453 NSURL *url = [NSURL fileURLWithPath:nsprogram]; 12488 NSURL *url = _dw_url_from_program(nsprogram, ws);
12454 12489
12455 [[NSWorkspace sharedWorkspace] openApplicationAtURL:url 12490 [ws openApplicationAtURL:url
12456 configuration:[NSWorkspaceOpenConfiguration configuration] 12491 configuration:[NSWorkspaceOpenConfiguration configuration]
12457 completionHandler:^(NSRunningApplication *app, NSError *error) { 12492 completionHandler:^(NSRunningApplication *app, NSError *error) {
12493 if(error)
12494 NSLog(@"openApplicationAtURL: %@", [error localizedDescription]);
12458 }]; 12495 }];
12459 } 12496 }
12460 else 12497 else
12461 #endif 12498 #endif
12462 [[NSWorkspace sharedWorkspace] launchApplication:nsprogram]; 12499 {
12500 SEL sla = NSSelectorFromString(@"launchApplication");
12501
12502 if([ws respondsToSelector:sla])
12503 {
12504 DWIMP ila = (DWIMP)[ws methodForSelector:sla];
12505 ila(ws, sla, nsprogram);
12506 }
12507 }
12463 } 12508 }
12464 return DW_ERROR_NONE; 12509 return DW_ERROR_NONE;
12465 } 12510 }
12466 12511
12467 if((ret = fork()) == 0) 12512 if((ret = fork()) == 0)