Skip to content

Commit 6712327

Browse files
committed
Use NSWorkspace for checking for running instance, instead of NSTask with a shell
1 parent b25e6e1 commit 6712327

File tree

1 file changed

+10
-5
lines changed

1 file changed

+10
-5
lines changed

PFMoveApplication.m

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -159,13 +159,18 @@ void PFMoveToApplicationsFolderIfNecessary() {
159159
// If a copy already exists in the Applications folder, put it in the Trash
160160
if ([fm fileExistsAtPath:destinationPath]) {
161161
// But first, make sure that it's not running
162-
NSString *script = [NSString stringWithFormat:@"ps ax -o comm | grep '%@/' | grep -v grep >/dev/null", destinationPath];
163-
NSTask *task = [NSTask launchedTaskWithLaunchPath:@"/bin/sh" arguments:[NSArray arrayWithObjects:@"-c", script, nil]];
164-
[task waitUntilExit];
165-
162+
BOOL destinationIsRunning = NO;
163+
for (NSRunningApplication *runningApplication in [[NSWorkspace sharedWorkspace] runningApplications]) {
164+
NSString *executablePath = [[runningApplication executableURL] path];
165+
if ([[executablePath substringToIndex:[destinationPath length]] isEqualToString:destinationPath]) {
166+
destinationIsRunning = YES;
167+
break;
168+
}
169+
}
170+
166171
// If the task terminated with status 0, it means that the final grep produced 1 or more lines of output.
167172
// It means that the app is already running
168-
if ([task terminationStatus] == 0) {
173+
if (destinationIsRunning) {
169174
// Give the running app focus and terminate myself
170175
NSLog(@"INFO -- Switching to an already running version");
171176
[[NSTask launchedTaskWithLaunchPath:@"/usr/bin/open" arguments:[NSArray arrayWithObject:destinationPath]] waitUntilExit];

0 commit comments

Comments
 (0)