From 54d2f46d1bb9f08e03aed0507b728b8093494a0b Mon Sep 17 00:00:00 2001 From: Scott Wallace Date: Sat, 12 Jan 2019 16:39:54 +0000 Subject: [PATCH] Added a 'pasteboard' Python module for Albert --- .../modules/pasteboard.py | 96 +++++++++++++++++++ 1 file changed, 96 insertions(+) create mode 100644 org.albert.extension.python/modules/pasteboard.py diff --git a/org.albert.extension.python/modules/pasteboard.py b/org.albert.extension.python/modules/pasteboard.py new file mode 100644 index 0000000..e6d932c --- /dev/null +++ b/org.albert.extension.python/modules/pasteboard.py @@ -0,0 +1,96 @@ +# -*- coding: utf-8 -*- + +"""PasteBoard clipboard management. + +Slight modification of CopyQ by Manuel Schneider""" + +import html +import json +import re +import subprocess +from shutil import which + +from albertv0 import * + +__iid__ = "PythonInterface/v0.1" +__prettyname__ = "PasteBoard" +__version__ = "1.0" +__trigger__ = "pb " +__author__ = "Scott Wallace" +__dependencies__ = ["copyq"] + + +if which("copyq") is None: + raise Exception("'copyq' is not in $PATH.") + +iconPath = iconLookup('copyq') + +copyq_script_getAll = r""" +var result=[]; +for ( var i = 0; i < size(); ++i ) { + var obj = {}; + obj.row = i; + obj.mimetypes = str(read("?", i)).split("\n"); + obj.mimetypes.pop(); + obj.text = str(read(i)); + result.push(obj); +} +JSON.stringify(result); +""" + +copyq_script_getMatches = r""" +var result=[]; +var match = "%s"; +for ( var i = 0; i < size(); ++i ) { + if (str(read(i)).search(new RegExp(match, "i")) !== -1) { + var obj = {}; + obj.row = i; + obj.mimetypes = str(read("?", i)).split("\n"); + obj.mimetypes.pop(); + obj.text = str(read(i)); + result.push(obj); + } +} +JSON.stringify(result); +""" + +def copyq_get_matches(substring): + script = copyq_script_getMatches % substring + proc = subprocess.run(['copyq', '-'], input=script.encode(), stdout=subprocess.PIPE) + return json.loads(proc.stdout.decode()) + + +def copyq_get_all(): + proc = subprocess.run(['copyq', '-'], input=copyq_script_getAll.encode(), stdout=subprocess.PIPE) + return json.loads(proc.stdout.decode()) + + +def handleQuery(query): + if query.isTriggered: + + items = [] + pattern = re.compile(query.string, re.IGNORECASE) + json_arr = copyq_get_matches(query.string) if query.string else copyq_get_all() + for json_obj in json_arr: + row = json_obj['row'] + text = json_obj['text'] + if not text: + text = "No text" + else: + text = html.escape(" ".join(filter(None, text.replace("\n", " ").split(" ")))) + if query.string: + text = pattern.sub(lambda m: "%s" % m.group(0), text) + items.append( + Item( + id=__prettyname__, + icon=iconPath, + text=text, + subtext="%s: %s" % (row, ", ".join(json_obj['mimetypes'])), + actions=[ + ProcAction("Copy", ["copyq", "select(%s);" % row]), + ProcAction("Put", ["copyq", "select(%s); sleep(60); paste();" % row]), + ProcAction("Remove", ["copyq", "remove(%s);" % row]), + ] + ) + ) + return items