10

Given:

I have Firefox with the [Firefox Page Title] page open on my Ubuntu computer.

Here is my command:

xdotool search "[Firefox Page Title]" windowactivate --sync key --clearmodifiers ctrl+r

Documentation:

xdotool website with documentation/examples is here.

Example straight from the xdotool website:

# As of version 2.20100623, you can do this simpler version of above:
xdotool search "Mozilla Firefox" windowactivate --sync key --clearmodifiers ctrl+l

Notes:

I'm using xdotool version 2.20110530.1.

The command correctly focuses my screen to whatever window title I choose, but it doesn't send the ctrl+r key to the window, as the website doesn't refresh. I receive no error messages from the command. (Refresh shortcut in Firefox is ctrl+r)

Wanted Behavior:

The command will hopefully (when it works) be applied to a web server statistics page that is open 24/7 on my server computer, I'd like it to refresh the page automatically so I can view new traffic to my website without me having to do anything.

EDIT: I'm open to using other applications that can provide this functionality, if you know of something else that's easy/similar, please provide it as an answer! If I can't get this working I'll default to the next best thing.

CODe
  • 2,253
  • 6
  • 36
  • 65
  • It seems like this keys are consumed by Flash, not by browser, try to move focus away from the plugin first (if you use Flash at all). – Dmytro Sirenko Aug 19 '12 at 14:27
  • 1
    The Opera browser has built-in functionality for automatic page refresh. – Dmytro Sirenko Aug 19 '12 at 14:28
  • @EarlGray I'll check out Opera, thanks. I don't think the command is being consumed by Flash (the page I'm grabbing focus on doesn't have any flash), I think it's either an issue with the tool or something I'm doing wrong in the command. – CODe Aug 19 '12 at 14:37
  • Since I'm using Firefox, here's an auto-refresh add-on: https://addons.mozilla.org/en-US/firefox/addon/reloadevery/ – CODe Aug 20 '12 at 08:05

7 Answers7

14

I was trying to send keystrokes to an application and I also concluded that xdotool just doesn't work as described. I ended up using xvkbd to do the same thing.

For your example the following command refreshes a page in Firefox:

xvkbd -window Firefox -text "\Cr"
John Slade
  • 12,246
  • 2
  • 25
  • 20
  • Good alternative solution to my problem for people who still want to use a command line tool. +1 – CODe Mar 24 '13 at 19:39
  • 1
    This is very good solution. However it refreshes only one firefox window. Can it refresh all firefox pop-up windows too? – Dusan Milosevic Jul 16 '14 at 18:06
  • 1
    According to the manual for xvkbd using the -window option "If there are two or more windows which have the name specified with this option, the window which was found first will be selected". The -window option can take a window ID and you might be able to use http://stackoverflow.com/questions/2250757/is-there-a-linux-command-to-determine-the-window-ids-associated-with-a-given-pro to get the window IDs, and then make multiple calls to xvkbd. Possibly worth asking this as a separate question - someone else might know a better way. – John Slade Jul 16 '14 at 19:22
  • worked for me after a lot of nervous wasted with xdotool not working somehow. thx a lot! – sandric Jul 03 '15 at 15:35
  • Thanks bro! I could'nt make Xdotool work with none of those comments, but i integrated xvkbd on the shell script alongside xdotool and it worked like a charm! Thanks a lot @JohnTESlade – Raul Chiarella Jul 07 '21 at 13:36
7
xdotool search --onlyvisible --classname Navigator windowactivate --sync key F5

runing arch linux

hepha
  • 81
  • 1
  • 2
  • --classname Navigator was what I was looking for. Tried --classname Firefox, --classname firefox but they didn't work. – AAAfarmclub Jan 05 '19 at 09:38
4

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

CODe
  • 2,253
  • 6
  • 36
  • 65
  • Perhaps you weren't finding your window? What happens when you do something like, if you use firefox, for a test on this page: `xdotool search "firefox - Automatic" key F5`, that works for me. –  Dec 30 '15 at 05:46
  • *ReloadEvery* seems gone but as of writing this there is **Tab Reloader** - which works fine ! – Robert Riedl Sep 04 '20 at 07:05
4

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.

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
Community
  • 1
  • 1
phunehehe
  • 8,618
  • 7
  • 49
  • 79
1

I have key bind win + shift + s to get the window id using

xdotool getactivewindow getwindowgeometry

and win + s to refresh

xdotool key --window savedWindowID ctrl+r
Nafis Ahmad
  • 2,689
  • 2
  • 26
  • 13
1

The following xdotool command works perfectly for me (switches to the first found Firefox window, refreshes current tab):

xdotool search "Navigator" windowactivate key 'ctrl+r'

Running Ubuntu 14.04.1 on xdotool 3.20140217.1

luopio
  • 507
  • 4
  • 14
1

For me, the following works:

xdotool search --onlyvisible --class Firefox key F5

as well as

{ 
   xdotool search --onlyvisible --class Firefox windowfocus
   sleep 0.1
   xdotool key ctrl+r
}

but

xdotool search --onlyvisible --class Firefox key ctrl+r

and

xdotool search --onlyvisible --class Firefox windowfocus key ctrl+r

do not work.

So it seems that using a key combo with modifiers requires a little delay, at least with Firefox (I can send ctrl-keys to Emacs with no such issues, however).

unhammer
  • 4,306
  • 2
  • 39
  • 52