Submitted by raddleboy in programming (edited )

I'd like to simulate a bunch of key-presses on Tails OS but i don't know anything other than pyautogui (which i couldn't get to work on Tails). Is there a way i could convert my scripts into an appimage?

A simplified script (they are more lengthy otherwise) -

<div>
import pyautogui
import time
p.hotkey('alt', 'f2')
time.sleep(1)
p.write('gedit')
time.sleep(.5)
p.hotkey('enter')
</div>

Is there any other solution?

5

Comments

You must log in or register to comment.

nulloperation wrote

Is there a way i could convert my scripts into an appimage?

Here's an example recipe for packaging a Python app as an AppImage.

but i don't know anything other than pyautogui (which i couldn't get to work on Tails).

There's also xdotool, or for something more advanced (using image recognition to detect specific GUI elements) try SikuliX.

3

raddleboy OP wrote

xdotool is a godsend. You are a god send.

Just one thing.... How do i make a failsafe in xdotool like it is in pyautogui where you move the mouse to a screen corner and the script stops.

Is there a way to do that in xdotool?

3

nulloperation wrote

xdotool is a godsend.

Nice. Glad you found it useful. With xdotool you can do xdotool getmouselocation to get the location of the mouse. Then, before you move the mouse, you just need to check that the x and y values correspond to a corner.

How do i make a failsafe in xdotool like it is in pyautogui where you move the mouse to a screen corner and the script stops.

I'm not sure if you're calling xdotool from Python or just writing shell script, but if you have trouble programming this if_mouse_is_in_corner_then_exit function, just ask.

3

raddleboy OP wrote

<div>
#!/bin/bash
xdotool if_mouse_is_in_corner_then_exit
sleep 1
xdotool key super
sleep 1
xdotool type "text editor"
sleep 1
xdotool key Return
sleep 1
xdotool type "oire oiqerjh ;ouqrjbj erqvkhb nvurv hk erv kbrvbk qrvkh k qero;nq eruobqv kjqbviybqek vihbv sihfbv afvkhlaek vkhebvh khbk iubefvibo afkbajfipufv[  ounqnvfq v knv knqef viu;nqlv oqv qoubvekjvbkd vn zdfkj kjfvbfnkbffdn nv jdfjnfgjbnfjbfjskmcokpslcpljc jdnvnm ckenci,d kn"
</div>

This is the sample script. As you can see, i didn't quite understand how to apply the "if_mouse......._exit"

How do i make it stop by either moving my mouse into a corner or any other way.

3

nulloperation wrote (edited )

As you can see, i didn't quite understand how to apply the "if_mouse......._exit"

Well, that function doesn't exist yet, so the idea would be for you to write it.

How do i make it stop by either moving my mouse into a corner or any other way.

I'll give it a try. First explanation, then some code.

To tell whether the mouse is near the corner, we'll define a function mouse_near_corner. First we need to use xdotool getmouselocation to get the location of the mouse, and xdotool getdisplaygeometry to get the display geometry. (Unfortunately this output of getmouselocation is a bit verbose and inconsistent with the output getdisplaygeometry, so we have to massage the data a bit using "lookbehind" in grep, which matches "regular expressions", which may look a bit frightening.) Then we need to compare the mouse position to the display geometry to tell whether the mouse is near one of the four corners (if we're assuming there's just one screen). For convenience, we can define a function near which tells whether two numbers are closer than a given threshold (which I've set to 50 here).

(Another tricky bit is that return uses 0 for success and 1 for failure, while expr uses 1 for true and 0 for false, so the comparison has to be inverted to account for that. Yea, shell scripts are kinda messed up.)

Then we need to define a function that'll exit if the mouse is near the corner. We'll call that function check unless you have a better idea for a name. That function exits the program if mouse_near_corner (which we just defined) is true.

Now we can define our main method where we do the GUI automation with xdotool (for now, it'll just type "hello" repeatedly). We just have to make sure we call check before calling xdotool inside the main function, so that the program will halt if the mouse is in the corner.

near(){
	threshold=50
	diff=$(expr ${1#-} - ${2#-})
	return $(expr ${diff#-} \> $threshold )
}

mouse_near_corner() {
	display_geometry=($(xdotool getdisplaygeometry))
	mouse_location=($(xdotool getmouselocation|grep -P -o "(?<=[x,y]:)\d+"|tr "\n" " "))
	if near ${mouse_location[0]} ${display_geometry[0]} || near ${mouse_location[0]} 0; then
		if near ${mouse_location[1]} ${display_geometry[1]} || near ${mouse_location[1]} 0; then
			return 0
		fi
	fi
	return 1
}

check(){ mouse_near_corner && exit; }

main(){ check; xdotool type hello; sleep 1; main; }

main

That may be daunting, but that's how I'd do that in Bash.

3