Skip to content

Commit f2ef92c

Browse files
Fix for #1905: PythonLauncher not working correctly on OSX 10.5/Leopard
This fixes both Python Launchar and the terminalcommand module.
1 parent 6780a9d commit f2ef92c

4 files changed

Lines changed: 45 additions & 103 deletions

File tree

‎Lib/plat-mac/Carbon/AppleEvents.py‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# Generated from 'AEDataModel.h'
22

33
def FOUR_CHAR_CODE(x): return x
4+
typeApplicationBundleID = FOUR_CHAR_CODE('bund')
45
typeBoolean = FOUR_CHAR_CODE('bool')
56
typeChar = FOUR_CHAR_CODE('TEXT')
67
typeSInt16 = FOUR_CHAR_CODE('shor')

‎Lib/plat-mac/terminalcommand.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727

2828
def run(command):
2929
"""Run a shell command in a new Terminal.app window."""
30-
termAddress = AE.AECreateDesc(typeApplSignature, TERMINAL_SIG)
30+
termAddress = AE.AECreateDesc(typeApplicationBundleID, "com.apple.Terminal")
3131
theEvent = AE.AECreateAppleEvent(kAECoreSuite, kAEDoScript, termAddress,
3232
kAutoGenerateReturnID, kAnyTransactionID)
3333
commandDesc = AE.AECreateDesc(typeChar, command)

‎Mac/PythonLauncher/MyDocument.m‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -121,8 +121,8 @@ - (BOOL)readFromFile:(NSString *)fileName ofType:(NSString *)type;
121121
return YES;
122122
} else {
123123
[self run];
124-
[self close];
125-
return NO;
124+
[self performSelector:@selector(close) withObject:nil afterDelay:0.0];
125+
return YES;
126126
}
127127
}
128128

‎Mac/PythonLauncher/doscript.m‎

Lines changed: 41 additions & 100 deletions
Original file line numberDiff line numberDiff line change
@@ -11,108 +11,49 @@
1111
#import <ApplicationServices/ApplicationServices.h>
1212
#import "doscript.h"
1313

14-
/* I assume I could pick these up from somewhere, but where... */
15-
#define CREATOR 'trmx'
16-
17-
#define ACTIVATE_CMD 'misc'
18-
#define ACTIVATE_SUITE 'actv'
19-
20-
#define DOSCRIPT_CMD 'dosc'
21-
#define DOSCRIPT_SUITE 'core'
22-
#define WITHCOMMAND 'cmnd'
23-
24-
/* ... and there's probably also a better way to do this... */
25-
#define START_TERMINAL "/Applications/Utilities/Terminal.app/Contents/MacOS/Terminal &"
26-
2714
extern int
2815
doscript(const char *command)
2916
{
30-
OSErr err;
31-
AppleEvent theAEvent, theReply;
32-
AEAddressDesc terminalAddress;
33-
AEDesc commandDesc;
34-
OSType terminalCreator = CREATOR;
35-
36-
/* set up locals */
37-
AECreateDesc(typeNull, NULL, 0, &theAEvent);
38-
AECreateDesc(typeNull, NULL, 0, &terminalAddress);
39-
AECreateDesc(typeNull, NULL, 0, &theReply);
40-
AECreateDesc(typeNull, NULL, 0, &commandDesc);
41-
42-
/* create the "activate" event for Terminal */
43-
err = AECreateDesc(typeApplSignature, (Ptr) &terminalCreator,
44-
sizeof(terminalCreator), &terminalAddress);
45-
if (err != noErr) {
46-
NSLog(@"doscript: AECreateDesc: error %d\n", err);
47-
goto bail;
48-
}
49-
err = AECreateAppleEvent(ACTIVATE_SUITE, ACTIVATE_CMD,
50-
&terminalAddress, kAutoGenerateReturnID,
51-
kAnyTransactionID, &theAEvent);
52-
53-
if (err != noErr) {
54-
NSLog(@"doscript: AECreateAppleEvent(activate): error %d\n", err);
55-
goto bail;
56-
}
57-
/* send the event */
58-
err = AESend(&theAEvent, &theReply, kAEWaitReply,
59-
kAENormalPriority, kAEDefaultTimeout, NULL, NULL);
60-
if ( err == -600 ) {
61-
int count=10;
62-
/* If it failed with "no such process" try to start Terminal */
63-
err = system(START_TERMINAL);
64-
if ( err ) {
65-
NSLog(@"doscript: system(): %s\n", strerror(errno));
66-
goto bail;
67-
}
68-
do {
69-
sleep(1);
70-
/* send the event again */
71-
err = AESend(&theAEvent, &theReply, kAEWaitReply,
72-
kAENormalPriority, kAEDefaultTimeout, NULL, NULL);
73-
} while (err == -600 && --count > 0);
74-
if ( err == -600 )
75-
NSLog(@"doscript: Could not activate Terminal\n");
76-
}
77-
if (err != noErr) {
78-
NSLog(@"doscript: AESend(activate): error %d\n", err);
79-
goto bail;
80-
}
81-
82-
/* create the "doscript with command" event for Terminal */
83-
err = AECreateAppleEvent(DOSCRIPT_SUITE, DOSCRIPT_CMD,
84-
&terminalAddress, kAutoGenerateReturnID,
85-
kAnyTransactionID, &theAEvent);
86-
if (err != noErr) {
87-
NSLog(@"doscript: AECreateAppleEvent(doscript): error %d\n", err);
88-
goto bail;
89-
}
90-
91-
/* add the command to the apple event */
92-
err = AECreateDesc(typeChar, command, strlen(command), &commandDesc);
93-
if (err != noErr) {
94-
NSLog(@"doscript: AECreateDesc(command): error %d\n", err);
95-
goto bail;
96-
}
97-
err = AEPutParamDesc(&theAEvent, WITHCOMMAND, &commandDesc);
98-
if (err != noErr) {
99-
NSLog(@"doscript: AEPutParamDesc: error %d\n", err);
100-
goto bail;
101-
}
17+
char *bundleID = "com.apple.Terminal";
18+
AppleEvent evt, res;
19+
AEDesc desc;
20+
OSStatus err;
21+
22+
[[NSWorkspace sharedWorkspace] launchApplication:@"/Applications/Utilities/Terminal.app/"];
23+
24+
// Build event
25+
err = AEBuildAppleEvent(kAECoreSuite, kAEDoScript,
26+
typeApplicationBundleID,
27+
bundleID, strlen(bundleID),
28+
kAutoGenerateReturnID,
29+
kAnyTransactionID,
30+
&evt, NULL,
31+
"'----':utf8(@)", strlen(command),
32+
command);
33+
if (err) {
34+
NSLog(@"AEBuildAppleEvent failed: %d\n", err);
35+
return err;
36+
}
37+
38+
// Send event and check for any Apple Event Manager errors
39+
err = AESendMessage(&evt, &res, kAEWaitReply, kAEDefaultTimeout);
40+
AEDisposeDesc(&evt);
41+
if (err) {
42+
NSLog(@"AESendMessage failed: %d\n", err);
43+
return err;
44+
}
45+
// Check for any application errors
46+
err = AEGetParamDesc(&res, keyErrorNumber, typeSInt32, &desc);
47+
AEDisposeDesc(&res);
48+
if (!err) {
49+
AEGetDescData(&desc, &err, sizeof(err));
50+
NSLog(@"Terminal returned an error: %d", err);
51+
AEDisposeDesc(&desc);
52+
} else if (err == errAEDescNotFound) {
53+
err = noErr;
54+
} else {
55+
NSLog(@"AEGetPArmDesc returned an error: %d", err);
56+
}
10257

103-
/* send the command event to Terminal.app */
104-
err = AESend(&theAEvent, &theReply, kAEWaitReply,
105-
kAENormalPriority, kAEDefaultTimeout, NULL, NULL);
106-
107-
if (err != noErr) {
108-
NSLog(@"doscript: AESend(docommand): error %d\n", err);
109-
goto bail;
110-
}
111-
/* clean up and leave */
112-
bail:
113-
AEDisposeDesc(&commandDesc);
114-
AEDisposeDesc(&theAEvent);
115-
AEDisposeDesc(&terminalAddress);
116-
AEDisposeDesc(&theReply);
117-
return err;
58+
return err;
11859
}

0 commit comments

Comments
 (0)