Search code examples
objective-ccocoansurlnsworkspace

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?


Solution

  • 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.