Simulate mouse movement in Ubuntu - command-line

Problem
Am looking to automatically move the mouse cursor and simulate mouse button clicks from the command-line using an external script. Am not looking to:
Record mouse movement and playback (e.g., xnee, xmacro)
Instantly move the mouse from one location to another (e.g., xdotool, Python's warp_pointer)
Ideal Solution
What I'd like to do is the following:
Edit a simple script file (e.g., mouse-script.txt).
Add a list of coordinates, movement speeds, delays, and button clicks. For example:
(x, y, rate) = (500, 500, 50)
sleep = 5
click = left
Run the script: xsim < mouse-script.txt.
Question
How do you automate mouse movement so that it transitions from its current location to another spot on the screen, at a specific velocity? For example:
xdotool mousemove 500 500 --rate 50
The --rate 50 doesn't exist with xdotool.

on newer versions of Ubuntu (14.04+), you can use Autopilot, a UI testing tool for Ubuntu. It is made for creating and running user interface tests, but can also be used for basic GUI automation tasks.
to install:
$ sudo apt-get install python3-autopilot
an example script (Python3) to automate mouse movement:
#!/usr/bin/env python3
from autopilot.input import Mouse
mouse = Mouse.create()
mouse.move(100, 50)
mouse.click()
You would run this just like any other Python3 script. Watch your mouse pointer move!

Download xaut for Python
Follow the README instructions
Run:
sudo apt-get install swig x11proto-xext-dev libx11-dev libxtst-dev
cd /usr/local/src
tar zxf xaut-0.2.0.tar.gz
./configure
Edit src/Makefile
Change the CFLAGS line as follows:
CFLAGS = -Wall -fPIC -fno-stack-protector
Run:
make
Copy /usr/local/src/xaut-0.2.0/python/build/lib/* to a new directory.
Change to that new directory.
Copy and paste the following script into mm.py:
import xaut
mouse = xaut.mouse()
delay mouse.move_delay( 100 )
mouse.move( 500, 500 )
Run the script:
python mm.py

Related

How to setup zsh personal config (.dotfiles/zsh/zshrc) at every start on Raspberry Pi 400

I am training myself on Linux with a Raspberry Pi 400 (OS: Raspbian) and I would like to setup my personal config file for apps (notably the Terminal in Zsh). I am able to setup my config onto the terminal with the following:
$ cd .dotfiles
$ ./export.sh
$ cd zsh/
$ source ./zshrc
The problem comes when I quit the terminal or if I open a new terminal or if I reboot the computer, the config is not "saved".
I am not an expert, but for changing bash to zsh and maintaining it as default shell language, I did the following:
$ nano /etc/passwd
and changed the following line from :
pi:x:1000:1000:,,,:/home/pi:/bin/bash
to:
pi:x:1000:1000:,,,:/home/pi:/bin/zsh
This move managed to get me zsh from start every time now. I am wondering if i can directly source my .zshrc with the same technique ... Or would you have any better idea?
I would like to avoid repeating the sourcing steps at every start in the terminal ...

How to translate an ADB command line control ---> to MonkeyRunner Python script

When I run this on the command line it brings up the screen on my device to change the language.
$ adb shell am start -a android.settings.LOCALE_SETTINGS
works great.
I am trying the same functionality into a Python script that monkeyrunner calls.
How do you translate the above into something that will work with monkeyrunner / python?
e.g. I tried:
device = Monkeyrunner.waitForConnection()
package = 'android.settings'
activity = 'LOCALE_SETTINGS'
runComponent = package + '/' activity
device.startActivity(component=runComponent
This does nothing. No error message. But nothing.
Any suggestions?
android.settings.LOCALE_SETTINGS is not a component but an action. You can do either
device.startActivity(action='android.settings.LOCALE_SETTINGS')
or
device.startActivity(component='com.android.settings/com.android.settings.Settings$LocalePickerActivity')

Display GUI on Raspberry Pi without startx

I'm currently playing with my brand-new raspberry with adafruit's touch-tft and raspberry Cam.
I noticed, if I run raspivid -p, it displays the preview in a frame on top of the bash.
How can I create an application (java, python, c++(preferred)) to display a GUI without having to startx?
I'd love to use adafruit's touch-tft for a project, but startx needs a lot of resources. Of course it would be cool to have the touch-functionality too.
Edit:
Maybe I expressed myself a little bit confusing: I would like to create an application that doesn't need startx but has some kind of GUI.
The answers below aren't 100% ready to go but they pointed me to the right direction. Since I saw many similar questions I will try to give a step-by-step solution:
Setup your raspberry with adafruit's TFT: https://learn.adafruit.com/adafruit-pitft-28-inch-resistive-touchscreen-display-raspberry-pi/overview
(you don't have to replace the TFT with HDMI as standart display)
be sure FRAMEBUFFER=/dev/fb1 startx is running correctly on the TFT screen, I have my TV plugged in the HDMI port, this way I can use the bash on the TV screen and run x on the TFT
Follow this tutorial to create your first pygame: http://www.pygame-doku.laymaxx.de/tut/intro/intro.html
(you can just copy&paste, wget ball.gif)
Insert this 2 lines at the beggining: (github.com/notro/fbtft/wiki/Pygame)
import os
os.environ["SDL_FBDEV"] = "/dev/fb1"
If you want to test the touch-screen to, you can change Line 15:
From:
if event.type == pygame.QUIT: sys.exit()
To:
if event.type == pygame.MOUSEBUTTONDOWN: sys.exit()
This will exit the application if you touch the screen
Save file e.g. pygame1.py and execute with $python pygame1.py
You should now see a bouncing ball on your TFT while the HDMI output still displays the BASH on your TV (but it isn't active of course) touching the screen will exit the game if you completed step 6
Why I'm doing this? This way you can display a simple GUI with e.g. buttons to do some action, like turn your lights on/off if you use your RPI for home-control.
You can use for example pygame library! check this tutorial http://archive.furtherfield.org/rp-resources/RP-workshop-handout.pdf pg. 17
Below, you will find a detailed description of how to be a minimal window manager to start a graphics program of your choice.
-Install Required Packages:
apt-get update
apt-get install xorg - no-install-recommends gdm3 matchbox-window-manager Iceweasel rsync-y
-The system in question, select Add a new user interface gdm3. The terminal is the default name will be used.
adduser kiosk
-Create-Xsession. This script will be executed when the user logs on.
xset s off
xset -dpms
matchbox-window-manager &
while true; do
rsync -qr --delete --exclude='.Xauthority’ /opt/kiosk/ $HOME/
firefox http://www.google.com (Replace your application!!!)
done
Save Ctrl + O; Exit: Ctrl + X
chmod a + x. xsession
cp / opt / kiosk / .xsession / home / kiosk
Set Auto-Login. Kiosk user will enter automatically after boot.
nano / etc/gdm3/daemon.conf
[Daemon]
AutomaticLoginEnable = true
AutomaticLogin = kiosk
We have chosen to configure, program, if necessary
-Screen calibration when not loaded up the whole screen:
/ Boot / config.txt file, clear the comment:
disable_overscan = 1
I hope I helped
Check out Urwid, a text-based ui that can run in a terminal, or, I assume, on a Raspberry Pi started up without startx.
In short you can't, applications like gedit are built atop of a desktop environment (e.g. Gnome, Kde).

OpenSuse - Command for Beep Sound (System Bell)

I have a source code that runs perfectly fine on Ubuntu, it does some copumtations, and at some points it beeps like this
system("beep -f 400 -l 500");
On Ubuntu I had to do
apt-get install beep
However, I migrated to OpenSuse (not my choice) and now I get this message "sh: beep: command not found", as the command and package are obviously different.
Does anybody know hot to trigger the system beep sound and define the duration and frequency? I have been able to find only one way to change the parameters
setterm -bfreq 500 -blength 500
, but no way to actually trigger the system bell (beep). The following things don't work
echo ^G
echo -e "\a"
PS - the system Bell is enabled at
Configure Desktop -> Application and System Notifications -> System Bell
and you can actually play with this
So, I did what #fvu proposed.
However, one needs to have sudo rights, to do so, otherwise (e.g. at my work-place we don't have sudo rights) there is this output message
Could not open /dev/tty0 or /dev/vc/0 for writing open: No such file or directory
In this case, you should:
sudo chmod 4755 /usr/bin/beep
as proposed here
I noticed that on my OpenSuse 12.3 system, the bell is working in xterm or gnome-terminal, but not in konsole or xfce4-terminal.
If the same applies to your system, then maybe a work-around could be creating a shell script called "beep" which calls xterm and rings the bell:
#!/bin/sh
xterm -e "echo -e '\a'; sleep 1"

Is it possible to open a file in an running instance of Matlab from the command line?

If I already have an instance of Matlab running is it possible to tell open a file in the Matlab editor from outside the Matlab application? I'm wondering if there it is possible do something like this.
Launch an instance of Matlab
$ ./matlab
Open a file for editing using an already running instance of Matlab:
$ matlab_open_file.sh theFile.m
The GUI variant is dragging a file from a folder and then dropping it onto Matlab icon (this actually works under OS X)
Note I know that you can launch Matlab and have it immediately execute a command (you could use this to start the editor on launch). This is not what I want.
I scripted a workaround for Linux (functional on Mint 17.1 with R2014a and R2014b), which I then associated with the .fig and .m file extensions. Note that this requires xdotool to be installed, and the keystrokes are set for Windows shortcuts (by default, MATLAB ships with Emacs shortcuts on Linux, but virtually everyone changes them in my experience). This has the limitation that any text currently on the command line is erased, and there is a small window of time where MATLAB must not lose focus. But in the absence of a more robust solution, it works well enough for me.
#!/bin/bash
# Hacky way to open a MATLAB figure in an existing instance if there is
# one, and start a new instance if not.
# What are we trying to open?
FILENAME="$#";
# Try to identify the main MATLAB window.
MLWINDOW=$( comm -12\
<(xdotool search --name MATLAB\ R | sort)\
<(xdotool search --class "com-mathworks-util-PostVMInit" | sort) )
if [ -z "$MLWINDOW" ]; then
# MATLAB isn't open; we have to open it to proceed.
matlab -desktop -r "open('$FILENAME')"
else
# We use the first existing instance since MATLAB is open
set -- $MLWINDOW
# Jump to the command line and erase it
xdotool windowactivate --sync $1 key --delay 0 "control+0" Escape
# Put the filename on the command line
xdotool type --delay 0 "$FILENAME"
# Select the filename and press ctrl-D to open, then clean up command line
xdotool key --delay 0 "shift+Home" "control+d" Escape
fi
You can type the path+filename into the command line and if a matlab session is open it will open this file in the current matlab session.
Note that this only works if you make sure matlab is the default program to open this kind of file. (Tested with .m file)
I modified Aoeuid's approach because
it did not work for me, as I had reassigned Ctrl+0 which jumps to the command line (and I don't see where I could set this to another value) → I replaced it with the “open file” dialog (Ctrl+O).
I might want to open scripts that are not on the current matlab path → I use $PWD/$filename instead of $filename. You could modify his version by using open($PWD/$FILENAME) and KP_Enter instead of $FILENAME and shift+Home/control+d.
This is the result:
#!/bin/bash
filename="$1"
# Try to identify the main MATLAB window.
MLWINDOW=$( comm -12\
<(xdotool search --name MATLAB\ R | sort)\
<(xdotool search --class "com-mathworks-util-PostVMInit" | sort) )
if [ -z "$MLWINDOW" ]; then
# MATLAB isn't open; we have to open it to proceed.
matlab -desktop -r "open('$PWD/$filename')"
else
## Activate window:
xdotool windowactivate --sync $MLWINDOW && \
## Press Ctrl+O to open the "open" dialog:
xdotool key --delay 0 "control+o" && \
## Wait for the file dialog to open:
sleep 0.5 && \
## Type the file name including the current directory
xdotool type --delay 0 "$PWD/$filename" && \
## Press enter:
xdotool key "KP_Enter"
fi
However, using key presses for an automated process might cause unwanted results.
Make sure that you added you folder to path.
Then you go to folder you need.
and just type in Matlab terminal
your_program_name
Then your program will run.