-
-
Notifications
You must be signed in to change notification settings - Fork 119
Ai prompt fix #396
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
Merged
Merged
Ai prompt fix #396
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -654,6 +654,7 @@ function getClipboardImageFingerprint(): { | |
| }; | ||
| } | ||
|
|
||
|
|
||
| } catch {} | ||
| return { fingerprint: '' }; | ||
| } | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,54 +1,230 @@ | ||
| import Foundation | ||
| import ApplicationServices | ||
| import AppKit | ||
|
|
||
| // Use AXUIElementCreateSystemWide to get the focused application directly — | ||
| // no NSWorkspace / AppKit needed, keeps startup overhead minimal (~10 ms). | ||
| let systemElement = AXUIElementCreateSystemWide() | ||
| private let debugEnabled = ProcessInfo.processInfo.environment["GET_SELECTED_TEXT_DEBUG"] == "1" | ||
|
|
||
| var focusedAppRaw: AnyObject? | ||
| guard AXUIElementCopyAttributeValue(systemElement, kAXFocusedApplicationAttribute as CFString, &focusedAppRaw) == .success, | ||
| let focusedApp = focusedAppRaw else { | ||
| exit(0) | ||
| private func dbg(_ message: @autoclosure () -> String) { | ||
| if debugEnabled { | ||
| FileHandle.standardError.write(Data(("[get-selected-text] " + message() + "\n").utf8)) | ||
| } | ||
| } | ||
| let appElement = focusedApp as! AXUIElement | ||
|
|
||
| var focusedRaw: AnyObject? | ||
| guard AXUIElementCopyAttributeValue(appElement, kAXFocusedUIElementAttribute as CFString, &focusedRaw) == .success, | ||
| let focused = focusedRaw else { | ||
| exit(0) | ||
| private func writeAndExit(_ text: String) -> Never { | ||
| if !text.isEmpty { | ||
| FileHandle.standardOutput.write(Data(text.utf8)) | ||
| } | ||
| exit(0) | ||
| } | ||
| let focusedElement = focused as! AXUIElement | ||
|
|
||
| // 1. kAXSelectedTextAttribute — supported by most native text controls. | ||
| var selectedRaw: AnyObject? | ||
| if AXUIElementCopyAttributeValue(focusedElement, kAXSelectedTextAttribute as CFString, &selectedRaw) == .success, | ||
| let text = selectedRaw as? String, !text.isEmpty { | ||
| FileHandle.standardOutput.write(text.data(using: .utf8)!) | ||
| exit(0) | ||
| private func copyAttribute(_ element: AXUIElement, _ attribute: CFString) -> AnyObject? { | ||
| var raw: AnyObject? | ||
| let err = AXUIElementCopyAttributeValue(element, attribute, &raw) | ||
| if err != .success { | ||
| dbg("attribute \(attribute) err=\(err.rawValue)") | ||
| return nil | ||
| } | ||
| return raw | ||
| } | ||
|
|
||
| // 2. Fall back: derive selection from kAXSelectedTextRangeAttribute + kAXValueAttribute. | ||
| // Works for controls that expose a range but not the text slice directly. | ||
| var rangeRaw: AnyObject? | ||
| var valueRaw: AnyObject? | ||
| guard AXUIElementCopyAttributeValue(focusedElement, kAXSelectedTextRangeAttribute as CFString, &rangeRaw) == .success, | ||
| let rangeVal = rangeRaw, | ||
| AXUIElementCopyAttributeValue(focusedElement, kAXValueAttribute as CFString, &valueRaw) == .success, | ||
| let fullText = valueRaw as? String else { | ||
| exit(0) | ||
| private func copyParameterizedAttribute(_ element: AXUIElement, _ attribute: CFString, _ parameter: AnyObject) -> AnyObject? { | ||
| var raw: AnyObject? | ||
| let err = AXUIElementCopyParameterizedAttributeValue(element, attribute, parameter, &raw) | ||
| if err != .success { | ||
| dbg("parameterized \(attribute) err=\(err.rawValue)") | ||
| return nil | ||
| } | ||
| return raw | ||
| } | ||
|
|
||
| var cfRange = CFRange(location: 0, length: 0) | ||
| AXValueGetValue(rangeVal as! AXValue, .cfRange, &cfRange) | ||
| guard cfRange.length > 0 else { exit(0) } | ||
| private func stringFromAXResult(_ raw: AnyObject?) -> String? { | ||
| guard let raw else { return nil } | ||
| if let text = raw as? String { return text.isEmpty ? nil : text } | ||
| if let attributed = raw as? NSAttributedString { | ||
| let text = attributed.string | ||
| return text.isEmpty ? nil : text | ||
| } | ||
| if CFGetTypeID(raw) == CFAttributedStringGetTypeID() { | ||
| let attributed = raw as! NSAttributedString | ||
| let text = attributed.string | ||
| return text.isEmpty ? nil : text | ||
| } | ||
| return nil | ||
| } | ||
|
|
||
| private func selectedRangeValue(_ element: AXUIElement) -> AnyObject? { | ||
| guard let rangeValue = copyAttribute(element, kAXSelectedTextRangeAttribute as CFString) else { | ||
| return nil | ||
| } | ||
| var range = CFRange(location: 0, length: 0) | ||
| guard AXValueGetValue(rangeValue as! AXValue, .cfRange, &range), range.length > 0 else { | ||
| return nil | ||
| } | ||
| return rangeValue | ||
| } | ||
|
|
||
| private func selectedTextViaValueRange(_ element: AXUIElement, _ rangeValue: AnyObject) -> String? { | ||
| guard let fullText = copyAttribute(element, kAXValueAttribute as CFString) as? String else { | ||
| return nil | ||
| } | ||
| var range = CFRange(location: 0, length: 0) | ||
| guard AXValueGetValue(rangeValue as! AXValue, .cfRange, &range), range.length > 0 else { | ||
| return nil | ||
| } | ||
|
|
||
| // CFRange from AX text controls is expressed in UTF-16 offsets. | ||
| let utf16 = fullText.utf16 | ||
| guard let startIdx = utf16.index(utf16.startIndex, offsetBy: range.location, limitedBy: utf16.endIndex), | ||
| let endIdx = utf16.index(startIdx, offsetBy: range.length, limitedBy: utf16.endIndex), | ||
| let slice = String(utf16[startIdx..<endIdx]), | ||
| !slice.isEmpty else { | ||
| return nil | ||
| } | ||
| return slice | ||
| } | ||
|
|
||
| private func selectedTextViaRangeParameterizedAttribute(_ element: AXUIElement, _ rangeValue: AnyObject) -> String? { | ||
| let attributes: [CFString] = [ | ||
| kAXStringForRangeParameterizedAttribute as CFString, | ||
| kAXAttributedStringForRangeParameterizedAttribute as CFString, | ||
| "AXStringForRange" as CFString, | ||
| "AXAttributedStringForRange" as CFString, | ||
| ] | ||
| for attribute in attributes { | ||
| if let text = stringFromAXResult(copyParameterizedAttribute(element, attribute, rangeValue)) { | ||
| return text | ||
| } | ||
| } | ||
| return nil | ||
| } | ||
|
|
||
| private func selectedTextViaTextMarkerRange(_ element: AXUIElement) -> String? { | ||
| guard let markerRange = copyAttribute(element, "AXSelectedTextMarkerRange" as CFString) else { | ||
| return nil | ||
| } | ||
|
|
||
| let attributes: [CFString] = [ | ||
| "AXStringForTextMarkerRange" as CFString, | ||
| "AXAttributedStringForTextMarkerRange" as CFString, | ||
| ] | ||
| for attribute in attributes { | ||
| if let text = stringFromAXResult(copyParameterizedAttribute(element, attribute, markerRange)) { | ||
| return text | ||
| } | ||
| } | ||
| return nil | ||
| } | ||
|
|
||
| private func selectedTextFromElement(_ element: AXUIElement) -> String? { | ||
| let role = copyAttribute(element, kAXRoleAttribute as CFString) as? String ?? "" | ||
| let subrole = copyAttribute(element, kAXSubroleAttribute as CFString) as? String ?? "" | ||
| if role == "AXSecureTextField" || subrole == (kAXSecureTextFieldSubrole as String) { | ||
| return nil | ||
| } | ||
|
|
||
| if let text = stringFromAXResult(copyAttribute(element, kAXSelectedTextAttribute as CFString)) { | ||
| return text | ||
| } | ||
| if let text = selectedTextViaTextMarkerRange(element) { | ||
| return text | ||
| } | ||
| if let rangeValue = selectedRangeValue(element) { | ||
| if let text = selectedTextViaRangeParameterizedAttribute(element, rangeValue) { | ||
| return text | ||
| } | ||
| if let text = selectedTextViaValueRange(element, rangeValue) { | ||
| return text | ||
| } | ||
| } | ||
| return nil | ||
| } | ||
|
|
||
| private func axElementFromRaw(_ raw: AnyObject?) -> AXUIElement? { | ||
| guard let raw, CFGetTypeID(raw) == AXUIElementGetTypeID() else { | ||
| return nil | ||
| } | ||
| return (raw as! AXUIElement) | ||
| } | ||
|
|
||
| private func enqueueFocusedChild(of element: AXUIElement, depth: Int, into queue: inout [(AXUIElement, Int)]) { | ||
| if let focused = axElementFromRaw(copyAttribute(element, kAXFocusedUIElementAttribute as CFString)) { | ||
| queue.append((focused, depth + 1)) | ||
| } | ||
| } | ||
|
|
||
| // CFRange uses UTF-16 offsets. | ||
| let utf16 = fullText.utf16 | ||
| guard let startIdx = utf16.index(utf16.startIndex, offsetBy: cfRange.location, limitedBy: utf16.endIndex), | ||
| let endIdx = utf16.index(startIdx, offsetBy: cfRange.length, limitedBy: utf16.endIndex) else { | ||
| exit(0) | ||
| private func enqueueChildren(of element: AXUIElement, depth: Int, into queue: inout [(AXUIElement, Int)]) { | ||
| guard let children = copyAttribute(element, kAXChildrenAttribute as CFString) as? [AXUIElement] else { | ||
| return | ||
| } | ||
| for child in children { | ||
| queue.append((child, depth + 1)) | ||
| } | ||
| } | ||
| if let slice = String(utf16[startIdx..<endIdx]), !slice.isEmpty { | ||
| FileHandle.standardOutput.write(slice.data(using: .utf8)!) | ||
|
|
||
| private func findSelectedText(from roots: [AXUIElement]) -> String? { | ||
| var queue = roots.map { ($0, 0) } | ||
| var inspected = 0 | ||
| let maxDepth = 8 | ||
| let maxElements = 240 | ||
|
|
||
| while let (element, depth) = queue.first { | ||
| queue.removeFirst() | ||
| inspected += 1 | ||
| if inspected > maxElements { break } | ||
|
|
||
| if let text = selectedTextFromElement(element) { | ||
| dbg("selected text found at depth \(depth)") | ||
| return text | ||
| } | ||
| if depth >= maxDepth { continue } | ||
|
|
||
| enqueueFocusedChild(of: element, depth: depth, into: &queue) | ||
| enqueueChildren(of: element, depth: depth, into: &queue) | ||
| } | ||
| return nil | ||
| } | ||
|
|
||
| private func frontmostApplicationElement() -> AXUIElement? { | ||
| guard let frontApp = NSWorkspace.shared.frontmostApplication else { | ||
| return nil | ||
| } | ||
| let appElement = AXUIElementCreateApplication(frontApp.processIdentifier) | ||
|
|
||
| // Chromium/Electron apps often expose richer text-marker attributes only | ||
| // after these AX opt-in flags have been set. They are idempotent. | ||
| AXUIElementSetAttributeValue(appElement, "AXEnhancedUserInterface" as CFString, kCFBooleanTrue) | ||
| AXUIElementSetAttributeValue(appElement, "AXManualAccessibility" as CFString, kCFBooleanTrue) | ||
|
|
||
| return appElement | ||
| } | ||
| exit(0) | ||
|
|
||
| private func focusedElementRoots() -> [AXUIElement] { | ||
| var roots: [AXUIElement] = [] | ||
|
|
||
| if let appElement = frontmostApplicationElement() { | ||
| var focused = axElementFromRaw(copyAttribute(appElement, kAXFocusedUIElementAttribute as CFString)) | ||
| if focused == nil { | ||
| Thread.sleep(forTimeInterval: 0.06) | ||
| focused = axElementFromRaw(copyAttribute(appElement, kAXFocusedUIElementAttribute as CFString)) | ||
| } | ||
| if let focused { | ||
| roots.append(focused) | ||
| } | ||
| if let focusedWindow = axElementFromRaw(copyAttribute(appElement, kAXFocusedWindowAttribute as CFString)) { | ||
| roots.append(focusedWindow) | ||
| } | ||
| } | ||
|
|
||
| let systemElement = AXUIElementCreateSystemWide() | ||
| if let focused = axElementFromRaw(copyAttribute(systemElement, kAXFocusedUIElementAttribute as CFString)) { | ||
| roots.append(focused) | ||
| } | ||
|
|
||
| return roots | ||
| } | ||
|
|
||
| if let text = findSelectedText(from: focusedElementRoots()) { | ||
| writeAndExit(text) | ||
| } | ||
|
|
||
| writeAndExit("") | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
findSelectedTextnow breadth-first scans everyAXChildrennode and returns the first non-empty selection it finds, even if that element is not focused. In multi-pane apps (editors, split views, sidebars), an unfocused control can keep stale selected text while the active control exposes selection only via marker APIs, so this search can return unrelated text and cause cursor-prompt rewrites to target the wrong content. The lookup should constrain accepted matches to the focused chain (or otherwise validate focus ownership) before returning.Useful? React with 👍 / 👎.