init commit
This commit is contained in:
@@ -0,0 +1,99 @@
|
||||
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":
|
||||
executable = os.path.basename(active_app.exe)
|
||||
return f"app.exe: /^{re.escape(executable.lower())}$/i"
|
||||
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.removesuffix(".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
|
||||
@@ -0,0 +1,170 @@
|
||||
import os
|
||||
import platform
|
||||
import pprint
|
||||
import re
|
||||
from itertools import islice
|
||||
from typing import Union
|
||||
|
||||
from talon import Module, actions, app, clip, registry, scope, speech_system, ui
|
||||
from talon.grammar import Phrase
|
||||
from talon.scripting.types import ListTypeFull
|
||||
|
||||
pp = pprint.PrettyPrinter()
|
||||
|
||||
|
||||
mod = Module()
|
||||
pattern = re.compile(r"[A-Z][a-z]*|[a-z]+|\d")
|
||||
|
||||
|
||||
def create_name(text, max_len=20):
|
||||
return "_".join(list(islice(pattern.findall(text), max_len))).lower()
|
||||
|
||||
|
||||
@mod.action_class
|
||||
class Actions:
|
||||
def talon_add_context_clipboard_python():
|
||||
"""Adds os-specific context info to the clipboard for the focused app for .py files. Assumes you've a Module named mod declared."""
|
||||
friendly_name = actions.app.name()
|
||||
# print(actions.app.executable())
|
||||
executable = os.path.basename(actions.app.executable())
|
||||
app_name = create_name(friendly_name.removesuffix(".exe"))
|
||||
if app.platform == "mac":
|
||||
result = 'mod.apps.{} = """\nos: mac\nand app.bundle: {}\n"""'.format(
|
||||
app_name, actions.app.bundle()
|
||||
)
|
||||
elif app.platform == "windows":
|
||||
result = 'mod.apps.{} = r"""\nos: windows\nand app.name: {}\nos: windows\nand app.exe: /^{}$/i\n"""'.format(
|
||||
app_name, friendly_name, re.escape(executable.lower())
|
||||
)
|
||||
else:
|
||||
result = 'mod.apps.{} = """\nos: {}\nand app.name: {}\n"""'.format(
|
||||
app_name, app.platform, friendly_name
|
||||
)
|
||||
|
||||
clip.set_text(result)
|
||||
|
||||
def talon_add_context_clipboard():
|
||||
"""Adds os-specific context info to the clipboard for the focused app for .talon files"""
|
||||
friendly_name = actions.app.name()
|
||||
# print(actions.app.executable())
|
||||
executable = os.path.basename(actions.app.executable())
|
||||
if app.platform == "mac":
|
||||
result = f"os: mac\nand app.bundle: {actions.app.bundle()}\n"
|
||||
elif app.platform == "windows":
|
||||
result = "os: windows\nand app.name: {}\nos: windows\nand app.exe: /^{}$/i\n".format(
|
||||
friendly_name, re.escape(executable.lower())
|
||||
)
|
||||
else:
|
||||
result = f"os: {app.platform}\nand app.name: {friendly_name}\n"
|
||||
|
||||
clip.set_text(result)
|
||||
|
||||
def talon_sim_phrase(phrase: Union[str, Phrase]):
|
||||
"""Sims the phrase in the active app and dumps to the log"""
|
||||
print("**** Simulated Phrase **** ")
|
||||
print(speech_system._sim(str(phrase)))
|
||||
print("*************************")
|
||||
|
||||
def talon_action_find(action: str):
|
||||
"""Runs action.find for the provided action and dumps to the log"""
|
||||
print(f"**** action.find{action} **** ")
|
||||
print(actions.find(action))
|
||||
print("***********************")
|
||||
|
||||
def talon_debug_list(name: str):
|
||||
"""Dumps the contents of list to the console"""
|
||||
print(f"**** Dumping list {name} **** ")
|
||||
|
||||
print(str(registry.lists[name]))
|
||||
print("***********************")
|
||||
|
||||
def talon_debug_tags():
|
||||
"""Dumps the active tags to the console"""
|
||||
print("**** Dumping active tags *** ")
|
||||
print(str(registry.tags))
|
||||
print("***********************")
|
||||
|
||||
def talon_debug_modes():
|
||||
"""Dumps active modes to the console"""
|
||||
print("**** Active modes ****")
|
||||
print(scope.get("mode"))
|
||||
print("***********************")
|
||||
|
||||
def talon_debug_scope(name: str):
|
||||
"""Dumps the active scope information to the console"""
|
||||
print(f"**** Dumping {name} scope ****")
|
||||
print(scope.get(name))
|
||||
print("***********************")
|
||||
|
||||
def talon_copy_list(name: str):
|
||||
"""Dumps the contents of list to the console"""
|
||||
print(f"**** Copied list {name} **** ")
|
||||
clip.set_text(pp.pformat(registry.lists[name]))
|
||||
print("***********************")
|
||||
|
||||
def talon_debug_setting(name: str):
|
||||
"""Dumps the current setting to the console"""
|
||||
print(f"**** Dumping setting {name} **** ")
|
||||
print(registry.settings[name])
|
||||
print("***********************")
|
||||
|
||||
def talon_debug_all_settings():
|
||||
"""Dumps all settings to the console"""
|
||||
print("**** Dumping settings **** ")
|
||||
print(str(registry.settings))
|
||||
print("***********************")
|
||||
|
||||
def talon_get_active_context() -> str:
|
||||
"""Returns active context info"""
|
||||
name = actions.app.name()
|
||||
executable = actions.app.executable()
|
||||
bundle = actions.app.bundle()
|
||||
title = actions.win.title()
|
||||
hostname = scope.get("hostname")
|
||||
result = f"Name: {name}\nExecutable: {executable}\nBundle: {bundle}\nTitle: {title}\nhostname: {hostname}"
|
||||
return result
|
||||
|
||||
def talon_get_hostname() -> str:
|
||||
"""Returns the hostname"""
|
||||
hostname = scope.get("hostname")
|
||||
return hostname
|
||||
|
||||
def talon_get_active_application_info() -> str:
|
||||
"""Returns all active app info to the cliboard"""
|
||||
result = str(ui.active_app())
|
||||
result += "\nActive window: " + str(ui.active_window())
|
||||
result += "\nWindows: " + str(ui.active_app().windows())
|
||||
result += "\nName: " + actions.app.name()
|
||||
result += "\nExecutable: " + actions.app.executable()
|
||||
result += "\nBundle: " + actions.app.bundle()
|
||||
result += "\nTitle: " + actions.win.title()
|
||||
return result
|
||||
|
||||
def talon_get_active_window_class_name() -> str:
|
||||
"""Returns the class name of the active window"""
|
||||
return ui.active_window().cls
|
||||
|
||||
def talon_version_info() -> str:
|
||||
"""Returns talon & operation system verison information"""
|
||||
result = (
|
||||
f"Version: {app.version}, Branch: {app.branch}, OS: {platform.platform()}"
|
||||
)
|
||||
return result
|
||||
|
||||
def talon_pretty_print(obj: object):
|
||||
"""Uses pretty print to dump an object"""
|
||||
pp.pprint(obj)
|
||||
|
||||
def talon_pretty_format(obj: object):
|
||||
"""Pretty formats an object"""
|
||||
return pp.pformat(obj)
|
||||
|
||||
def talon_debug_app_windows(app: str):
|
||||
"""Pretty prints the application windows"""
|
||||
apps = ui.apps(name=app, background=False)
|
||||
for app in apps:
|
||||
pp.pprint(app.windows())
|
||||
|
||||
def talon_get_active_registry_list(name: str) -> ListTypeFull:
|
||||
"""Returns the active list from the Talon registry"""
|
||||
return registry.lists[name][-1]
|
||||
@@ -0,0 +1,64 @@
|
||||
talon check updates: menu.check_for_updates()
|
||||
# the debug window is only available in the talon beta
|
||||
talon open debug: menu.open_debug_window()
|
||||
talon open log: menu.open_log()
|
||||
talon open rebel: menu.open_repl()
|
||||
talon home: menu.open_talon_home()
|
||||
talon copy context pie: user.talon_add_context_clipboard_python()
|
||||
talon copy context: user.talon_add_context_clipboard()
|
||||
talon copy name:
|
||||
name = app.name()
|
||||
clip.set_text(name)
|
||||
talon copy executable:
|
||||
executable = app.executable()
|
||||
clip.set_text(executable)
|
||||
talon copy bundle:
|
||||
bundle = app.bundle()
|
||||
clip.set_text(bundle)
|
||||
talon copy title:
|
||||
title = win.title()
|
||||
clip.set_text(title)
|
||||
talon copy class:
|
||||
class_name = user.talon_get_active_window_class_name()
|
||||
clip.set_text(class_name)
|
||||
talon dump version:
|
||||
result = user.talon_version_info()
|
||||
print(result)
|
||||
talon insert version:
|
||||
result = user.talon_version_info()
|
||||
user.paste(result)
|
||||
talon dump context:
|
||||
result = user.talon_get_active_context()
|
||||
print(result)
|
||||
^talon test last$:
|
||||
phrase = user.history_get(1)
|
||||
user.talon_sim_phrase(phrase)
|
||||
^talon test numb <number_small>$:
|
||||
phrase = user.history_get(number_small)
|
||||
user.talon_sim_phrase(phrase)
|
||||
^talon test <phrase>$: user.talon_sim_phrase(phrase)
|
||||
^talon debug action {user.talon_actions}$:
|
||||
user.talon_action_find("{user.talon_actions}")
|
||||
^talon debug list {user.talon_lists}$: user.talon_debug_list(talon_lists)
|
||||
^talon copy list {user.talon_lists}$: user.talon_copy_list(talon_lists)
|
||||
^talon debug tags$: user.talon_debug_tags()
|
||||
^talon debug modes$: user.talon_debug_modes()
|
||||
^talon debug scope {user.talon_scopes}$: user.talon_debug_scope(talon_scopes)
|
||||
^talon debug setting {user.talon_settings}$: user.talon_debug_setting(talon_settings)
|
||||
^talon debug all settings$: user.talon_debug_all_settings()
|
||||
^talon debug active app$:
|
||||
result = user.talon_get_active_application_info()
|
||||
print("**** Dumping active application **** ")
|
||||
print(result)
|
||||
print("***********************")
|
||||
^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 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")
|
||||
Reference in New Issue
Block a user