Created
May 5, 2019 12:09
-
-
Save Versatilus/1741a780d596c1862a8fd13d28b5df01 to your computer and use it in GitHub Desktop.
Personal speech recognition (Dragonfly) command set for Google Chrome
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!python | |
# -*- coding: utf-8 -*- | |
""" | |
This is relevant as of 20190505. I have no plans of updating this gist. Some of | |
this is written in a style I wouldn't necessarily use now, but I'll let you | |
guess what that is. | |
I'm not going to bet that any of this works. I haven't updated it in months, if | |
not years. I know that some of the Facebook commands don't work. I use most of | |
the commands for Gmail regularly, but I haven't used the commands for | |
Google Sheets since I made the rule. | |
The contexts with "-~-" in them assume the use of a browser extension like | |
https://chrome.google.com/webstore/detail/url-in-title/ignpacbgnbnkaiooknalneoeladjnfgb | |
to place the URL in the title bar. I configure the extension with the string | |
"{title} -~- {hostname}{port}/{path} -~- {hash}". Be aware that WSR has a bug | |
causing it to become unresponsive if the current window title is too long. I | |
don't remember how many characters "too long" is, so if you use WSR… | |
good luck. :-) | |
""" | |
from dragonfly import (AppContext, Choice, Function, Grammar, IntegerRef, Key, | |
MappingRule, Mouse) | |
chrome_context = AppContext(executable="chrome.exe") | |
gitter_context = AppContext(executable="chrome.exe", title="- gitter") | |
google_mail_context = AppContext(executable="chrome.exe", title=" - gmail") | |
google_sheets_context = AppContext( | |
executable="chrome.exe", title="-~- docs.google.com/spreadsheets/") | |
toy_blast_context = AppContext( | |
executable="chrome.exe", title="toy blast on facebook") | |
facebook_context = AppContext( | |
executable="chrome.exe", title="-~- www.facebook.com") | |
facebook_messages_context = AppContext( | |
executable="chrome.exe", title="-~- www.facebook.com/messages/") | |
vimium_context = ( | |
chrome_context & ~google_mail_context & ~google_sheets_context & | |
~facebook_messages_context & ~toy_blast_context) | |
grammar = Grammar("chrome_commands") | |
def toy_blast_processor(direction, steps, poke): | |
pixels_to_move = steps * 65 | |
dy = 0 | |
dx = 0 | |
if "north" in direction: | |
dy = pixels_to_move * -1 | |
if "west" in direction: | |
dx = pixels_to_move * -1 | |
if "east" in direction: | |
dx = pixels_to_move | |
if "south" in direction: | |
dy = pixels_to_move | |
Mouse("<%(dx)d, %(dy)d>, %(poke)s" % { | |
"dx": dx, | |
"dy": dy, | |
"poke": poke | |
}).execute() | |
class ToyBlastRule(MappingRule): | |
mapping = { | |
"<direction> [<steps>] [<poke>]": Function(toy_blast_processor), | |
} | |
extras = [ | |
Choice( | |
"direction", { | |
"North": "north", | |
"North East": "northeast", | |
"East": "east", | |
"South East": "southeast", | |
"South": "south", | |
"South West": "southwest", | |
"West": "west", | |
"North West": "northwest" | |
}), | |
IntegerRef("steps", 1, 9), | |
Choice("poke", { | |
"poke": "left", | |
"no poke": "left:up" | |
}) | |
] | |
defaults = {"steps": 1, "poke": "left:up"} | |
class GmailRule(MappingRule): | |
mapping = { | |
"keyboard shortcuts": Key("question"), | |
"select item": Key("x"), | |
"select all mail": Key("asterisk,a"), | |
"select none": Key("asterisk,n"), | |
"select read": Key("asterisk,r"), | |
"select unread": Key("asterisk,u"), | |
"mark as read": Key("I"), | |
"mark as unread": Key("U"), | |
"mark as important": Key("plus"), | |
"mark as not important": Key("minus"), | |
"delete selected": Key("hash"), | |
"move to": Key("v"), | |
"newer [conversation]": Key("k"), | |
"older [conversation]": Key("j"), | |
"newer message": Key("n"), | |
"older message": Key("p"), | |
"open [mail]": Key("o"), | |
"reply": Key("r"), | |
"(compose | new) message": Key("c"), | |
"send message": Key("c-enter"), | |
"refresh conversation": Key("s-n"), | |
"go to inbox": Key("g, i"), | |
"go to contacts": Key("g, c"), | |
"go to all mail": Key("g, a"), | |
"go to drafts": Key("g, d"), | |
} | |
class VimiumRule(MappingRule): | |
mapping = { | |
"(open | show) links": Key("f"), | |
"open link in a new tab": Key("F"), | |
"half down": Key("d"), | |
"half up": Key("u"), | |
"next page": Key("rbracket,rbracket"), | |
"previous page": Key("lbracket,lbracket"), | |
"keyboard shortcuts": Key("question"), | |
"copy link": Key("y, f"), | |
"copy current link": Key("y, y"), | |
} | |
class FacebookRule(MappingRule): | |
mapping = { | |
"Newsfeed": Key("a-1"), | |
"My Timeline": Key("a-2"), | |
"Friend Requests": Key("a-3"), | |
"Messages": Key("a-4"), | |
"Notifications": Key("a-5"), | |
} | |
class GoogleSheetsRule(MappingRule): | |
mapping = { | |
"keyboard shortcuts": Key("c-slash"), | |
"select row": Key("s-space"), | |
"select all cells": Key("cs-space"), | |
"select column": Key("c-space"), | |
"find": Key("c-f"), | |
"fill range": Key("c-enter"), | |
"find and replace": Key("c-h"), | |
} | |
grammar.add_rule(FacebookRule(context=facebook_context)) | |
grammar.add_rule(GmailRule(context=google_mail_context)) | |
grammar.add_rule(VimiumRule(context=vimium_context)) | |
grammar.add_rule(GoogleSheetsRule(context=google_sheets_context)) | |
grammar.add_rule(ToyBlastRule(context=toy_blast_context)) | |
grammar.load() | |
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