[Pythonmac-SIG] Found a way to test for the window manager

Jack Jansen Jack.Jansen@cwi.nl
Fri, 21 Feb 2003 17:27:23 +0100


Folks,
I found a way to test for window manager availablity that is fairly 
cheap and doesn't seem to crash Python under any circumstances. I'm 
going to add this to MacOS, but I'm not sure about the Python API yet. 
I can do one of two things (or maybe more, please give alternatives if 
you have them):

1. A call MacOS.WMAvailable(). Use this something like
    if MacOS.WMAvailable():
       EasyDialogs.Message("This is an important message")
    else:
       stderr.write("This is an important message\n")
2. A call MacOS.NeedWM() which would raise an exception if the WM 
wasn't available. This you would simply call as
    MacOS.NeedWM()

2 would give a better error message (as it could be encoded in the 
exception), but simulating 1 with 2 would be expensive/ugly. Opinions?

In case people are interested, the trick is twofold. First I call 
CGMainDisplayID(). This fails with a message (but not an abort) if 
there isn't a window manager, or if you don't have permission to 
connect to it. Only if this call succeeds I try to call 
SetFrontProcess() for the current process. This fails with an error 
-606 (app is BG-only, and launch flags disallow this) if you're not 
running from a .app bundle (but it would have abort()ed if you called 
it from a program in the category above). Oh yes, this whole sequence 
is only done the first time you call WMAvailable, so the fact the 
SetFrontProcess pushes you to the front isn't a problem. Actually, it 
can be seen as a feature, since it'll raise apps to the front also when 
launched from the terminal window.
--
Jack Jansen, <Jack.Jansen@cwi.nl>, http://www.cwi.nl/~jack
If I can't dance I don't want to be part of your revolution -- Emma 
Goldman