Skip to content
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

Add capability to focus window by title #1262

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
15 changes: 15 additions & 0 deletions core/app_switcher/app_switcher.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import os
import re
import shlex
import subprocess
import time
Expand Down Expand Up @@ -316,6 +317,20 @@ def switcher_focus_app(app: ui.App):
raise RuntimeError(f"Can't focus app: {app.name}")
actions.sleep(0.1)

def switcher_focus_app_title(app: str, regex: str):
"""Focus the window whose app name constains app and title matches regex"""
for window in ui.windows():
if (
not (window.hidden)
and (app in window.app.name or app == "*")
and window.title != ""
):
# logging.warn(f'Checking Window: "{window.app.name}" window:"{window.title}" hidden: "{window.hidden}"')
if regex is None or re.search(regex, window.title):
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What's your usecase for passing a regex here?

Copy link
Author

@pturpin pturpin Sep 18, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I currently have two commands that utilize an actual regex vs a plain string.

  1. Where the app title could contain one of two different things.
    focus outlook: user.switcher_focus_app_title("Outlook", "(Inbox|Calendar)")

  2. When I don't want the title to contain a specific string.
    focus studio: user.switcher_focus_app_title("Microsoft Visual Studio", "^(?:(?!Application Management).)*$")

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this seems reasonable given the use cases. @auscompgeek any objection or you think it's ok?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suppose the use-case makes sense. I'm not sure we should make this the primary way, or indeed the only way, of focusing by title, as it'd be prone to user error – it's unclear from the action name that it takes a regex.

window.focus()
return
logging.error(f'(switcher_focus_app_title) Window not found: "{app}" "{regex}"')

def switcher_focus_window(window: ui.Window):
"""Focus window and wait until switch is made"""
window.focus()
Expand Down
1 change: 1 addition & 0 deletions core/windows_and_tabs/window_management.talon
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ window last: app.window_previous()
window close: app.window_close()
window hide: app.window_hide()
focus <user.running_applications>: user.switcher_focus(running_applications)
focus title <phrase>: user.switcher_focus_app_title("*", "{phrase}")
# following only works on windows. Can't figure out how to make it work for mac. No idea what the equivalent for linux would be.
focus$: user.switcher_menu()
running list: user.switcher_toggle_running()
Expand Down