Faster frontmost app detection on macOS via NSWorkspace + CGEvent#127
Merged
Faster frontmost app detection on macOS via NSWorkspace + CGEvent#127
Conversation
Replace the osascript-based 'frontmost' query with a direct call to [NSWorkspace sharedWorkspace].frontmostApplication via the Objective-C runtime. Spawning osascript took tens to hundreds of milliseconds on every hotkey press; the in-process message send is effectively free. Activation and paste keystroke still go through AppleScript and are unchanged. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
ActivateProcess now uses NSRunningApplication runningApplicationWithProcessIdentifier: + activateWithOptions: via objc_msgSend, and SendPasteShortcut posts Cmd+V via CGEventCreateKeyboardEvent + CGEventPost. Both avoid spawning osascript and run fully in-process. The old RunAppleScript helper is no longer needed and is removed. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
There was a problem hiding this comment.
Pull request overview
This PR removes the AppleScript/osascript hot-path on macOS and replaces it with direct in-process AppKit/CoreGraphics calls for (1) detecting the frontmost application, (2) re-activating a target process, and (3) synthesizing Cmd+V.
Changes:
- Replace frontmost-app detection with
NSWorkspace.sharedWorkspace.frontmostApplicationviaobjc_msgSend. - Replace process activation with
NSRunningApplication.runningApplicationWithProcessIdentifier:+activateWithOptions:. - Replace AppleScript-based paste with
CGEventCreateKeyboardEvent+CGEventPost, and remove the now-unused AppleScript runner.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Address PR review feedback: surface a clearer error when dlopen cannot load AppKit, and distinguish it from a plain NSWorkspace class lookup miss. AppKitHandle is now read by the NSWorkspace-miss branch so the field is genuinely used. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Every hotkey press previously spawned one or more
osascriptprocesses to read the frontmost app, reactivate the previous app, and send Cmd+V. Each AppleScript invocation fork/execs a shell and spins up the AppleScript runtime (tens to hundreds of milliseconds), which is noticeable on the hot path from hotkey to paste.This PR replaces all three AppleScript calls with direct in-process macOS APIs:
GetFrontmostApplicationnow calls[NSWorkspace sharedWorkspace].frontmostApplicationthrough the Objective-C runtime (objc_msgSend), readingprocessIdentifier,localizedName, andbundleIdentifierdirectly. AppKit isdlopened defensively on first use so the call works regardless of initialization order (it is a no-op when Avalonia has already linked it).ActivateProcessusesNSRunningApplication.runningApplicationWithProcessIdentifier:+activateWithOptions:withNSApplicationActivateIgnoringOtherApps.SendPasteShortcutposts a synthetic Cmd+V viaCGEventCreateKeyboardEventandCGEventPost, withCFReleaseon all Create-rule objects.The now-unused
RunAppleScripthelper is removed.Notes for reviewers
DllImportis used rather thanLibraryImportto avoid enablingAllowUnsafeBlocksproject-wide (the source generator forLibraryImportrequires it).CGEventPostneeds the same Accessibility/Input Monitoring permission the AppleScriptkeystrokepath already required, so users with existing permissions are unaffected.NSRunningApplication.activateWithOptions:actually relaxes one requirement: the old path also needed System Events automation permission, which is no longer used.