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

Added voice command to create Talon application context #1242

Merged
merged 12 commits into from
Sep 2, 2023
98 changes: 98 additions & 0 deletions plugin/talon_helpers/create_app_context.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
import os
import re
from itertools import islice
from pathlib import Path

from talon import Module, actions, app, ui

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

mod = Module()


@mod.action_class
class Actions:
def talon_create_app_context(platform_suffix: str = None):
"""Create a new directory with talon and python context files for the current application"""
active_app = ui.active_app()
app_name = get_app_name(active_app.name)
app_dir = APPS_DIR / app_name
talon_file = app_dir / f"{app_name}.talon"
python_file = app_dir / f"{get_platform_filename(app_name, platform_suffix)}.py"

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

if not app_dir.is_dir():
os.mkdir(app_dir)

talon_file_created = create_file(talon_file, talon_context)
python_file_created = create_file(python_file, python_context)

if talon_file_created or python_file_created:
actions.user.file_manager_open_directory(str(app_dir))


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"""
os: {os}
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 f"""app: {app_name}
-

"""


def get_platform_filename(app_name: str, platform_suffix: str = None) -> str:
if platform_suffix:
return f"{app_name}_{platform_suffix}"
return 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 get_app_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()


def create_file(path: Path, content: str) -> bool:
if path.is_file():
actions.app.notify(f"Application context file '{path}' already exists")
return False

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

return True
5 changes: 5 additions & 0 deletions plugin/talon_helpers/talon_helpers.talon
Original file line number Diff line number Diff line change
Expand Up @@ -50,5 +50,10 @@ talon dump context:
result = user.talon_get_active_application_info()
clip.set_text(result)

^talon create app context$: user.talon_create_app_context()
^talon create windows app context$: user.talon_create_app_context("win")
^talon create linux app context$: user.talon_create_app_context("linux")
^talon create mac app context$: user.talon_create_app_context("mac")

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