Skip to content

Instantly share code, notes, and snippets.

@tmandry
Last active March 29, 2022 21:22
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tmandry/e88ca15045cd02a97fa1034edf90ba98 to your computer and use it in GitHub Desktop.
Save tmandry/e88ca15045cd02a97fa1034edf90ba98 to your computer and use it in GitHub Desktop.
Getting Spaces and their order for each display
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")
}
}
@tmandry
Copy link
Author

tmandry commented Nov 1, 2018

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).

@tmandry
Copy link
Author

tmandry commented Nov 9, 2018

The idea (except KVO) comes from this blog post: http://ianyh.com/blog/identifying-spaces-in-mac-os-x/

@rgbworld
Copy link

rgbworld commented Dec 6, 2020

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?

@tmandry
Copy link
Author

tmandry commented Dec 7, 2020

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