How to get current workspace in macOS 10.13?
Asked Answered
L

1

6

I want to get the current workspace in my app. I found a way to achieve my goal from valexa's answer:

-(int)spaceNumber
{
    CFArrayRef windowsInSpace = CGWindowListCopyWindowInfo(kCGWindowListOptionAll | kCGWindowListOptionOnScreenOnly, kCGNullWindowID);      
    for (NSMutableDictionary *thisWindow in (NSArray *)windowsInSpace)    {
        if ([thisWindow objectForKey:(id)kCGWindowWorkspace]){
            return [[thisWindow objectForKey:(id)kCGWindowWorkspace] intValue];
        }
    }
    return -1;
}

But now(macOS 10.13.3 for my case) the kCGWindowWorkspace is not existed any more. So are there any other ways to achieve my goal?

Thanks in advance!

Limpid answered 15/2, 2018 at 14:56 Comment(0)
A
0

The GitHub utility WhichSpace [github.com/gechr/WhichSpace] will put a numeric icon in the toolbar, indicating which Desktop number is the current Space. This can be queried with Applescript:

tell application "System Events" ¬
  to tell process "WhichSpace" ¬
    to set temp to (title of menu bar items of menu bar 1)
return item 1 of temp

The following Swift code will call a function when the Desktop changes. That's also potentially useful.

NSWorkspace.shared.notificationCenter.addObserver(forName: 
  NSWorkspace.activeSpaceDidChangeNotification, object: nil, queue: nil,
    using: <the function you want to handle this event> )
Agle answered 23/8, 2022 at 5:15 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.