Skip to content

Instantly share code, notes, and snippets.

@comodoro
Last active October 10, 2018 16:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save comodoro/922717a4eedd4a5691ae42b41e3d349f to your computer and use it in GitHub Desktop.
Save comodoro/922717a4eedd4a5691ae42b41e3d349f to your computer and use it in GitHub Desktop.
Bring me for Caster
{
"Caster": "https://caster.readthedocs.io/en/latest/",
"Dragonfly": "https://dragonfly2.readthedocs.io/en/latest/",
"Google": "https://www.google.com",
"libraries": "%USERPROFILE%",
"my documents": "%USERPROFILE%\\Documents",
"my pictures": "%USERPROFILE%\\Pictures",
"notepad": "notepad"
}
import os
import webbrowser
import threading
from dragonfly import Grammar, Choice, Function, Dictation
from caster.lib import control, context, utilities, settings
from caster.lib.dfplus.merge.selfmodrule import SelfModifyingRule
from caster.lib.dfplus.state.short import R
config = utilities.load_json_file(settings.SETTINGS["paths"]["BRINGME_PATH"])
def reload():
bring_rule.reset(_rebuild_mapping())
#module functions
def bring_it(item):
'''
Currently simply invoke os.startfile. New thread keeps Dragon from crashing.
'''
threading.Thread(target=os.startfile, args=(item,)).start()
def bring_add(launch, key):
'''
Add current program or highlighted text to bring me
'''
if launch=="program":
path = utilities.get_active_window_path()
else:
path = context.read_selected_without_altering_clipboard()
config[str(key)] = path
utilities.save_json_file(config, settings.SETTINGS["paths"]["BRINGME_PATH"])
reload()
def bring_remove(key):
'''
Remove item from bring me
'''
del config[str(key)]
utilities.save_json_file(config, settings.SETTINGS["paths"]["BRINGME_PATH"])
reload()
def _rebuild_mapping():
return {key: os.path.expandvars(value) for key, value in config.iteritems()}
class BringRule(SelfModifyingRule):
pronunciation = "bring me"
mapping = {
"bring me <item>":
R(Function(bring_it), rdescript="Launch preconfigured program, folder or website"),
"<launch> to bring me as <key>":
R(Function(bring_add), rdescript="Launch preconfigured program, folder or website"),
"remove <item> from bring me":
R(Function(bring_remove), rdescript="Launch preconfigured program, folder or website"),
}
extras = [
Choice("item", _rebuild_mapping()),
Choice("launch", {
"[current] program": "program",
"website": "website",
"folder": "folder",
}),
Dictation("key"),
]
bring_rule = BringRule()
# bring_rule.set_merger(control.nexus().merger)
control.nexus().merger.add_selfmodrule(bring_rule)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment