2

I am writing an IM client for Mac (in Python, but an Objective C / Cocoa solution here is fine). I want to detect whether or not the user is currently watching a movie or playing a game in the foreground, or doing anything else that takes up the entire screen. If so, I won't play a sound when a new IM comes in, but if not, I will play the sound.

How can I detect this? Is there some way to get the foreground window with Applescript and look at its dimensions? Or is there some other API call?

halfer
  • 19,824
  • 17
  • 99
  • 186
zak23
  • 3,378
  • 5
  • 30
  • 28
  • 1
    Following vasi, maybe instead your app config could include a list of apps which, if in the foreground, will cause you to suppress notifications? You could populate by default with quicktime player, front row, and popular games (a short list, alas). – John Fouhy Mar 11 '09 at 03:23

4 Answers4

9

To check for full-screen, call CGDisplayIsCaptured(screenID) on each screen.

But I'm not sure if you're checking the right thing. For one thing, I could have one screen captured ("full screen") and a second screen uncaptured, what do you want to do in this case?

Also, does fullscreen really mean anything? If I'm using GarageBand to work on a song, I probably don't want to hear random sounds, regardless of whether or not anything's full screen. Or I could be running a Windows VM full-screen, but still want to be notified of IMs.

vasi
  • 1,026
  • 7
  • 6
2

not entirely sure how to do this, but the apple docs say:

To track changes in the login session’s presentation mode, you may handle the kEventAppSystemUIModeChanged Carbon event

cobbal
  • 69,903
  • 20
  • 143
  • 156
0

In Mountain Lion (and probably earlier), you can track the presence of the menu bar by monitoring the distributed notifications com.apple.HIToolbox.hideMenuBarShown and com.apple.HIToolbox.hideMenuBarShown. No menu bar usually == fullscreen mode. This works across apps, so you can tell when, say, VLC goes fullscreen, or when someone switches to iCal in fullscreen mode.

To do this, register for these two notifications:

[[NSDistributedNotificationCenter defaultCenter] addObserver:self
        selector:@selector(windowDidEnterFullScreen:)
        name:@"com.apple.HIToolbox.hideMenuBarShown"
        object:nil];

[[NSDistributedNotificationCenter defaultCenter] addObserver:self
       selector:@selector(windowDidExitFullScreen:)
       name:@"com.apple.HIToolbox.frontMenuBarShown"
       object:nil];

then create your own selectors to handle those cases. frontMenuBarShown fires all the time, so to catch a real return from fullscreen, watch for the first 'didExit' that follows a 'didEnter'...

ecume des jours
  • 2,493
  • 1
  • 19
  • 15
  • This is implementation detail may be removed in future [See this](http://www.cocoabuilder.com/archive/cocoa/102291-fullscreen-enter-exit-notification.html). – Raviprakash Apr 09 '13 at 04:28
  • This notifications are also get posted when some app is in full screen mode and user moves mouse upward, so that menuBar is shown in full screen. – Oleksiy Ivanov Mar 10 '14 at 12:41
0

The two solutions posted so far apply to “real” full-screen, but it’s worth noting that many full-screen apps just put a window over the whole screen (or, as vasi points out, a whole screen). To be accurate, you’ll have to check both.

Jens Ayton
  • 14,532
  • 3
  • 33
  • 47