Skip to content

Instantly share code, notes, and snippets.

@EthanSK
Created June 1, 2024 18:51
Show Gist options
  • Save EthanSK/bc2c92ad613c9d7a71c36053ecaec12c to your computer and use it in GitHub Desktop.
Save EthanSK/bc2c92ad613c9d7a71c36053ecaec12c to your computer and use it in GitHub Desktop.
Change macOS Pointer Size (2024+)
import Foundation
import ApplicationServices
import AppKit
let appleScript = """
tell application "System Settings"
activate
reveal anchor "AX_CURSOR_SIZE" of pane id "com.apple.Accessibility-Settings.extension"
delay 1.0
end tell
"""
// Function to execute AppleScript
func runAppleScript(_ script: String) {
let process = Process()
let pipe = Pipe()
process.executableURL = URL(fileURLWithPath: "/usr/bin/osascript")
process.arguments = ["-e", script]
process.standardOutput = pipe
process.standardError = pipe
do {
try process.run()
process.waitUntilExit()
let data = pipe.fileHandleForReading.readDataToEndOfFile()
let output = String(data: data, encoding: .utf8) ?? "No output"
print(output)
adjustCursorSizeSlider(to: parseArguments())
} catch {
print("Failed to run script: \(error.localizedDescription)")
}
}
// Execute the AppleScript to open System Settings and navigate
runAppleScript(appleScript)
func findPIDForSystemSettings() -> pid_t? {
let apps = NSWorkspace.shared.runningApplications
for app in apps {
if app.localizedName == "System Settings" || app.bundleIdentifier == "com.apple.systempreferences" {
return app.processIdentifier
}
}
return nil
}
// Function to recursively search for the slider element
func findElement(with identifier: String, in elements: [AXUIElement]) -> AXUIElement? {
for element in elements {
var currentIdentifier: CFTypeRef?
AXUIElementCopyAttributeValue(element, kAXIdentifierAttribute as CFString, &currentIdentifier)
if let currentIdentifierStr = currentIdentifier as? String, currentIdentifierStr == identifier {
return element
}
var children: CFTypeRef?
AXUIElementCopyAttributeValue(element, kAXChildrenAttribute as CFString, &children)
if let childrenElements = children as? [AXUIElement], let foundElement = findElement(with: identifier, in: childrenElements) {
return foundElement
}
}
return nil
}
// Function to adjust the slider value to match the target value
func adjustSlider(to targetValue: Float, element: AXUIElement) {
var value: CFTypeRef?
AXUIElementCopyAttributeValue(element, kAXValueAttribute as CFString, &value)
var direction = (value as! Float) < targetValue ? 1 : -1
if let currentValue = value as? Float {
while let updatedValue = value as? Float, updatedValue != targetValue {
if updatedValue < targetValue {
AXUIElementPerformAction(element, kAXIncrementAction as CFString)
if (direction == -1) {break} //shouldn't go the other way
} else if updatedValue > targetValue {
AXUIElementPerformAction(element, kAXDecrementAction as CFString)
if (direction == 1) {break} //shouldn't go the other way
}
AXUIElementCopyAttributeValue(element, kAXValueAttribute as CFString, &value)
}
print("Cursor size adjusted to \(targetValue).")
} else {
print("Unable to read current slider value.")
}
}
// Adjust the cursor size slider using the updated adjustSlider function
func adjustCursorSizeSlider(to newValue: Float) {
guard let pid = findPIDForSystemSettings() else {
print("System Settings is not running or not found.")
return
}
let appElement = AXUIElementCreateApplication(pid_t(pid))
var valueElement: CFTypeRef?
AXUIElementCopyAttributeValue(appElement, kAXChildrenAttribute as CFString, &valueElement)
if let elements = valueElement as? [AXUIElement], let cursorSizeSlider = findElement(with: "AX_CURSOR_SIZE", in: elements) {
adjustSlider(to: newValue, element: cursorSizeSlider)
} else {
print("Cursor size slider not found.")
}
}
// Function to parse the target value from command-line arguments
func parseArguments() -> Float {
let args = CommandLine.arguments
if args.count > 1 {
if let targetValue = Float(args[1]) {
return targetValue
} else {
print("Invalid target value provided.")
}
} else {
print("Please provide a target value for the cursor size. Defaulting to 4")
}
return 4
}
@EthanSK
Copy link
Author

EthanSK commented Jun 1, 2024

Just create a XCode CLI project for mac, and give accessibility access in privacy & security

@EthanSK
Copy link
Author

EthanSK commented Jun 1, 2024

Here is the built binary https://drive.google.com/drive/folders/1W7mWXqCiUnwqRbHRT-djfqxGxKBSKVb5?usp=sharing

just run in cli and pass the argument as a float between 1 and 4

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment