Search code examples
objective-cmacoscocoadesktopfinder

How to programmatically focus Desktop/Finder via Mac App?


I want to focus Desktop/Finder via my App, especially if the user is currently inside a fullscreen application. I've been trying to use stuff like:

[[NSWorkspace sharedWorkspace] launchApplication:@"Finder"];

and

[[NSWorkspace sharedWorkspace] openFile:nil withApplication:@"Finder"];

But using these lines of code, a new Finder window will open "All Files".
I just want to focus the Desktop/Finder, how can I do that? Looking for App Store-ready code.
Thanks!


Solution

  • you can do it using cocoa's NSWorkspace - no special entitlements needed

    NSArray *apps = [[NSWorkspace sharedWorkspace] runningApplications];
    
    for (NSRunningApplication *app in apps) {
        if([app.bundleIdentifier.lowercaseString isEqualToString:@"com.apple.finder"]) {
            [app activateWithOptions:NSApplicationActivateAllWindows|NSApplicationActivateIgnoringOtherApps];
            break;
        }
    }