cpython/Mac/PythonLauncher/doscript.m
Ned Deily 549e628272
bpo-40477: macOS Python Launcher app fixes for recent macOS releases (GH-30348)
This change solves two problems encountered by users of the macOS Python Launcher app on recent macOS releases (10.14+):

- The launcher app was no longer able to launch the macOS Terminal.app to run a script.

- Even if Terminal.app was already launched, the launcher app was unable to send an Apple Event to Terminal.app to open and run Python with the desired .py file.
2022-01-03 01:19:59 -05:00

59 lines
1.7 KiB
Objective-C

/*
* doscript.c
* PythonLauncher
*
* Created by Jack Jansen on Wed Jul 31 2002.
* Copyright (c) 2002 __MyCompanyName__. All rights reserved.
*
*/
#import <Cocoa/Cocoa.h>
#import <ApplicationServices/ApplicationServices.h>
#import "doscript.h"
extern int
doscript(const char *command)
{
char *bundleID = "com.apple.Terminal";
AppleEvent evt, res;
AEDesc desc;
OSStatus err;
[[NSWorkspace sharedWorkspace] launchApplication:@"Terminal.app"];
// Build event
err = AEBuildAppleEvent(kAECoreSuite, kAEDoScript,
typeApplicationBundleID,
bundleID, strlen(bundleID),
kAutoGenerateReturnID,
kAnyTransactionID,
&evt, NULL,
"'----':utf8(@)", strlen(command),
command);
if (err) {
NSLog(@"AEBuildAppleEvent failed: %ld\n", (long)err);
return err;
}
// Send event and check for any Apple Event Manager errors
err = AESendMessage(&evt, &res, kAEWaitReply, kAEDefaultTimeout);
AEDisposeDesc(&evt);
if (err) {
NSLog(@"AESendMessage failed: %ld\n", (long)err);
return err;
}
// Check for any application errors
err = AEGetParamDesc(&res, keyErrorNumber, typeSInt32, &desc);
AEDisposeDesc(&res);
if (!err) {
AEGetDescData(&desc, &err, sizeof(err));
NSLog(@"Terminal returned an error: %ld", (long)err);
AEDisposeDesc(&desc);
} else if (err == errAEDescNotFound) {
err = noErr;
} else {
NSLog(@"AEGetPArmDesc returned an error: %ld", (long)err);
}
return err;
}