Python 脚本不会在键盘快捷键上运行

标签 python linux bash keyboard-shortcuts redhat

所以我有很多通过键盘快捷键运行的脚本,比如将屏幕截图上传到 imgur 并将链接放入剪贴板,数字化绘图等。

我有这个当前脚本,它只从终端运行,而不是当我尝试将它作为键盘快捷键运行时。

我正在尝试通过 System > Preferences > Keyboard ShortcutsScientific 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/

相关文章:

python - 如何在 numpy 数组中查找唯一对象?

linux - 用于计算目录大小并在超过使用阈值时删除其中最旧日志的脚本?

linux - 如何在 bash 中评论参数列表

python - 如何使用 Numpy.concatenate 连接空数组?

c# - 如何从 Azure Function App 中的请求正文中检索字节数据

linux - 通过 ssh 执行脚本时无法使用命令 `date` 或 `find`

linux - 高性能文件服务的设计选择

php - Mysql连接VLAN外的外部/远程服务器

bash - 如何在 shell 中找到值 2 指数?

python - 在 Python 3.2 中调用 Python 2.7 模块