Python脚本无法在键盘快捷键上运行 [英] Python script won't run on keyboard shortcut

查看:84
本文介绍了Python脚本无法在键盘快捷键上运行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我有很多脚本,它们是通过键盘快捷键运行的,例如将屏幕截图上传到imgur并将链接放置在剪贴板中,用于将图形数字化的东西等.

So i have plenty of scripts which i run from keyboard shortcuts, things like uploading screenshots to imgur and putting links in the clipboard, stuff for digitising plots, etc.

我有此当前脚本,该脚本仅从终端运行,而当我尝试将其作为键盘快捷键运行时则没有.

I have this current script, which only runs from the terminal, and not when i try to run it as a keyboard shortcut.

我正在尝试通过Scientific linux 6.4上的System > Preferences > Keyboard Shortcuts运行它.

I'm trying to run it via the System > Preferences > Keyboard Shortcuts on Scientific linux 6.4.

我已经在下面添加了脚本,以防万一有任何特定的细节使脚本无法正常工作.

I've included the script below, in case there's anything specific about it which would stop it from working.

#!/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%正常工作),然后将该图像网址放回剪贴板.

The way it works, is by extracting the clipboard selection using xclip, using pygments on the text, and then both creating an html document and opening it in firefox, and uploading an image to imgur (using another script i have, which i know 100% works), and then putting that image url back into the clipboard.

它所在的bin文件夹在我的路径中.

The bin folder it resides in is in my path.

我已经尝试了所有:

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

作为keyboard preferences中的command.

如果我将这些脚本的内容更改为类似notify-send "hello"的内容,然后生成通知消息,那么我相当有信心,这是该脚本的探针,而不是keyboard shortcuts菜单.

If i change the contents of these scripts to just something like notify-send "hello", and that then produces the notification message, so i'm fairlyconfident it's a probelm with the script, and not the keyboard shortcuts menu.

推荐答案

我遇到了完全相同的问题.这是我的解决方法:

I ran into exactly the same issue. Here's how I fixed it:

首先,我编写了一个单行的shell脚本,其外观类似于python /home/user/Scripts/script.py,其中"/home/user/Scripts/script.py"是我的Python的位置.我将此外壳程序脚本放置在可执行路径中.

First of all, I wrote a one-liner shell script that looked something like python /home/user/Scripts/script.py where "/home/user/Scripts/script.py" was the location of my Python. I placed this shell script in the executable path.

接下来,当我创建快捷方式时,我没有告诉计算机运行文件.我告诉计算机启动一个终端,并将shell脚本作为该终端的参数.就我而言,它类似于:xfce4-terminal -x ralia.sh.

Next, when I went to make my shortcut, I didn't tell the computer to run the file. I told the computer to start a terminal and gave the shell script as an argument to that terminal. In my case, it looked something like:xfce4-terminal -x ralia.sh.

这对我有用.

这篇关于Python脚本无法在键盘快捷键上运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆