Simulating key press using bash - linux

I try to run gedit from terminal and type some text in a file opened there, but have no success. I try to use gedit; sleep 2; xte -x display 'key k'; command but this one run gedit only(without typing the k char). Can some one chip in me?

Xdotool (no idea what happened to the webpage, here is its source) is great for that:
xdotool key k
And with gedit:
gedit & sleep 2; xdotool key k;

Related

Loop function on Bash freezes my Raspberry Pi while executing xdotool commands

I have a rpi4 8gb that on the startup opens chromium and executes this Bash script
The idea is to refresh the browser every x random interval and execute some key strokes
#!/bin/bash
export XAUTHORITY=/home/sean/.Xauthority
export DISPLAY=:0
while true
do
random=$(( $RANDOM % 1740 + 3540 )) #random number between 29 to 59 minutes
sleep $random
xdotool key F11
xdotool key "Ctrl+Shift+r" &
done
Sometimes I found that the system freezes, what could cause the problem?
My guess is that the & sign on the last line is what causes the freeze. However consider this alternative:
I recommend that instead of making an infinite loop (which could potentially freeze up your system in many ways), just assign it as a cronjob in 30min intervals.
this simplified script without loop should do it:
#!/bin/bash
export XAUTHORITY=/home/sean/.Xauthority
export DISPLAY=:0
xdotool key F11
xdotool key "Ctrl+Shift+r"
crontab -e (choose an editor example nano)
add this line at the end of the file: (runs script every hour:00 and hour:30)
*/30 * * * * /home/sean/autorefresh_chromium.sh add the correct path
ctrl+S to save
ctrl+X to exit
That's it. It will run in the background every 30min, even after reboots. The benefit is that this way the script refreshes the browser and exits, so it is not constantly sitting on your system and it cannot get stuck.
I also use xdotool, but not on a pi. I recommend to be cautious if you are running in headless mode, because without a monitor and other GUI components xdotool might not behave as expected. Considering that you refresh a webpage I guess you have GUI so it should be fine.

print to gedit whit xdotool

I want to read the content of a text file, copy it to the clipboard (if it is not empty) and then paste to the gedit window where the mouse cursor is positioned.
Here is my script:
while true
do
if [ -s textfile ]
then
cat textfile | xclip -selection clipboard
xdotool key --clearmodifiers Control_L+v
truncate -s 0 textfile
fi
done
All works except xdotool:
If i press manually CTRL+v it paste correctly.
If i run xev i can see that xdotool send the correct keyboard events, but nothing happen.
I have tried some other commands, like:
xdotool key ctrl+v
xdotool type $(xclip -selection clipboard -o)
xdotool getactivewindow type $(xclip -selection clipboard -o), that gives me the error XGetWindowProperty[_NET_ACTIVE_WINDOW] failed (code=1)
sleep 1 && xdotool key Control_L+v
No Luck.
Can someone help me?
This worked for me :
xdotool type "$(xclip -o)"
I solved, the problem was that Libx11-dev was missing. Once the library was installed, the script worked correctly.

How do I close a GNOME terminal by giving a command in another terminal in linux?

I have opened a gnome terminal as a new different window terminal using the gnome command in Linux. Now what I want is that I want to close only that gnome terminal by giving a certain command in the other terminal.
I had tried a command as 'kill PID', but with that both the terminals are closed.
Can anyone help me with how to close only the gnome terminal by writing command in the other terminal??
Use "xdotool" to send alt+F4 to the target window. Replace zeros with your windowID:
xdotool windowactivate --sync 00000000 key --clearmodifiers alt+F4
You can use "xdotool search" to get the windowID. Insert between quotes the terminal title:
xdotool search --name "text_to_search"
Joining all commands:
xdotool windowactivate --sync $(xdotool search --name "text_to_search") key --clearmodifiers alt+F4
If you need, add "--delay 100" to get a better performance.
To install "xdotool" type:
sudo apt-get install xdotool

Refresh Desktop using xdotool

Hi I am trying to refresh my Desktop, simulate "F5"/"ctrl+R" key pressing using xdotool.
I have tried to xdotool key F5 but I got
^[[15~jake#jake-PC ~ $ ~
and after pressing Enter:
bash: /home/jake: Is a directory
Then I have tried
xdotool search --classname Desktop
73400321
Got that id, then tried:
xdotool search --classname Desktop key F5
xdotool search --classname Desktop key ctrl+R
it didn't refresh my page.
Question: what can I do to refresh my desktop? By the way I am using Ubuntu 16.04. Thank you
You need to call with sync:
xdotool search --onlyvisible --classname Desktop windowactivate --sync key F5
also you can try xdotool script like this:
#!/usr/bin/env xdotool
keydown Ctrl
keydown super
sleep 0.1
key D
keyup Ctrl
keyup super
key F5
Another alternative you can use exec function on xdotool to call show desktop script (or create it)

How to execute a command in a bash script and then focus the appearing window

I have a bash script like this
#!/bin/sh
firefox &
The Firefox window opens, but it doesn't have focus. What can I do so that it has the focus automatically?
I could use some X window tools / commands, but how do I get the window ID of Firefox? The window name is changing with the URLs displayed and therefore not useful.
EDIT: My window manager is Gnome.
xdotool is the tool to do so.
The simplest form for your particular task is
xdotool windowactivate `xdotool search --pid $! `

Resources