Search code examples
pythonbasharchlinuxi3

Is there a way to directly tell my python program to use the mouse with os.system("import filename.png")?


I've been trying to write a little screenshot script for arch. It works, but the problem is when I try to assign it a keybinding in my i3-config it does nothing.

First I tried writing it fully in bash which worked fine but I stumbled onto the same problem with it not executing. So I have redirected the output to a log-file to check it out and it welcomes me with this:

import: unable to grab mouse '': Datei oder Verzeichnis nicht gefunden @ error/xwindow.c/XSelectWindow/9306.
import: unable to read X window image '': Erfolg @ error/xwindow.c/XImportImage/4942.
import: unable to read X window image '': Erfolg @ error/xwindow.c/XImportImage/5049.
import:  `/home/lukas/Screenshot/20190419/scoot7.png' @ error/import.c/ImportImageCommand/1288.

Translation first line: Couldn't find file or directory @ error[...] Translation Erfolg: success

I've tried googling it, which didn't lead me to anything really and I didn't really find any path resembling error/xwindow.c and so on.

Here is my code:

#!/usr/bin/env python
import os, os.path
import datetime
import sys

d = datetime.datetime.today()
directory="/home/lukas/Screenshot/%s"%d.strftime('%Y%m%d')

if not os.path.exists(directory):
    os.mkdir(directory)

fileCount = 1
for file in os.listdir(directory):
    if file.endswith('.png'):
        fileCount+=1

filename = "%s/scr%d.png"%(directory,fileCount)
os.system("import %s"%filename)

and here is my entry to my i3 config:

bindsym $mod+Shift+F12          exec --no-startup-id scoot > /tmp/log.out 2>&1

The expected outcome of this is that when I press mod+shift+f12 it should transform my mouse-pointer to a "crosshair" so I can select something and takes a screenshot of that. The actual result is that if I run it normaly it works, but if I try to use the Keyboard-Shortcut it just does nothing but output to my logfile.

I am pretty much a newbie to programming and linux, and I have no idea why it cannot find my mouse and I wanted to know if I can explicitly tell the program to use it or if there is another way to do this.

Thanks alot.

Hugenotte


Solution

  • I wrote a bash script a little while ago to do exactly what you are trying to do, using the very common utility ImageMagick that you can find here on Arch.

    Here it is in case you are open to this alternative:

    #!/bin/bash
    
    # take screenshot using import from imagemagick
    # allows to select the area by dragging across a rectangle
    # or to select entire window by clicking inside it
    
    set -e
    
    # the date and time will be used as the file name
    time=$(date +"%F_%H:%M:%S")
    
    # naming the urxvt window "screenshot" so that the i3 "no_focus" option gets applied to it
    urxvt -title "screenshot" -e bash -c "import $HOME/Screenshot/$time.png"
    

    I then use it in i3 with:

    no_focus [title="screenshot"]
    bindsym $mod+Shift+F12 layout tabbed; exec --no-startup-id bash my_script.sh
    

    Of course, you may want to change the date format to what you had in python. I like having the time in case I take several screenshots in a row. And you have to replace urxvt with the name of your terminal emulator and you might have to adapt the -title flag if your terminal emulator doesn't give windows a name in this way.

    Note that naming the window is important: it took me a little while to figure out how to go around what happens without naming the window and using the no_focus on that window in i3:

    The focus would jump to the screen capture window and thus out of the window of which I wanted to take a screenshot. It might be possible that the issue you are facing with your bash and python scripts may be related to this. You have to force i3 to keep your mouse on the old window and not jump to the screen capture window triggered by the script (by default, i3 will focus on a newly created window).