這一篇接着上篇:編寫 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 的描述,第二個參數是複製到粘貼板中去的文本內容。