Skip to content

Commit

Permalink
feat: add more fallback
Browse files Browse the repository at this point in the history
  • Loading branch information
mishamyrt committed Aug 11, 2024
1 parent 588dcbf commit e381738
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 5 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ The following event sources can be subscribed to:

- `screen` — connected displays list change. declares `connected`, `disconnected`, `locked` and `unlocked` events. In `with` takes the display name.
- `audio` — connected audio device list change. declares `connected` and `disconnected` events. In `with` takes the audio device name. Handles both input and output devices.
- `app` — active application change. declares `activated` and `deactivated` events. In `with` takes the app bundle identifier (like `com.microsoft.VSCode`).
- `app` — active application change. declares `activated`, `deactivated`, `launched` and `terminated` events. In `with` takes the app bundle identifier (like `com.microsoft.VSCode`). If the application does not have a bundleID, the name of the executable will be used as the fallback. To see which event the application emits, use the `runon print` command.

## Usage

Expand Down
38 changes: 34 additions & 4 deletions Sources/Events/EventSources/ApplicationSource.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,36 @@ class ApplicationSource: EventSource {

@objc
func handleAppActivate(notification: Notification) {
guard let bundleId = bundleIdentifier(from: notification) else {
guard let bundleId = appIdentifier(from: notification) else {
return
}
emit(kind: "activated", target: bundleId)
}

@objc
func handleAppDeactivate(notification: Notification) {
guard let bundleId = bundleIdentifier(from: notification) else {
guard let bundleId = appIdentifier(from: notification) else {
return
}
emit(kind: "deactivated", target: bundleId)
}

@objc
func handleAppLaunch(notification: Notification) {
guard let bundleId = appIdentifier(from: notification) else {
return
}
emit(kind: "launched", target: bundleId)
}

@objc
func handleAppTerminate(notification: Notification) {
guard let bundleId = appIdentifier(from: notification) else {
return
}
emit(kind: "terminated", target: bundleId)
}

func subscribe() {
NSWorkspace.shared.notificationCenter.addObserver(
self,
Expand All @@ -34,10 +50,24 @@ class ApplicationSource: EventSource {
name: NSWorkspace.didDeactivateApplicationNotification,
object: nil
)
NSWorkspace.shared.notificationCenter.addObserver(
self,
selector: #selector(handleAppLaunch),
name: NSWorkspace.didLaunchApplicationNotification,
object: nil
)
NSWorkspace.shared.notificationCenter.addObserver(
self,
selector: #selector(handleAppTerminate),
name: NSWorkspace.didTerminateApplicationNotification,
object: nil
)
}

private func bundleIdentifier(from: Notification) -> String? {
private func appIdentifier(from: Notification) -> String? {
let app = from.userInfo?["NSWorkspaceApplicationKey"] as? NSRunningApplication
return app?.bundleIdentifier ?? app?.localizedName
return app?.bundleIdentifier
?? app?.localizedName
?? app?.executableURL?.lastPathComponent
}
}

0 comments on commit e381738

Please sign in to comment.