这一篇接着上篇:编写 Albert 翻译插件之功能升级 实现选中翻译结果拷贝到粘贴板!
实现选中(Enter)复制到粘贴板
代码已经更新到 github 上:
只需要使用 ClipAction
即可:
---
youdao-fanyi/youdao_translate.py | 14 ++++++++------
1 file changed, 8 insertions(+), 6 deletions(-)
diff --git a/youdao-fanyi/youdao_translate.py b/youdao-fanyi/youdao_translate.py
index 4a76214..953f020 100755
--- a/youdao-fanyi/youdao_translate.py
+++ b/youdao-fanyi/youdao_translate.py
@@ -15,7 +15,7 @@ import requests
__iid__ = "PythonInterface/v0.1"
__prettyname__ = "Translation Word"
__trigger__ = "tr "
-__version__ = "2.2"
+__version__ = "2.3"
__author__ = "Joseph Lin"
__dependencies__ = ["requests", "youdao account"]
@@ -121,7 +121,7 @@ def generate_display_items(data):
text=str(data['basic']['phonetic']),
subtext="phonetic",
actions=[]))
- except:
+ except Exception:
pass
try:
@@ -129,8 +129,9 @@ def generate_display_items(data):
results.append(
Item(id="", icon=ICON_PATH,
completion="", urgency=ItemBase.Notification,
- text=str(explain), subtext='explain', actions=[]))
- except:
+ text=str(explain), subtext='explain',
+ actions=[ClipAction("copy", str(explain)), ]))
+ except Exception:
pass
try:
@@ -139,8 +140,9 @@ def generate_display_items(data):
Item(id="", icon=ICON_PATH,
completion="", urgency=ItemBase.Notification,
text="{}: {}".format(web_expl['key'], web_expl['value']),
- subtext='web explain', actions=[]))
- except:
+ subtext='web explain',
+ actions=[ClipAction("copy", str(web_expl['value'])), ]))
+ except Exception:
pass
return results
--
ClipAction
ClipAction
的使用:
ClipAction("<describe>", "<string to be copyed>")
第一个参数是这个 Action 的描述,第二个参数是复制到粘贴板中去的文本内容。