Last active
September 24, 2024 09:17
-
-
Save dagronf/b71315a999678aece610838497fe9d0e to your computer and use it in GitHub Desktop.
Swift command-line tool to find which application is stealing your Mac's focus [macOS, script, steal, focus]
This file contains 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
#!/usr/bin/swift | |
import Foundation | |
import AppKit.NSWorkspace | |
// Returns the name of the frontmost app, or <none> if no app is frontmost | |
func currentFocusApp() -> String { | |
NSWorkspace.shared.frontmostApplication?.localizedName ?? "<none>" | |
} | |
var prev_name = currentFocusApp() | |
Swift.print("New focus: \(prev_name)") | |
// Schedule a timer to check every second | |
let updateTimer = Timer.scheduledTimer(withTimeInterval: 1.0, repeats: true, block: { _ in | |
let new_name = currentFocusApp() | |
if prev_name != new_name { | |
Swift.print("New focus: \(new_name)") | |
prev_name = new_name | |
} | |
}) | |
// Detect Ctrl-C to stop observing | |
let sigintSrc = DispatchSource.makeSignalSource(signal: SIGINT, queue: .main) | |
sigintSrc.setEventHandler { | |
Swift.print("") | |
exit(0) | |
} | |
sigintSrc.resume() | |
RunLoop.current.run(mode: .default, before: .distantFuture) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi there. Can you make the script remember last active application details and switch to it in case "WindowManager" is focused?
Sequoia introduced nasty productivity killer bug when switching between spaces: