rraallvv
rraallvv

Reputation: 2933

Hide fullscreen applications programmatically on mac

I have this snippet of code that hide all the running applications' windows, except for Finder's.

NSArray *apps = [[NSWorkspace sharedWorkspace] runningApplications];

for (NSRunningApplication *app in apps) {
    if([app.bundleIdentifier.lowercaseString isEqualToString:@"com.apple.finder"]) {
        [app activateWithOptions:NSApplicationActivateAllWindows|NSApplicationActivateIgnoringOtherApps];
    } else {
        [app hide];
    }
}

It works OK for non full screen windows, though.

How can I hide all the fullscreen windows too?

This doesn't work either

[NSWorkspace.sharedWorkspace hideOtherApplications];

Upvotes: 0

Views: 235

Answers (1)

rraallvv
rraallvv

Reputation: 2933

This is how I did:

// Create a tiny window on each screen to force all the full screen windows to get out of the way
for (NSScreen *screen in [NSScreen screens]) {
    NSRect dummyFrame = {0,0,1,1};

    NSWindow *dummyWindow = [[NSWindow alloc] initWithContentRect:dummyFrame
                                                             styleMask:NSBorderlessWindowMask
                                                               backing:NSBackingStoreBuffered
                                                                 defer:NO
                                                                screen:screen];

    NSView *dummyView = [[NSView alloc] initWithFrame:dummyFrame];
    [dummyWindow setContentView: dummyView];
    [dummyWindow makeKeyAndOrderFront:self];
}

// Now hide all the windows except for Finder's 
NSArray *apps = [[NSWorkspace sharedWorkspace] runningApplications];

for (NSRunningApplication *app in apps) {
    if([app.bundleIdentifier.lowercaseString isEqualToString:@"com.apple.finder"]) {
        [app activateWithOptions:NSApplicationActivateAllWindows|NSApplicationActivateIgnoringOtherApps];
    } else {
        [app hide];
    }
}

Upvotes: 1

Related Questions