-
Notifications
You must be signed in to change notification settings - Fork 0
/
osxlaunch.m
180 lines (157 loc) · 5.33 KB
/
osxlaunch.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
// a very simple utility to that allows to launch OS X applications (app bundles) from a command line
// and lets them appear in the foreground. Contrary to OS X's own open command, it does not treat all
// arguments like they're documents to be opened, and so doesn't impose to pass non-file arguments after
// a --args option. It also removes the need to specify full paths for the files to be opened for
// certain applications (KDE app bundles for instance).
#import <Foundation/Foundation.h>
#import <Cocoa/Cocoa.h>
#import <sys/types.h>
#import <libgen.h>
#import <mach/clock_types.h>
#import <dispatch/semaphore.h>
// from http://stackoverflow.com/a/4933492
@interface NSWorkspace (MDAdditions)
- (pid_t) launchApplicationAtPath:(NSString *)path
withArguments:(NSArray *)argv
waitFinished:(BOOL)wait
psn:(ProcessSerialNumber*)psn
error:(NSError **)error;
- (void) appTerminated:(NSNotification*)note;
@end
#import <CoreServices/CoreServices.h>
@interface NSString (MDAdditions)
- (BOOL) getFSRef:(FSRef *)anFSRef error:(NSError **)anError;
@end
#import <sys/syslimits.h>
@implementation NSString (MDAdditions)
- (BOOL) getFSRef:(FSRef *)anFSRef error:(NSError **)anError
{
if( anError ){
*anError = nil;
}
OSStatus status = noErr;
status = FSPathMakeRef( (const UInt8 *)[self UTF8String], anFSRef, NULL );
if( status != noErr ){
if( anError ){
*anError = [NSError errorWithDomain:NSOSStatusErrorDomain code:status userInfo:nil];
}
}
return (status == noErr);
}
@end
static dispatch_semaphore_t sema;
static pid_t waitPID;
@implementation NSWorkspace (MDAdditions)
- (void)appTerminated:(NSNotification *)note
{
// NSLog(@"terminated %@\n", [[note userInfo] objectForKey:@"NSApplicationProcessIdentifier"]);
if( [[[note userInfo] objectForKey:@"NSApplicationProcessIdentifier"] unsignedLongLongValue] == (unsigned long long) waitPID ){
dispatch_semaphore_signal(sema);
}
}
- (pid_t) launchApplicationAtPath:(NSString *)path withArguments:(NSArray *)argv
waitFinished:(BOOL)wait
psn:(ProcessSerialNumber*)psn
error:(NSError **)error
{ pid_t pid = 0;
if( error ){
*error = nil;
}
if( path ){
FSRef itemRef;
if( [path getFSRef:&itemRef error:error] ){
LSApplicationParameters appParameters = { 0,
kLSLaunchAndDisplayErrors|kLSLaunchDontAddToRecents, &itemRef, NULL, NULL,
(argv ? (CFArrayRef)argv : NULL), NULL };
ProcessSerialNumber PSN;
OSStatus status = noErr;
status = LSOpenApplication( &appParameters, &PSN );
if( status != noErr ){
NSLog( @"[%@ %@] LSOpenApplication() returned %d for %@",
NSStringFromClass([self class]),
NSStringFromSelector(_cmd), (int) status, path );
if( error ){
*error = [NSError errorWithDomain:NSOSStatusErrorDomain code:status userInfo:nil];
}
}
else{
if( psn ){
*psn = PSN;
}
GetProcessPID( &PSN, &pid );
if( wait ){
NSNotificationCenter *center = [self notificationCenter];
sema = dispatch_semaphore_create(0);
[center addObserver:self selector:@selector(appTerminated:)
name:NSWorkspaceDidTerminateApplicationNotification
object:NSApp];
waitPID = pid;
if( kill( pid, 0 ) ){
// catch the case where the app we launched exited before we could put the notification centre
// in place
dispatch_semaphore_signal(sema);
}
}
}
}
}
else{
if( error ){
*error = [NSError errorWithDomain:NSOSStatusErrorDomain code:paramErr userInfo:nil];
}
}
return pid;
}
@end
int main( int argc, const char *argv[] )
{ NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
BOOL success = NO, waitApp = NO;
if ( argc > 1 ){
int i = 1;
NSString *cwd = [[NSFileManager defaultManager] currentDirectoryPath];
NSMutableArray *theArguments = [[NSMutableArray alloc] init];
if( strcmp( argv[i], "--help" ) == 0 ){
fprintf( stderr, "Usage: %s [-W|--wait-app] command [arguments]\n",
basename((char*)argv[0]) );
exit(0);
}
if( strcmp( argv[i], "-W" ) == 0 || strcmp( argv[i], "--wait-apps" ) == 0 ){
waitApp = YES;
i += 1;
}
NSString *command = [NSString stringWithCString:argv[i] encoding:NSUTF8StringEncoding];
for( ++i ; i < argc ; ++i ){
NSString *arg = [NSString stringWithCString:argv[i] encoding:NSUTF8StringEncoding];
if( argv[i][0] != '/' && access( argv[i], R_OK ) == 0 ){
NSString *fullPath = [NSString stringWithFormat:@"%@/%s", cwd, argv[i]];
if( fullPath ){
// NSLog( @"%s@%@ -> %@", argv[i], cwd, fullPath );
arg = fullPath;
}
}
[theArguments addObject:arg];
}
NSError *err = nil;
ProcessSerialNumber psn;
[[NSWorkspace sharedWorkspace] launchApplicationAtPath:command withArguments:theArguments
waitFinished:waitApp psn:&psn error:&err];
if( err ){
NSLog( @"%@", err );
success = NO;
}
else{
success = YES;
if( waitApp ){
// thanks to http://michiganlabs.com/unit-testing-objectivec-asychronous-blockbased-api/
// and http://stackoverflow.com/questions/4326350/how-do-i-wait-for-an-asynchronously-dispatched-block-to-finish
NSTimeInterval TIMEOUT = 5.0;
while( dispatch_semaphore_wait( sema, DISPATCH_TIME_NOW ) ){
[[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode
beforeDate:[NSDate dateWithTimeIntervalSinceNow:TIMEOUT]];
}
}
}
}
[pool release];
exit( !success );
}