Work around macOS issue in which elements lose their window property#75
Open
nriley wants to merge 4 commits intophillco:mainfrom
Open
Work around macOS issue in which elements lose their window property#75nriley wants to merge 4 commits intophillco:mainfrom
nriley wants to merge 4 commits intophillco:mainfrom
Conversation
phillco
approved these changes
May 11, 2025
Owner
phillco
left a comment
There was a problem hiding this comment.
Is there a Talon issue for this we can link?
Comment on lines
+112
to
+135
| while True: | ||
| if hasattr(element, "AXWindow"): | ||
| try: | ||
| app = element.window.app | ||
| except ui.UIErr: | ||
| pass | ||
| else: | ||
| break | ||
| match element.AXRole: | ||
| case "AXWindow": | ||
| with suppress(AttributeError): | ||
| app = element.app | ||
| break | ||
| case "AXApplication": | ||
| apps = ui.apps(name=element.AXTitle) | ||
| if len(apps) == 1: | ||
| app = apps[0] | ||
| break | ||
| if hasattr(element, "AXParent"): | ||
| element = element.parent | ||
| continue | ||
| app = None | ||
| break | ||
|
|
Owner
There was a problem hiding this comment.
I think splitting it out into its own function can improve the readability a bit -- what do you think about this?
def extract_app(element) -> Optional[ui.App]:
# Works around a macOS issue where elements lose their window property
# https://github.com/talonvoice/talon/issues/XX
while element:
try:
return element.window.app
except (ui.UIErr, AttributeError):
pass
match element.AXRole:
case "AXWindow":
with suppress(AttributeError):
return element.app
case "AXApplication":
apps = ui.apps(name=element.AXTitle)
if len(apps) == 1:
return apps[0]
if not hasattr(element, "AXParent"):
break
element = element.parent
return NoneI normally would check for the property rather than catch the AttributeError, but since we're already doing this for ui.UIErr, I think it's cleaner and saves a level of indentation.
Collaborator
Author
There was a problem hiding this comment.
Nope, hadn't filed an issue but have now (talonvoice/talon#689).
Your refactor looks good to me. Unfortunately sometimes checking for properties doesn't always work right with UI elements (see the ticket above), so I've defaulted to catching/suppressing exceptions.
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.
Also use double quotes consistently.