How to Use Xdotool to Enter a Web Console Command

How to send a shortcut by bash to a program?

Now found the bug on bug tracker:

  • https://github.com/jordansissel/xdotool/issues/340

Found the follow bug reason:

"The fact that xdotool doesn't seem to work is probably related to applications detecting and discarding synthesized events:

Sending keystrokes to a specific window uses a different API than simply typing to the active window.

[...]

Many programs observe this flag and reject these events."

Source: Automatic web-page refresh using xdotool - not sending key after window focus

Found the follow solution:

"With that in mind I was able to make it work with the series of commands below. This reloads both Chromium and Firefox.

cwid=$(xdotool getwindowfocus) # Save the current window
twid=$(xdotool search --name somename)
xdotool windowactivate $twid
sleep 0.1 # The key event might be sent before the window has been fully activated
xdotool key --window $twid F5
xdotool windowactivate $cwid # Done, now go back to where we were

"
Source: Automatic web-page refresh using xdotool - not sending key after window focus

Automatic web-page refresh using xdotool - not sending key after window focus

After trying several different approaches to get xdotool to work correctly, I'm inclined to believe that xdotool itself is the issue. Here is what I tried, none worked.

  • Running the command (and variations - removing/adding args) from Terminal.
  • Running the command (and variations - removing/adding args) from a SH script.
  • Changing between F5 and ctrl+r keys, as they should both refresh a Firefox page.
  • Trying other parameters, such as:
    • --window to set the window the keys are to be sent to.
    • --delay to add a delay before the key is sent, after the window is focused.
    • Adding a sleep before the key is sent, after the window is focused.

I also tried these commands in a script, as the frontpage for xdotool recommends, although it states this is the "older" version, as it is separated into multiple commands. The "new" version was the version I was trying to execute before and is a single command (see question).

WID=`xdotool search "Firefox Page Title"`
xdotool windowactivate --sync $WID
xdotool key --clearmodifiers ctrl+r

All of the above attempts ALWAYS correctly focused to the window I wanted, but it does not send the key whether it was F5 or ctrl+r.

However, the following worked correctly:

xdotool selectwindow key ctrl+r

OR

xdotool selectwindow key F5

The selectwindow command, when executed, turns your cursor into a rectangular selection tool at which point you can select the window you want to be focused and, in this case, what window to send either the ctrl+r or F5 key to. Unfortunately, this is not what I was looking for, as it requires user input to work correctly.

Final Solution:

My solution (since I was attempting to use xdotool to constantly refresh a web-page) was to use the ReloadEvery Firefox add-on, which refreshes any page you set it on in any time interval you choose. It is intended to be a replica of the Opera browser's built-in automatic page refresh feature, and thus far, it works well.

For those of you who use Chrome and are looking for a similar solution, there are plenty of add-ons available for you too. https://chrome.google.com/webstore/search/auto%20refresh

How to make xdotool work with matchbow-window-manager?

I got it to work. I eventually found this tutorial and used some ideas from it. I'll post the solutions for the people who may have a similar problem.

This is what i placed in the /home/pi/.xinitrc file:

#!/bin/sh
xset -dpms
xset s off
xset s noblank

// not sure if this is needed.
killall -TERM matchbox-window-manager 2>/dev/null;
killall -9 matchbox-window-manager 2>/dev/null;

exec matchbox-window-manager -use_titlebar no &
iceweasel [someURL] &
sudo /etc/xdo_test.sh

I changed the python script to a shell script an inserted the following code:

sleep 20
$WIN=$(xdotool search --onlyvisible --class Iceweasel|head -1)
xdotool key --window $WIN c

while:
do
sleep 300
done

The while loop at the end is something I added because Xserver crashed from the moment it lost connection to the script. I'm still looking for a cleaner solution to end the script but this works for now. I'll update this awnser when I find one.

Thanks Sebastian Stigler for the help!

`xdotool type ` - randomly repeats characters

Here is another solution, tested and working on my Ubuntu 11.04 system:

#!/bin/bash

function open_lxterminal_execute_hold() {
xwininfo -name $1 > /dev/null 2>&1
if [ $? -eq 0 ]; then
echo "Window for title '$1' already exists"
else
t=$(tempfile)
echo ". ~/.bashrc" > $t
echo "$2" >> $t
lxterminal -t $1 -e "$SHELL --rcfile $t" &
fi
}

#pkill devilspie
#cd ~/my_src/ruby_apps/ro
open_lxterminal_execute_hold 'rails-commandline' 'ls'
open_lxterminal_execute_hold 'rails-development' 'tail -fn 100 log/development.log'
open_lxterminal_execute_hold 'rails-user_case' 'tail -fn 100 log/thin.0.log'
#open_lxterminal_execute_hold 'rails-console' 'rvm use ruby-1.9.3-pl94@ro && rails c'
#devilspie -a 2>/dev/null &

As you may notice, I've commented some lines for testing, so you should remove the trailing '#' from the commented lines before running the script on your system.

The trick I've used is to start in each terminal a new shell with the "--rcfile" option.

This way, there is no need to use the "xdotool", "wait" and "sleep" commands.

how to escape characters in xdotool

Similar problem here:
http://code.google.com/p/semicomplete/issues/detail?id=13#c29

Try:

xdotool type setxkbmap de
xdotool key Return
xdotool type "sh path-to-script/deploy.sh $1"
xdotool key Return

Maybe it works better if you write a script:

#!/bin/bash
xterm -e "ssh myserver \"sh path-to-script/deploy.sh $1\"; sleep 5"

and invoke it like this:

./theAboveScript.sh &
sleep 10
xdotool type "password"
xdotool key Return

How to Switch Focus to a Gnome Terminal Tab via Script or Shell Command

I solved this with xdotool

On one line, first, open a new tab with the key command. The default behavior is to switch focus to this tab. Then, use the type command to run a function, script, or other program in the new tab. Last, use the key command to "press enter." Repeat for N-many tabs!

# inside a file loaded by .bashrc which contains all my functions:
function setupterm() {
# run a command, like set a title, in the current window/tab
customCommandOne
# do the needful as described above
xdotool key Control+Shift+t && xdotool type customCommandTwo && xdotool key Return
# repeat for n-many tabs
xdotool key Control+Shift+t && xdotool type customCommandThree && xdotool key Return
# return focus to first tab
xdotool key Control+Page_Down
}


Related Topics



Leave a reply



Submit