forked from victorpimentel/TVShows
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Relaunch.m
63 lines (52 loc) · 2.06 KB
/
Relaunch.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
/*
* This file is part of the TVShows 2 ("Phoenix") source code.
* http://github.com/victorpimentel/TVShows/
*
* TVShows is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* You should have received a copy of the GNU General Public License
* along with TVShows. If not, see <http://www.gnu.org/licenses/>.
*
*/
#import <Cocoa/Cocoa.h>
#import "TVShows_Prefix.pch"
#import "AppInfoConstants.h"
// Thanks to Matt Patenaude and his blog post on how to relaunch an application.
// Slightly modified to work with a Preference Pane and a Launch Helper.
// http://iloveco.de/relaunching-your-application/
void relaunchHelper(NSString *launchAgentPath)
{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
NSTask *aTask = [[NSTask alloc] init];
[aTask setLaunchPath:@"/bin/launchctl"];
[aTask setArguments:[NSArray arrayWithObjects:@"load",@"-w",launchAgentPath,nil]];
[aTask launch];
[aTask waitUntilExit];
[aTask release];
[pool drain];
}
int main(int argc, char *argv[])
{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
pid_t parentPID = atoi(argv[3]);
ProcessSerialNumber psn;
while (GetProcessForPID(parentPID, &psn) != procNotFound)
sleep(1);
NSString *appPath = [NSString stringWithCString:argv[1] encoding:NSUTF8StringEncoding];
NSString *prefPath = [NSString stringWithCString:argv[2] encoding:NSUTF8StringEncoding];
BOOL success = NO;
if (prefPath.length == 0) {
relaunchHelper(appPath);
success = YES;
} else {
success = [[NSWorkspace sharedWorkspace] openFile:[prefPath stringByExpandingTildeInPath]
withApplication:[appPath stringByExpandingTildeInPath]];
}
if (!success)
NSLog(@"Could not relaunch application at %@", appPath);
[pool drain];
return (success) ? 0 : 1;
}