-
Notifications
You must be signed in to change notification settings - Fork 1
/
OSX_Dictionary_Lookup.py
50 lines (37 loc) · 1.18 KB
/
OSX_Dictionary_Lookup.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# coding: utf-8
"""
Simple addon to quickly lookup words in the OSX dictionary
Author: Eddie Blundell <[email protected]>
Heavily based of work by Artiom Basenko <[email protected]>
https://gist.github.com/Xifax/f36002ddf910993d6bfb
License: The MIT License (MIT)
"""
# Stdlib
# Anki
from aqt.qt import *
from aqt.webview import AnkiWebView
from anki.hooks import addHook
# Qt
from PyQt4.QtGui import *
from PyQt4.QtCore import *
class OSXDictionary:
"""OSX Dictionary launcher
"""
OSX_CMD = 'open 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.get_selected(view))
def add_action(self, view, menu, action):
"""Add 'lookup' action to context menu"""
action = menu.addAction(action)
action.connect(action, SIGNAL('triggered()'),
lambda view=view: 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)