Skip to content

Instantly share code, notes, and snippets.

@alexboche
Created March 6, 2019 09:28
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 alexboche/aebe19a8ed04d784a9e33b9ec59a44ac to your computer and use it in GitHub Desktop.
Save alexboche/aebe19a8ed04d784a9e33b9ec59a44ac to your computer and use it in GitHub Desktop.
#
# This file is part of Dragonfly.
# (c) Copyright 2007, 2008 by Christo Butcher
# Licensed under the LGPL.
#
# Dragonfly is free software: you can redistribute it and/or modify it
# under the terms of the GNU Lesser General Public License as published
# by the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Dragonfly is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with Dragonfly. If not, see
# <http://www.gnu.org/licenses/>.
#
"""
This module is a simple example of Dragonfly use.
It shows how to use Dragonfly's Grammar, AppContext, and MappingRule
classes. This module can be activated in the same way as other
Natlink macros by placing it in the natlink user directory.
"""
from dragonfly import (Grammar, AppContext, MappingRule, Dictation, Key,
Text, WaitWindow, Choice, IntegerRef, Function)
# --------------------------------------------------------------------------
# Create this module's grammar and the context under which it'll be active.
grammar_context = AppContext(executable="notepad")
grammar = Grammar("notepad_example", context=grammar_context)
# --------------------------------------------------------------------------
# Create a mapping rule which maps things you can say to actions.
#
# Note the relationship between the *mapping* and *extras* keyword
# arguments. The extras is a list of Dragonfly elements which are
# available to be used in the specs of the mapping. In this example
# the Dictation("text")* extra makes it possible to use "<text>"
# within a mapping spec and "%(text)s" within the associated action.
example_rule = MappingRule(
# The name of the rule.
name="example",
# The mapping dict: spec -> action.
mapping={
"save [file]": Key("c-s"),
"save [file] as": Key("a-f, a"),
"save [file] as <text>": Key("a-f, a/20") + Text("%(text)s"),
"find <text>": Key("c-f/20") + Text("%(text)s\n"),
},
# Special elements in the specs of the mapping.
extras=[
Dictation("text"),
],
)
# --------------------------------------------------------------------------
# Create another mapping rule that uses the dragonfly equivalent of Vocola
# functions: http://vocola.net/v2/DefiningFunctions.asp
#
# Define getFontPanel() using actions.
# getFontPanel() := {Alt+o}f WaitForWindow(font);
getFontPanel = Key("a-o, f") + WaitWindow("font")
# Define setFontProperty(field, value).
# setFontProperty(field, value) := {Alt+o}f WaitForWindow(font)
# {Alt+$field} $value {Enter};
def setFontProperty(field, value):
value = str(value) # Ensure the value passed to Text is a string.
action = getFontPanel + Key("a-" + field) + Text(value) + Key("enter")
action.execute()
# Define functions for setting three different font properties.
setSize = lambda size: setFontProperty("s", size)
setFont = lambda font: setFontProperty("f", font)
setStyle = lambda style: setFontProperty("y", style)
font_rule = MappingRule(
name="fonts",
mapping={
"Font Size <size>": Function(setSize),
"Font <font>": Function(setFont),
"Font Style <style>": Function(setStyle),
},
extras=[
# Define an IntegerRef for font sizes 6 to 73 (exclusive).
IntegerRef("size", 6, 73),
# Define choices for fonts and styles.
Choice("font", {"Arial": "Arial", "Courier": "Courier"}),
Choice("style", {"Regular": "Regular", "Bold": "Bold"}),
],
)
# Add the rules to the grammar instance.
grammar.add_rule(example_rule)
grammar.add_rule(font_rule)
# --------------------------------------------------------------------------
# Load the grammar instance and define how to unload it.
grammar.load()
# Unload function which will be called by natlink at unload time.
def unload():
global grammar
if grammar: grammar.unload()
grammar = None
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment