Python script won't work on keyboard

So, I have a lot of scripts that I run with keyboard shortcuts like loading screenshots and imgur and placing links on the clipboard, stuff to digitize graphs, etc.

I have a current script that only works with the terminal, not when I try to run it as a keyboard shortcut.

I am trying to run it through System > Preferences > Keyboard Shortcuts

on Scientific linux 6.4

.

I have included the script below, in case there is something specific about this that 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

      

How does it work by selecting the clipboard selection with xclip

, using pygments

in text and then creating an html document and opening it in firefox and loading the image in imgur (using another script I have that I know 100% works), and then returning that image url to the clipboard.

The folder bin

it is in is in my path.

I've tried everything:

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

      

as command

in keyboard preferences

.

If I only change the content of these scripts to something like notify-send "hello"

and then generate a notification, so I'm pretty sure this is a menu script issue, not keyboard shortcuts

.

+3


source to share


3 answers


I faced the same problem. This is how I fixed it:

First of all, I wrote a one-line shell script that looked something like python /home/user/Scripts/script.py

, where "/home/user/Scripts/script.py" was my Python location. I have put this shell script in the executable path.



Then 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 a script as an argument for that terminal. In my case, it looks like this: xfce4-terminal -x ralia.sh

.

This works for me.

+2


source


Possible problem: $ PATH is different between your interactive shell and the daemon or program environment that handles keyboard shortcuts.

Try right after "import os":

open("/tmp/debug.txt", "w").write(os.environ["PATH"])

      



Then launch it with a keyboard shortcut and see /tmp/debug.txt

.

-> Try absolute paths for binaries and if that doesn't help, consider jhutar's advice.

0


source


The problem is "with open (" syntax.html "," w ") as f:". When using keyboard shortcuts for script, use the full path to the files in the script.

Instead:

with open("syntax.html", "w") as f:

      

Using:

with open("/home/user/my_script_folder/syntax.html", "w") as f:

      

Change all filenames in the script to full path and it should work.

0


source







All Articles