Last active
January 29, 2024 07:16
-
-
Save nillsondg/f322c3ec821d325450e47266f44a3f89 to your computer and use it in GitHub Desktop.
Simple addon to quickly lookup words in the OSX dictionary
This file contains 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
# coding: utf-8 | |
""" | |
Simple addon to quickly lookup words in the OSX dictionary | |
Author: Dmitry Gordeev <[email protected]> | |
Heavily based of work by | |
Eddie Blundell <[email protected]> | |
https://gist.github.com/eddie/ff3d820fb267ae26ca0e | |
Artiom Basenko <[email protected]> | |
https://gist.github.com/Xifax/f36002ddf910993d6bfb | |
License: The MIT License (MIT) | |
""" | |
# Anki | |
from aqt.qt import * | |
from aqt.webview import AnkiWebView | |
from anki.hooks import addHook | |
from urllib.parse import quote_plus | |
class OSXDictionary: | |
"""OSX Dictionary launcher""" | |
OSX_CMD = 'open' | |
OSX_ARGS = 'dict:///%s' | |
def get_selected(self, view): | |
"""Copy selected text""" | |
return view.page().selectedText() | |
def lookup_osx(self, view): | |
QProcess.startDetached(self.OSX_CMD, [self.OSX_ARGS % quote_plus(self.get_selected(view))]) | |
def add_action(self, view, menu, action): | |
"""Add 'lookup' action to context menu""" | |
action = menu.addAction(action) | |
action.triggered.connect(lambda: self.lookup_osx(view)) | |
def lookup_osx_action(self, view, menu): | |
"""Lookup OSX action""" | |
self.add_action(view, menu, | |
u'Lookup "%s"' % self.get_selected(view)[:10]) | |
# Add lookup actions to context menu | |
osx_dict = OSXDictionary() | |
addHook("AnkiWebView.contextMenuEvent", osx_dict.lookup_osx_action) | |
addHook("EditorWebView.contextMenuEvent", osx_dict.lookup_osx_action) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks! Works great!