Activate window in Ubuntu 20.04 and send key combination - ubuntu-20.04

In Ubuntu 20.04, I am trying to write a very small script to bring a specified window to the foreground and then send a key combination on this window.
For that I am using:
#!/bin/bash
xdotool search --onlyvisible --class <myWindow> windowactivate %#
xdotool key ctrl+alt+p
Now, while the specified window in <myWindow> is indeed coming to the foreground, the key combination seems to not have any effect, no error showed or anything like that. However, it does have the desired effect if I send the combination manually with the keyboard and with the window in the foreground.
I also tried adding a little delay sleep 2 between the 2 commands, no luck so far.
Missing something here?

except windowactivate
maybe also windowfocus
and little sleep ?
xdotool search --onlyvisible --all terminator \
windowactivate windowfocus sleep 0.5 type 'abc'
maybe also --sync
xdotool search --onlyvisible --class terminator \
windowactivate --sync windowfocus --sync key ctrl+r
man xdotool
windowfocus [options] [window]
Focus a window.
Uses XSetInputFocus which may be ignored by some window managers or programs.
--sync
After sending the window focus request, wait until the window is actually focused.
windowactivate [options] [window]
Activate the window.
This command is different from windowfocus: if the window is on another desktop, we will switch to that desktop.
It also uses a different method for bringing the window up.
I recommend trying this command before using windowfocus, as it will work on more window managers.
--sync
After sending the window activation, wait until the window is actually activated.

Related

Run OMNeT++ simulation from terminal using Qtenv

I'm using Veins (v5.1) along with SUMO (v0.32) and OMNeT++ (v5.6.2) in Linux Ubuntu 20.04 and I'm trying to run the Qtenv from the terminal with the opp_run command. When the simulation window opens I still need to click run (F5) in order to start the simulation. Is there any way to start the simulation directly while using the Qtenv?
In Qtenv there is no way to start simulation automatically after opening the window.
EDIT
However, based on that question you may try use xdotool to start simulation by sending F5, for example:
xdotool search --name Qtenv windowactivate --sync %1 key F5 windowactivate `xdotool getactivewindow`
where Qtenv is a part of the title of window with your simulation.

How to enter keyboard shortcuts in bash script

Is it possible to enter keyboard shortcuts into a bash script? For example, I have tried typing in ^d to substitute for control + d (logout) without success.
ubuntu#vfleet:~$ ^d
-bash: :s^d: substitution failed
ubuntu#vfleet:~$ ^D
-bash: :s^D: substitution failed
I am using screen to run a django development server in the background. One of the commands to run a particular screen as a daemon is control + a + d. The goal is to be able to enter in control + a + d into a bash script. For example:
python manage.py runserver
^a + d
Is this possible?
Edit:
A valid method of avoiding the keyboard shortcut in screen is linked by Eric Renouf in the comments below. screen -d -m sh -c "python manage.py runserver" will start a development server as a daemon. This is a great solution for my particular problem, but it would still be nice to have a solution for the original question at hand.
xdotool package is the solution here.
xdotool key ctrl+d
Full reference
You probably should just start the command without attaching to the screen session in the first place, like
screen -d -m python manage.py runserver
but if you can't do that for some reason, you could detach from a screen session you're currently in by doing:
screen -S "$STY" -X detach
screen saves its current session info in STY, so we'll use that to make sure we're interacting with the correct session (in case there are many). Then we'll use -X to send a command to that session, in this case our command will be detach which will detach all the attached sessions, including the one used to execute that command
So while this doesn't actually send key strokes, it does highlight that there is often another command that you can send to accomplish your goals. Here detach takes the place of ctrl+a+d. Sending quit or running exit could often replace ctrl+d.
Another work-around would be to use expect which you could then use to send the strings containing control characters or hex values of them.

openining xterm and sending commands from xinitrc

In my xinitrc file, how can I execute commands after the xterm has opened so that I can use xdotool to move the mouse etc ?
xterm -geometry 132x45+0+0
xdotool windowfocus
xdotool mousemove 100 100
xdotool click 1
In this example, the xdotool command only execute when xterm quits, I can't add & to the command line for xterm as it will not remain open ?
.xinitrc is a shell script and usually exits with an exec to start your the last application or window manager. When this last application quits, the X session finishes.
Any application started before reaching the end of the script must be stated asynchronously by using a trailing &.
You could end the script with a sleep forever statement (for example this one) to keep the X session open.
Your overall plan is error prone, however, as there is no guarantee that Xterm will be fully stated before the ancillary operations are executed.

Bash script executes fine on its own, but not with cron

I type
crontab -e
my crontab looks like
*/1 * * * * /home/sara/Desktop/kioskscripts/reloadpage.sh >> /home/sara/Desktop/kioskscripts/logfile.log
logfile is created in /kioskscripts but remains empty.
reloadpage.sh looks like this
#!/bin/bash
sleep 5
/usr/bin/xdotool key F5
sh reloadpage.sh
works as expected and simulates f5 being pressed 5 seconds after execution.
The program executed by cron does not have an active window, so you would need to explicitly specify which window you want the keystroke to be sent to using the --window option.
You can get the window id of your currently active window with xdotool getactivewindow and then use that number in an xdotool command. Or you can use xdotool search with various options to find the window you want to direct the keystroke to. Read man xdotool for the various search options. (You can do that in a single command: xdotool search --name Foo key F5 will send F5 to a window with FOO in its name.)
But that will only work if the indicated window accepts the events, and many windows don't.

Detect mouse click in bash script

I'm wondering how to run a bash script in the background that will do something (i.e. run a script, or a command, or whatever) whenever a user clicks the mouse. I'd like this to continue running even if the terminal is closed. Any ideas? Thanks!
If you are using X11, you can try xdotool to catch mouse events
It would be something like:
xdotool search --onlyvisible . behave %# mouse-click getmouselocation
xdotool manual
If you want to run the script in background you can use:
./myscript.sh &>/dev/null &
if you just want to run bash command in xterm on mouse click (or wheel event) you can try this example:
$ echo -e "\e[?1000h"
$ while read -n 6; do echo hellowworld; done
this is for wheel event (for click set 12 instead)
To keep the script running even when terminal is closed you may try nohup.

Resources