Skip to content

Instantly share code, notes, and snippets.

@nillsondg
Last active January 29, 2024 07:16
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nillsondg/f322c3ec821d325450e47266f44a3f89 to your computer and use it in GitHub Desktop.
Save nillsondg/f322c3ec821d325450e47266f44a3f89 to your computer and use it in GitHub Desktop.
Simple addon to quickly lookup words in the OSX dictionary
# coding: utf-8
"""
Simple addon to quickly lookup words in the OSX dictionary
Author: Dmitry Gordeev <nillsondg@gmail.com>
Heavily based of work by
Eddie Blundell <eblundell@gmail.com>
https://gist.github.com/eddie/ff3d820fb267ae26ca0e
Artiom Basenko <demi.log@gmail.com>
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)
@kamitchell
Copy link

If you add...

addHook("EditorWebView.contextMenuEvent", osx_dict.lookup_osx_action)

...at the end here, you can also look up words while editing cards. I look words up sometimes to double-check their meaning, or to verify that I read them out of the book correctly.

image

@nillsondg
Copy link
Author

nillsondg commented Oct 18, 2023

If you add...

addHook("EditorWebView.contextMenuEvent", osx_dict.lookup_osx_action)

...at the end here, you can also look up words while editing cards. I look words up sometimes to double-check their meaning, or to verify that I read them out of the book correctly.

image

Hello. Thank you for comment. I have updated the plugin with this feature. Check it out.

@kamitchell
Copy link

Thanks! Works great!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment