Last active
March 29, 2022 21:22
-
-
Save tmandry/e88ca15045cd02a97fa1034edf90ba98 to your computer and use it in GitHub Desktop.
Getting Spaces and their order for each display
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var observer: Observer! | |
func stuff() { | |
guard let database = UserDefaults.init(suiteName: "com.apple.spaces") else { | |
fatalError("cannot read spaces data") | |
} | |
print(String(describing: database.object(forKey: "SpacesDisplayConfiguration"))) | |
observer = Observer(object: database) | |
} | |
class Observer: NSObject { | |
@objc var database: UserDefaults | |
var observation: NSKeyValueObservation? | |
init(object: UserDefaults) { | |
database = object | |
super.init() | |
database.addObserver(self, forKeyPath: "SpacesDisplayConfiguration", options: [.new, .old, | |
.initial], context: nil); | |
} | |
override func observeValue(forKeyPath: String?, of object: Any?, change: [NSKeyValueChangeKey: | |
Any]?, context: UnsafeMutableRawPointer?) { | |
print("changed") | |
} | |
} |
The idea (except KVO) comes from this blog post: http://ianyh.com/blog/identifying-spaces-in-mac-os-x/
Do you know if the "app-bindings
key updates its values when a user right-clicks on an application's Dock icon and changes the "Assign to" options?
For example, does changing options from None
to All Desktops
update the app-Bindings
values?
Off the top of my head I don't know, but it seems plausible that it would.
Mostly I've just had to experiment with little programs and find out what happens. (Which isn't great for backwards/forwards compatibility, but not much else you can do.)
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This also includes a list of window numbers for each space, but it is not updated when windows are moved between spaces or the user changes spaces. It only updates because the space configuration changed (spaces were added, removed, or reordered).