Shebuka
Shebuka

Reputation: 3228

Search for application in /Application folder first

i'm searching for a way to open my other application with parameters and found a very good post here, but on my developer machine I have like 10 test/brach/release versions of my application and only one current stable version in /Application folder.

Both this code:

NSWorkspace *workspace = [NSWorkspace sharedWorkspace];

NSURL *url = [NSURL fileURLWithPath:[workspace fullPathForApplication:@"My Application"]];
NSURL *url = [workspace URLForApplicationWithBundleIdentifier:@"it.my_company.my_application"];

points to the wrong test/branch/release version. How do I find the URL of my application in /Application folder or at least URL at same level as my current running application?

Upvotes: 2

Views: 277

Answers (2)

Shebuka
Shebuka

Reputation: 3228

This is what i'v came out to use:

NSWorkspace *workspace = [NSWorkspace sharedWorkspace];

NSString *appPath = [[[NSBundle mainBundle] bundlePath] stringByDeletingLastPathComponent];
appPath = [appPath stringByAppendingPathComponent:@"MyApp.app"];

if (not [[NSFileManager defaultManager] fileExistsAtPath:appPath])
    appPath = @"/Applications/MyApp.app";

if (not [[NSFileManager defaultManager] fileExistsAtPath:appPath])
    appPath = [workspace fullPathForApplication:@"MyApp"];

if (not [[NSFileManager defaultManager] fileExistsAtPath:appPath]) {
    return NO;
}
else {
    // found it in some place, now can launch.

Upvotes: 1

pb2q
pb2q

Reputation: 59617

You could use the NSTask messages: setLaunchPath or launchedTaskWithLaunchPath to supply an explicit path to your other application.

Something like:

NSArray *args = [NSArray arrayWithObjects:nil];
[NSTask launchedTaskWithLaunchPath:@"/Applications/My Application.app/Contents/MacOS/My Application" arguments:args];

Upvotes: 1

Related Questions