我试图获取一个macOS应用程序来检测用户何时切换Spaces。
尽管已更新为
,但大多数遵循此建议(Detecting when a space changes in Spaces in Mac OS X)import Cocoa
@NSApplicationMain
class AppDelegate: NSObject, NSApplicationDelegate {
func applicationDidFinishLaunching(_ aNotification: Notification) {
NotificationCenter.default.addObserver(self, selector: #selector(self.spaceChange), name: NSWorkspace.activeSpaceDidChangeNotification, object: nil)
}
func applicationShouldTerminateAfterLastWindowClosed(_ sender: NSApplication) -> Bool {
return true
}
@objc func spaceChange() {
print("space did change")
}
}
看起来不错,至少它编译时没有警告/错误。
当我切换空格时,我希望当应用程序运行时,“空格确实改变了”会打印到日志中。但是,它永远不会打印到日志中。
答案 0 :(得分:1)
是的,这里的问题是我使用了错误的NotificationCenter,我们需要:
NSWorkspace.shared.notificationCenter.addObserver(self, selector: #selector(self.spaceChange), name: NSWorkspace.activeSpaceDidChangeNotification, object: nil)