Get The PID Of The App Currently Focused On macOS With Swift
import SwiftUI
struct ContentView: View {
func focusedAppPid() -> Optional<pid_t> {
if let process = NSWorkspace.shared.frontmostApplication {
return process.processIdentifier
} else {
return Optional.none
}
}
var body: some View {
if let pid = focusedAppPid() {
Text(String(pid))
.padding()
} else {
Text("could not get pid")
.padding()
}
}
}
#Preview {
ContentView()
}
-- end of line --