Contents

m1guelpf/watcheye

This library sits on top of the macOS Accessibility API, making it easy to react when the user changes the active application, or when the window the user is focused on changes its title. It also includes some basic utilities for working with browsers.

Installation

Swift Package Manager

The Swift Package Manager allows for developers to easily integrate packages into their Xcode projects and packages; and is also fully integrated into the swift compiler.

SPM Through XCode Project

  • File > Swift Packages > Add Package Dependency
  • Add https://github.com/m1guelpf/WatchEye.git
  • Select "Branch" with "main"

SPM Through Xcode Package

Once you have your Swift package set up, add the Git link within the dependencies value of your Package.swift file.

dependencies: [
    .package(url: "https://github.com/m1guelpf/WatchEye.git", .branch("main"))
]

Adding the necessary permissions

This library makes use of the macOS Accessibility API. To use it, you need to disable the App Sandbox.

You can optionally also get extra data from browsers (like the current URL), which requires com.apple.security.automation.apple-events entitlement. You will also need to add the following to your Info.plist:

<key>NSAppleEventsUsageDescription</key>
<string>$(PRODUCT_NAME) needs this permission to track detailed information like the current website URL.</string>

Getting started πŸš€

The easiest way to get started is to define a delegate and start reacting to events:

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)")
		}
	}
}

License

This project is licensed under the MIT License - see the LICENSE file for details.

Package Metadata

Repository: m1guelpf/watcheye

Default branch: main

README: README.md