Mac OS X Lion: Detect if another application is running in full screen mode?
Asked Answered
N

4

18

In a Cocoa app, is there a way to tell if another application currently is in full screen mode?

My application is configured to show up on all Spaces and listens for mouseEntered events to order itself to the front.

Problem is that when another app is in full screen mode and the user happens to move the mouse across the black area where my app's window is located, it is brought to the front (happens with multiple monitors).

I've only seen the above behavior with [self setCollectionBehavior: NSWindowCollectionBehaviorCanJoinAllSpaces]; enabled.

Here the other relevant code for my app.

- (void) mouseEntered:(NSEvent *)theEvent
{
    // Don't do this when another app is in full screen mode:
    [[self window] orderFront:self];
}
Necessitous answered 21/8, 2011 at 12:17 Comment(1)
Check these two links out - they may give you a hint: https://mcmap.net/q/742407/-detecting-fullscreen-on-mac AND #6816417Niobous
S
1

After a great deal of frustration, this worked for me to get a window that floats on all spaces except full-screen ones. I saw the fullScreenNone constant name, and since it described what I wanted, I tried it and found that it worked.

    window.level = .floating
    window.collectionBehavior = [.canJoinAllSpaces, .fullScreenNone]
    window.canHide = false
Stuyvesant answered 30/11, 2020 at 2:48 Comment(1)
Nine years later, thanks for taking the time to answer such an old question! The app I worked on when I ran into this does not even exist anymore. I'll accept your answer to finally put this to rest. Cheers.Necessitous
C
2

The above mentioned methods of registering for

"NSWindowWillEnterFullScreenNotification"

does not work, they can be used to notify your own app, using them we cannot detect whether any other application is in full screen mode or not.

However After trying out so many options found out FullScreen detector app at github this usefull link ..:):)

https://github.com/shinypb/FullScreenDetector.git

Cruse answered 9/4, 2013 at 7:3 Comment(1)
+1 on this, In a nutshell, you need to listen to NSWorkspace.activeSpaceDidChangeNotification (Swift 5)Denesedengue
P
1

Hmm, have you ruled out using applescript/scriptingbridge? You can get the size of windows from applescript and compare them to the size of the screen. (or do you not know which screen a given app is on?)
Certain apps which are accessible will have an 'AXFullScreen' attribute on their windows. For example this works for some apps:

  tell application "System Events"
    tell process "Pages"
        repeat with myWin in windows
            get value of attribute "AXFullScreen" of myWin
        end repeat
    end tell 
end tell

The real action seems to be down in carbon... MacWindows.h and CarbonEvents.h have references to "FullScreen" in them.

You will need to research this though.

Parshall answered 6/9, 2011 at 4:58 Comment(1)
Anything you can do via AppleScript you can do faster in native code. This blog post is informative on the subject of accessing AX attributes in code: cocoatutorial.grapewave.com/tag/axuielementcopyattributevalueMantua
N
1

Use notifications. For example:

[[NSNotificationCenter defaultCenter] addObserver:self
                                         selector:@selector(willEnterFull:)
                                             name:NSWindowWillEnterFullScreenNotification
                                           object:nil];

Actually, you'll probably want to use NSDistributedNotificationCenter instead, since it goes across processes.

You're adding your object as an observer, so that when something else posts a notification that it will enter full screen, your object will receive that notification.

The selector is the message/method you want called by the notification process.

The name parameter is the actual name of the notification. These are standard, unless you were to create a custom notification for something you would be using.

The object parameter is for specifying which object you want to receive notifications from. Since you want to know when ANY app is going full screen, you'd want to leave this nil.

Remember to remove your object as an observer before it's deallocated!

Niobous answered 20/12, 2011 at 6:46 Comment(3)
This will only work for applications that become full screen after your application has launched. If there are full screen applications running when your application launches, they will not send new notifications to your application.Dispassionate
Yes, prior notifications will not be re-sent to the app that has just been launched. So, true, if Mark needs to know whether other apps are already open fullscreen when his app is launched, then this won't help. But once his app is launched, it will detect any apps that go into fullscreen mode from that point forward.Niobous
Did the notification behavior change in Mountain Lion? Because your code will not give me any notifications (with the distributed center). When I set the name parameter to nil (to receive all notifications) I only get "com.apple.HIToolbox.hideMenuBarShown" when entering fullscreen mode.Request
S
1

After a great deal of frustration, this worked for me to get a window that floats on all spaces except full-screen ones. I saw the fullScreenNone constant name, and since it described what I wanted, I tried it and found that it worked.

    window.level = .floating
    window.collectionBehavior = [.canJoinAllSpaces, .fullScreenNone]
    window.canHide = false
Stuyvesant answered 30/11, 2020 at 2:48 Comment(1)
Nine years later, thanks for taking the time to answer such an old question! The app I worked on when I ran into this does not even exist anymore. I'll accept your answer to finally put this to rest. Cheers.Necessitous

© 2022 - 2024 — McMap. All rights reserved.