Skip to content

Commit

Permalink
Support get app title
Browse files Browse the repository at this point in the history
  • Loading branch information
Innei committed Feb 14, 2024
1 parent 8e9e600 commit fb5b4e8
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 2 deletions.
2 changes: 2 additions & 0 deletions ProcessReporter/Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>NSAccessibilityUsageDescription</key>
<string>我们的应用使用辅助功能服务来读取当前活动窗口的标题,以提供更丰富的功能。</string>
<key>NSApplicationRequiresAccessToUserApplicationFolder</key>
<true/>
</dict>
Expand Down
2 changes: 1 addition & 1 deletion ProcessReporter/ProcessReporter.entitlements
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<plist version="1.0">
<dict>
<key>com.apple.security.app-sandbox</key>
<true/>
<false/>
<key>com.apple.security.files.user-selected.read-only</key>
<true/>
<key>com.apple.security.network.client</key>
Expand Down
2 changes: 1 addition & 1 deletion ProcessReporter/ProcessReporterApp.swift
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ struct swiftui_menu_barApp: App {
var reporter = Reporter.shared

init() {
ProcessInfo.processInfo.processName = "NewProcessName"
ProcessInfo.processInfo.processName = "ProcessReporter"

NotificationManager.requestNotificationAuthorization()

Expand Down
34 changes: 34 additions & 0 deletions ProcessReporter/Utils/ActiveApplicationObserver.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

import Cocoa
import SwiftJotai
import ApplicationServices

class ActiveApplicationObserver {
private var observer: NSObjectProtocol?
Expand All @@ -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?
}
17 changes: 17 additions & 0 deletions ProcessReporter/Utils/Reporter/Reporter+API.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit fb5b4e8

Please sign in to comment.