Skip to content

Instantly share code, notes, and snippets.

@frypf
Last active April 22, 2024 15:01
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 frypf/66636117556fc9bdc092505d14edfe0b to your computer and use it in GitHub Desktop.
Save frypf/66636117556fc9bdc092505d14edfe0b to your computer and use it in GitHub Desktop.
#!/usr/bin/swift
// DETERMINE IF MISSION CONTROL IS CURRENTLY ACTIVE (tested on: Mojave, Monterey)
// • returns "true" (exit code 0) if Mission Control is active (all apps / single app / reveal desktop)
// • returns "false" (exit code 1) otherwise
// –––
// • uses hackish check for existence of title-less window owned by Apple's Dock process
//  • see: https://stackoverflow.com/a/66629065
// –––
// @frypf (Oct 2022)
import Cocoa
func missionControlActive() -> Bool {
let windowList = CGWindowListCopyWindowInfo([.optionOnScreenOnly], kCGNullWindowID) as! [[String: Any]]
for window in windowList {
if
// window must be owned by "Dock" process
(window["kCGWindowOwnerName"] as? String) == "Dock",
// ensure the owner is not some other process named "Dock" (seems unlikely though)
let windowOwnerPID = window["kCGWindowOwnerPID"] as? pid_t,
let app = NSRunningApplication(processIdentifier: windowOwnerPID),
app.bundleIdentifier == "com.apple.dock",
// window must have no title
window["kCGWindowName"] == nil
{
return true
}
}
return false
}
let result = missionControlActive()
print(result)
exit(result ? 0 : 1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment