Skip to content

Swift 6 version #46

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 40 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
40 commits
Select commit Hold shift + click to select a range
e7d61e9
(bump) swift version
mredig Jun 13, 2025
24525c3
(refactor) typed throws
mredig Jun 13, 2025
a3b1096
(feat) UIElement hashable
mredig Jun 13, 2025
cbb4772
(nit) mark retroactive
mredig Jun 13, 2025
0b6f36b
(refactor) change most enums to RawRep structs
mredig Jun 14, 2025
134ea9e
(fix) update Package.swift conventions
mredig Jun 14, 2025
b3d7e21
(refactor) attributes
mredig Jun 14, 2025
a73bcbf
(refactor) attributeIsSupported
mredig Jun 14, 2025
e2345a6
(refactor) attributeIsSettable
mredig Jun 14, 2025
8bbd46b
(refactor) attribute
mredig Jun 14, 2025
7076fb6
(feat) add AXValueCompatibility
mredig Jun 14, 2025
9f71e09
(refactor) setAttribute
mredig Jun 14, 2025
c108bfc
(refactor) multiattr stuff
mredig Jun 14, 2025
bacb302
(refactor) arrayAttribute
mredig Jun 14, 2025
c69dc90
(refactor) valuesForAttribute
mredig Jun 14, 2025
1b16055
(refactor) valueCountForAttribute
mredig Jun 14, 2025
7860874
(refactor) parameterizedAttributes
mredig Jun 14, 2025
5d6ef03
(refactor) parameterizedAttribute
mredig Jun 14, 2025
2842e37
(refactor) actions
mredig Jun 14, 2025
e9e5bdf
(refactor) restore original order of operations
mredig Jun 14, 2025
4d5bde6
(nit) mark deprecations
mredig Jun 14, 2025
67e82ea
(refactor) actionDescription
mredig Jun 14, 2025
f0fd34c
(refactor) performAction
mredig Jun 14, 2025
d747e6c
(feat) elementAtPosition(_ position: CGPoint)
mredig Jun 14, 2025
49a9132
(refactor) revery protocol constraint
mredig Jun 14, 2025
bf8ee46
(fix) resolve some deprecations in sample code
mredig Jun 14, 2025
a42fd86
(fix) indentation as original project intended
mredig Jun 14, 2025
fc406f0
(nit) delete empty whitespace
mredig Jun 14, 2025
57e87c1
(refactor) organize file to group all deprecated Strings methods toge…
mredig Jun 14, 2025
85485c5
(refactor) update docs
mredig Jun 14, 2025
cd7f589
(nit) remove empty whitespace
mredig Jun 14, 2025
9142fb2
(refactor) delete commented code
mredig Jun 14, 2025
033445b
(refactor) dry up trusted prompt check
mredig Jun 14, 2025
46b4676
(fix) replace sample String call
mredig Jun 14, 2025
d05582a
(fix) resolve concurrency race condition
mredig Jun 14, 2025
85bc750
(fix) typed throws on overlooked methods
mredig Jun 15, 2025
9c71344
(fix) operation order
mredig Jun 15, 2025
5d73ab4
(fix) spacing
mredig Jun 15, 2025
124ef9b
(refactor) mutate a bunch of Attribute comments to docs
mredig Jun 15, 2025
cd1f582
(refactor) swift 6.1 upgrade
mredig Jun 14, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion AXSwiftExample/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ class ApplicationDelegate: NSObject, NSApplicationDelegate {
if let title: String = try! app.attribute(.title) {
NSLog("title: \(title)")
}
NSLog("multi: \(try! app.getMultipleAttributes(["AXRole", "asdf", "AXTitle"]))")
NSLog("multi: \(try! app.getMultipleAttributes([.role, .init(rawValue: "asdf"), .title]))")
NSLog("multi: \(try! app.getMultipleAttributes(.role, .title))")

// Try to set an unsettable attribute
Expand Down
31 changes: 25 additions & 6 deletions AXSwiftObserverExample/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@ class AppDelegate: NSObject, NSApplicationDelegate {

func applicationDidFinishLaunching(_ aNotification: Notification) {
let app = Application.allForBundleID("com.apple.finder").first!
guard UIElement.isProcessTrusted(withPrompt: true) else {
NSLog("No accessibility API permission, exiting")
NSRunningApplication.current.terminate()
return
}

do {
try startWatcher(app)
Expand All @@ -16,9 +21,24 @@ class AppDelegate: NSObject, NSApplicationDelegate {
}
}

private class Sendly<T>: @unchecked Sendable {
private let lock = NSLock()
private var _value: T
var value: T {
get { lock.withLock { _value } }
set { lock.withLock { _value = newValue } }
}

init(_ value: T) {
lock.lock()
defer { lock.unlock() }
self._value = value
}
}

func startWatcher(_ app: Application) throws {
var updated = false
observer = app.createObserver { (observer: Observer, element: UIElement, event: AXNotification, info: [String: AnyObject]?) in
let updated = Sendly(false)
observer = app.createObserver { (observer: Observer, element: UIElement, event: UIElement.AXNotification, info: [String: AnyObject]?) in
var elementDesc: String!
if let role = try? element.role()!, role == .window {
elementDesc = "\(element) \"\(try! (element.attribute(.title) as String?)!)\""
Expand All @@ -38,12 +58,12 @@ class AppDelegate: NSObject, NSApplicationDelegate {
}

// Group simultaneous events together with --- lines
if !updated {
updated = true
if !updated.value {
updated.value = true
// Set this code to run after the current run loop, which is dispatching all notifications.
DispatchQueue.main.async {
print("---")
updated = false
updated.value = false
}
}
}
Expand All @@ -55,5 +75,4 @@ class AppDelegate: NSObject, NSApplicationDelegate {
func applicationWillTerminate(_ aNotification: Notification) {
// Insert code here to tear down your application
}

}
11 changes: 8 additions & 3 deletions Package.swift
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
// swift-tools-version:4.0
// swift-tools-version:6.1

import PackageDescription

let package = Package(
name: "AXSwift",
platforms: [
.macOS(.v10_15)
],
products: [
.library(
name: "AXSwift",
Expand All @@ -13,10 +16,12 @@ let package = Package(
.target(
name: "AXSwift",
path: "Sources"),
.target(name: "AXSwiftExample",
.executableTarget(
name: "AXSwiftExample",
dependencies: ["AXSwift"],
path: "AXSwiftExample"),
.target(name: "AXSwiftObserverExample",
.executableTarget(
name: "AXSwiftObserverExample",
dependencies: ["AXSwift"],
path: "AXSwiftObserverExample"),
]
Expand Down
4 changes: 1 addition & 3 deletions Sources/AXSwift.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,5 @@ import Cocoa

@discardableResult
public func checkIsProcessTrusted(prompt: Bool = false) -> Bool {
let promptKey = kAXTrustedCheckOptionPrompt.takeUnretainedValue() as String
let opts = [promptKey: prompt] as CFDictionary
return AXIsProcessTrustedWithOptions(opts)
UIElement.isProcessTrusted(withPrompt: prompt)
}
6 changes: 3 additions & 3 deletions Sources/Application.swift
Original file line number Diff line number Diff line change
Expand Up @@ -75,13 +75,13 @@ public final class Application: UIElement {
/// Returns a list of the application's visible windows.
/// - returns: An array of `UIElement`s, one for every visible window. Or `nil` if the list
/// cannot be retrieved.
public func windows() throws -> [UIElement]? {
let axWindows: [AXUIElement]? = try attribute("AXWindows")
public func windows() throws(AXError) -> [UIElement]? {
let axWindows: [AXUIElement]? = try attribute(.windows)
return axWindows?.map({ UIElement($0) })
}

/// Returns the element at the specified top-down coordinates, or nil if there is none.
public override func elementAtPosition(_ x: Float, _ y: Float) throws -> UIElement? {
public override func elementAtPosition(_ x: Float, _ y: Float) throws(AXError) -> UIElement? {
return try super.elementAtPosition(x, y)
}
}
Loading