talon_helpers.py (2155B)
1 from talon import Context, actions, ui, Module, app, clip 2 import os 3 import re 4 from itertools import islice 5 6 7 mod = Module() 8 pattern = re.compile(r"[A-Z][a-z]*|[a-z]+|\d") 9 10 # todo: should this be an action that lives elsewhere?? 11 def create_name(text, max_len=20): 12 return "_".join(list(islice(pattern.findall(text), max_len))).lower() 13 14 15 @mod.action_class 16 class Actions: 17 def talon_add_context_clipboard_python(): 18 """Adds os-specific context info to the clipboard for the focused app for .py files. Assumes you've a Module named mod declared.""" 19 friendly_name = actions.app.name() 20 # print(actions.app.executable()) 21 executable = actions.app.executable().split(os.path.sep)[-1] 22 app_name = create_name(friendly_name.replace(".exe", "")) 23 if app.platform == "mac": 24 result = 'mod.apps.{} = """\nos: {}\nand app.bundle: {}\n"""'.format( 25 app_name, app.platform, actions.app.bundle() 26 ) 27 elif app.platform == "windows": 28 result = 'mod.apps.{} = """\nos: windows\nand app.name: {}\nos: windows\nand app.exe: {}\n"""'.format( 29 app_name, friendly_name, executable 30 ) 31 else: 32 result = 'mod.apps.{} = """\nos: {}\nand app.name: {}\n"""'.format( 33 app_name, app.platform, friendly_name 34 ) 35 36 clip.set_text(result) 37 38 def talon_add_context_clipboard(): 39 """Adds os-specific context info to the clipboard for the focused app for .talon files""" 40 friendly_name = actions.app.name() 41 # print(actions.app.executable()) 42 executable = actions.app.executable().split(os.path.sep)[-1] 43 if app.platform == "mac": 44 result = "os: {}\nand app.bundle: {}\n".format( 45 app.platform, actions.app.bundle() 46 ) 47 elif app.platform == "windows": 48 result = "os: windows\nand app.name: {}\nos: windows\nand app.exe: {}\n".format( 49 friendly_name, executable 50 ) 51 else: 52 result = "os: {}\nand app.name: {}\n".format(app.platform, friendly_name) 53 54 clip.set_text(result) 55