该库基于 macOS Accessibility API,可以轻松地对用户切换活动应用程序或用户聚焦的窗口标题发生变化时做出反应。它还包括一些用于处理浏览器的基本实用程序。
Swift 包管理器允许开发者轻松地将包集成到他们的 Xcode 项目和包中;并且也完全集成到 swift 编译器中。
一旦你设置好你的 Swift 包,将 Git 链接添加到 Package.swift 文件中的 dependencies 值中。
dependencies: [
.package(url: "https://github.com/m1guelpf/WatchEye.git", .branch("main"))
]
该库使用 macOS Accessibility API。 要使用它,你需要禁用 App 沙箱。
你可以选择从浏览器获取额外数据(例如当前 URL),这需要 com.apple.security.automation.apple-events
授权。 你还需要将以下内容添加到你的 Info.plist
文件中
<key>NSAppleEventsUsageDescription</key>
<string>$(PRODUCT_NAME) needs this permission to track detailed information like the current website URL.</string>
最简单的入门方法是定义一个代理并开始对事件做出反应
import AppKit
import WatchEye
import Foundation
class ExampleWatchEyeDelegate {
let watchEye: WatchEye
init() {
watchEye = WatchEye()
watchEye.delegate = self
}
}
extension ExampleWatchEyeDelegate: WatchEyeDelegate {
func watchEyeDidReceiveAccessibilityPermissions(_: WatchEye) {
print("Accessibility permissions granted!")
}
func watchEye(_: WatchEye, didFocusApplication app: NSRunningApplication) {
print("\(app.bundleIdentifier!) is now in focus")
}
func watchEye(_: WatchEye, didChangeTitleOf app: NSRunningApplication, newTitle title: String) {
if app.browser?.isIncognito(windowTitle: title) == true { return }
print("Title of \(app.bundleIdentifier!) changed to \(title)")
if let url = app.browser?.getURL() {
print("URL of \(app.bundleIdentifier!) is now \(url)")
}
}
}
本项目基于 MIT 许可证 - 有关详细信息,请参见 LICENSE 文件。