我有许多脚本都是通过键盘快捷方式运行的,例如将截图上传到imgur并在剪贴板中放置链接,用于数字化绘图等。
我有这个当前脚本,它只能从终端运行,而且无法通过键盘快捷方式运行。
我正在尝试通过Scientific Linux 6.4
上的系统>首选项>键盘快捷键
来运行它。
如下为脚本内容,以便查看是否有任何特定问题导致其无法工作。
#!/usr/bin/python
import fileinput, os
import subprocess
from pygments import highlight
from pygments.lexers import get_lexer_by_name, guess_lexer
import pygments.formatters as formatters
#stdin = "\n".join([line for line in fileinput.input()])
p = subprocess.Popen(["xclip", "-selection", "primary", "-o"], stdout=subprocess.PIPE)
code, err = p.communicate()
if not err:
lexer = guess_lexer(code)
print lexer.name
imageformatter = formatters.ImageFormatter(linenos=True, cssclass="source", font_name="Liberation Mono")
formatter = formatters.HtmlFormatter(linenos=True, cssclass="source")
HTMLresult = highlight(code, lexer, formatter)
Jpgresult = highlight(code, lexer, imageformatter, outfile=open("syntax.png", "w"))
with open("syntax.html", "w") as f:
f.write("<html><head><style media='screen' type='text/css'>")
f.write(formatters.HtmlFormatter().get_style_defs('.source'))
f.write("</style></head><body>")
f.write(HTMLresult)
f.write("</body></html>")
# os.system("pdflatex syntax.tex")
os.system("firefox syntax.html")
os.system("uploadImage.sh syntax.png")
else:
print err
它的工作方式是使用xclip
提取剪贴板选择的内容,对文本使用pygments
,然后创建一个HTML文档并在Firefox中打开它,并将图像上传到imgur(使用我拥有的另一个脚本,我知道它百分之百有效),然后将该图像URL放回剪贴板。
它所在的bin
文件夹位于我的路径中。
我尝试了所有以下方法:
script
script.sh (where this file is just a shell script which calls the python script)
/home/will/bin/script
/home/will/bin/script.sh
在 键盘首选项
中的 命令
栏中更改内容。
如果我将这些脚本的内容更改为类似于 notify-send "hello"
的内容,那么它会产生通知消息,因此我相当自信这是一个与脚本有关而不是 键盘快捷键
菜单的问题。
notify-send "hello"
来隔离导致问题的确切行。然后你就可以继续了。也许你的脚本的某些部分需要一些环境,而当通过键盘快捷方式调用脚本时,你没有这些环境? - jhutarscript.sh
,可以尝试通过类似于/home/will/bin/problematic_script.py &>/tmp/script.log
的方式将stdout和stderr(以及其他所有内容)重定向到文件中。也许这会提供更多线索? - jhutar