From fb5b4e8ef138afae36d7f4b7c21909b3ae3896da Mon Sep 17 00:00:00 2001 From: Innei Date: Wed, 14 Feb 2024 14:15:36 +0800 Subject: [PATCH] Support get app title --- ProcessReporter/Info.plist | 2 ++ ProcessReporter/ProcessReporter.entitlements | 2 +- ProcessReporter/ProcessReporterApp.swift | 2 +- .../Utils/ActiveApplicationObserver.swift | 34 +++++++++++++++++++ .../Utils/Reporter/Reporter+API.swift | 17 ++++++++++ 5 files changed, 55 insertions(+), 2 deletions(-) diff --git a/ProcessReporter/Info.plist b/ProcessReporter/Info.plist index 5f0431c..edd334c 100644 --- a/ProcessReporter/Info.plist +++ b/ProcessReporter/Info.plist @@ -2,6 +2,8 @@ + NSAccessibilityUsageDescription + 我们的应用使用辅助功能服务来读取当前活动窗口的标题,以提供更丰富的功能。 NSApplicationRequiresAccessToUserApplicationFolder diff --git a/ProcessReporter/ProcessReporter.entitlements b/ProcessReporter/ProcessReporter.entitlements index 40b639e..997a18c 100644 --- a/ProcessReporter/ProcessReporter.entitlements +++ b/ProcessReporter/ProcessReporter.entitlements @@ -3,7 +3,7 @@ com.apple.security.app-sandbox - + com.apple.security.files.user-selected.read-only com.apple.security.network.client diff --git a/ProcessReporter/ProcessReporterApp.swift b/ProcessReporter/ProcessReporterApp.swift index f3c71b2..51dd77f 100644 --- a/ProcessReporter/ProcessReporterApp.swift +++ b/ProcessReporter/ProcessReporterApp.swift @@ -27,7 +27,7 @@ struct swiftui_menu_barApp: App { var reporter = Reporter.shared init() { - ProcessInfo.processInfo.processName = "NewProcessName" + ProcessInfo.processInfo.processName = "ProcessReporter" NotificationManager.requestNotificationAuthorization() diff --git a/ProcessReporter/Utils/ActiveApplicationObserver.swift b/ProcessReporter/Utils/ActiveApplicationObserver.swift index 82a34a3..33ec496 100644 --- a/ProcessReporter/Utils/ActiveApplicationObserver.swift +++ b/ProcessReporter/Utils/ActiveApplicationObserver.swift @@ -7,6 +7,7 @@ import Cocoa import SwiftJotai +import ApplicationServices class ActiveApplicationObserver { private var observer: NSObjectProtocol? @@ -32,4 +33,37 @@ class ActiveApplicationObserver { guard observer != nil else { return } NSWorkspace.shared.notificationCenter.removeObserver(observer) } + + + public func getActiveApplicationInfo() -> ActiveApplicationInfo { + + guard let activeApp = NSWorkspace.shared.frontmostApplication else { + + debugPrint("no frontmost app") + return ActiveApplicationInfo() + } + + let appPID = activeApp.processIdentifier + + + var appRef: AXUIElement? + appRef = AXUIElementCreateApplication(appPID) + + var window: CFTypeRef? + var title: String? + + + let result = AXUIElementCopyAttributeValue(appRef!, kAXFocusedWindowAttribute as CFString, &window) + if result == .success, let window = window { + var windowTitle: CFTypeRef? + AXUIElementCopyAttributeValue(window as! AXUIElement, kAXTitleAttribute as CFString, &windowTitle) + title = windowTitle as? String + } + + return ActiveApplicationInfo(title: title) + } +} + +struct ActiveApplicationInfo { + var title: String? } diff --git a/ProcessReporter/Utils/Reporter/Reporter+API.swift b/ProcessReporter/Utils/Reporter/Reporter+API.swift index 5911faf..43e1f08 100644 --- a/ProcessReporter/Utils/Reporter/Reporter+API.swift +++ b/ProcessReporter/Utils/Reporter/Reporter+API.swift @@ -13,6 +13,13 @@ struct PostData: Codable, Equatable { var key: String var process: String? var media: MediaInfo? + var meta: PostMetaData? +} + +struct PostMetaData: Codable, Equatable { + var iconUrl: String? + var iconBase64: String? + var description: String? } struct MediaInfo: Codable, Equatable { @@ -72,12 +79,22 @@ extension Reporter { if processEnabled { let workspace = NSWorkspace.shared let processName = workspace.frontmostApplication?.localizedName + let processTitle = ActiveApplicationObserver.shared.getActiveApplicationInfo().title + + guard let processName else { debugPrint("app unkown") return } postData.process = processName + var description: String? = nil + if let processTitle = processTitle { + description = "\n-> \(processTitle)" + } + postData.meta = PostMetaData( + description: processTitle + ) } if mediaEnabled {