Detecting when a space changes in Spaces in Mac OS X

前端 未结 2 1101
隐瞒了意图╮
隐瞒了意图╮ 2020-12-07 23:29

Let\'s say I want to write a simple Cocoa app to make the Spaces feature of Leopard more useful. I would like to configure each space to have, say, different

  • s
相关标签:
2条回答
  • 2020-12-08 00:09

    As Peter says, in 10.6 you can use the NSWorkSpace NSWorkspaceActiveSpaceDidChangeNotification to get a notification when the workspace changes.

    You can then determine the current space using Quartz API, the kCGWindowWorkspace dictionary key holds the workspace. e.g:

    int currentSpace;
    // get an array of all the windows in the current Space
    CFArrayRef windowsInSpace = CGWindowListCopyWindowInfo(kCGWindowListOptionAll | kCGWindowListOptionOnScreenOnly, kCGNullWindowID);      
    
    // now loop over the array looking for a window with the kCGWindowWorkspace key
    for (NSMutableDictionary *thisWindow in (NSArray *)windowsInSpace)
    {
         if ([thisWindow objectForKey:(id)kCGWindowWorkspace])
           {
               currentSpace = [thisWindow objectForKey(id)kCGWindowWorkspace] intValue];
               break;
           }
    }
    

    Alternatively you can get the Space using the private API, take a look at CGSPrivate.h which allows you to do this:

    int currentSpace = 0;
    CGSGetWorkspace(_CGSDefaultConnection(), &currentSpace);
    

    To change the screen resolution you'll want to look at Quartz services, for altering the volume this may be helpful.

    0 讨论(0)
  • 2020-12-08 00:09

    NSWorkspace posts a NSWorkspaceActiveSpaceDidChangeNotification on its own notification center, but only on Snow Leopard.

    0 讨论(0)
提交回复
热议问题