Skip to content

Commit

Permalink
Added voice command to create Talon application context
Browse files Browse the repository at this point in the history
  • Loading branch information
AndreasArvidsson committed Aug 4, 2023
1 parent 468fb16 commit b54b22a
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 0 deletions.
83 changes: 83 additions & 0 deletions plugin/talon_helpers/create_app_context.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
from talon import Module, app, actions, ui
from pathlib import Path
from itertools import islice
import os, re

APPS_DIR = Path(__file__).parent.parent.parent / "apps"

mod = Module()


@mod.action_class
class Actions:
def talon_create_app_context():
"""Create a new python context file for the current application"""
active_app = ui.active_app()
app_name = create_name(active_app.name)
app_dir = APPS_DIR / app_name
talon_file = app_dir / f"{app_name}.talon"
python_file = app_dir / f"{app_name}.py"

if app_dir.is_dir():
raise OSError(f"Application directory '{app_name}' already exists")

talon_context = get_talon_context(app_name)
python_context = get_python_context(active_app, app_name)

os.mkdir(app_dir)

with open(talon_file, "w", encoding="utf-8") as file:
file.write(talon_context)

with open(python_file, "w", encoding="utf-8") as file:
file.write(python_context)


def get_python_context(active_app: ui.App, app_name: str) -> str:
return '''\
from talon import Module, Context, actions
mod = Module()
ctx = Context()
mod.apps.{app_name} = r"""
os: {os}
and {app_context}
"""
ctx.matches = r"""
app: {app_name}
"""
# @mod.action_class
# class Actions:
'''.format(
app_name=app_name,
os=app.platform,
app_context=get_app_context(active_app),
)


def get_talon_context(app_name: str) -> str:
return """\
app: {app_name}
-
""".format(
app_name=app_name,
)


def get_app_context(active_app: ui.App) -> str:
if app.platform == "mac":
return f"app.bundle: {active_app.bundle}"
if app.platform == "windows":
return f"app.exe: {active_app.exe.split(os.path.sep)[-1]}"
return f"app.name: {active_app.name}"


def create_name(text: str, max_len=20) -> str:
pattern = re.compile(r"[A-Z][a-z]*|[a-z]+|\d")
return "_".join(
list(islice(pattern.findall(text.replace(".exe", "")), max_len))
).lower()
1 change: 1 addition & 0 deletions plugin/talon_helpers/talon_helpers.talon
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ talon dump context:
^talon copy active app$:
result = user.talon_get_active_application_info()
clip.set_text(result)
^talon create app context$: user.talon_create_app_context()

talon (bug report | report bug):
user.open_url("https://github.com/talonhub/community/issues")

0 comments on commit b54b22a

Please sign in to comment.