Last active
December 24, 2020 21:43
-
-
Save ansaso/3f5e811576e84fa13d038579bdce4b58 to your computer and use it in GitHub Desktop.
alfred-quicklook python boilerplate
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from __future__ import print_function | |
from subprocess import Popen, PIPE | |
import json | |
import os | |
def extra_pane(json, bin_path=os.getenv('BIN_PATH')): | |
p = Popen((bin_path), stdin=PIPE, stdout=PIPE, stderr=PIPE) | |
out, err = p.communicate(json) | |
if err: | |
raise OSError(err) | |
return out | |
def fileify(items, html_dir=os.path.expanduser(os.getenv('HTML_DIR'))): | |
for n, item in enumerate(items): | |
html = item.get('quicklookurl') | |
if html: | |
path = os.path.join(html_dir, str(n) + '.html') | |
try: | |
with open(path, 'w+') as file: | |
file.write(html) | |
except IOError: | |
raise IOError(html_dir + ' directory does not exist') | |
item['quicklookurl'] = path | |
return items | |
def alfred(items, variables={}): | |
fileify(items) | |
dic = dict(items=items, **variables) | |
json_ = json.dumps(dic) | |
out = extra_pane(json_) | |
print(out) | |
return 0 | |
alfred([{'test':'test', 'quicklookurl':'<html><body><h1>test</h1></body></html>'}]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment