所以我有很多通过键盘快捷键运行的脚本,比如将屏幕截图上传到 imgur 并将链接放入剪贴板,数字化绘图等。
我有这个当前脚本,它只从终端运行,而不是当我尝试将它作为键盘快捷键运行时。
我正在尝试通过 System > Preferences > Keyboard Shortcuts
在 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(使用我拥有的另一个脚本,我知道它 100% 有效),然后将该图像 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"
的内容,然后生成通知消息,那么我相当有信心这是脚本的一个问题,并且不是键盘快捷键
菜单。
最佳答案
我遇到了完全相同的问题。这是我修复它的方法:
首先,我写了一个单行 shell 脚本,看起来像 python/home/user/Scripts/script.py
where "/home/user/Scripts/script.py"是我的 Python 的位置。我将这个 shell 脚本放在可执行路径中。
接下来,当我去创建快捷方式时,我没有告诉计算机运行该文件。我告诉计算机启动一个终端,并将 shell 脚本作为参数提供给该终端。在我的例子中,它看起来像:xfce4-terminal -x ralia.sh
。
这对我有用。
关于Python 脚本不会在键盘快捷键上运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26734591/