Run several xdotool commands in one line separated from each other
Solution 1
Long story short:
Use a script.
#! /bin/sh
# With some window selection magic, or a sleep
# if you want to do that manually.
xdotool type word
xdotool key Return
And put the path of the script in the Exec
field.
Long story:
According to the xdotool
manpage:
type
Supports newlines and tabs (ASCII newline and tab).
With respect to "COMMAND CHAINING", this command consumes the
remainder of the arguments and types them. That is, no commands can
chain after 'type'.
Command chaining via ;
or &
isn't possible, since that is shell syntax and Startup Applications doesn't support shell syntax. However, if all you wish to do is press Enter after typing something, there's a roundabout way to do so.
When it says "ASCII" newline, it doesn't mean a bare \n
. And command substitution (xdotool type "$(printf '\n')"
, say) eats trailing newlines. Following this xdotools
forum post, I tried this:
xdotool type "$(printf 'date\n ')"
And it worked. But it only works if there is some character after the \n
, and this obviously leaves a trailing space, which would not be what you want.
I modified that to:
xdotool type "$(printf 'date\n\e ')"
And this works and leaves no trailing space. However, it might cause problems for those using Vi mode in their shell.
Thanks to @steeldriver's comments I figured out that this was due to me trying it out on the very terminal I was executing the commands on. Just a small gap between my pressing Enter and the xdotool
command was enough for a single newline to be registered correctly. Thus:
sleep 0.1; xdotool type $'date\n'
So either extending the line by quoting it:
xdotool type 'date
'
or using the shell interpretation as @steeldriver suggested looks like the right option.
However, a script containing:
#! /bin/sh
sleep 1
xdotool type date
xdotool key Return
in the Exec
field worked fine. Indeed, I always recommend using a script for complex commands in a desktop file.
You can have a script with /usr/bin/xdotool
in the shebang, but the manpage says "script
mode isn't fully fleshed out and may fall below your expectations", so I stuck to bash scripts.
I might have been seeing things, but in my first couple of tries, I had to put a (small) . That was an artifact of trying it out on the terminal that was executing the commands instead of another window.sleep
between the type
and key
commands
Solution 2
Seems to me the application is not parsing multiple commands, abut treating it as a single command. As such make it a single command by wrapping it a shell call...
bash -c 'xdotool type date; xdotool key Return'
Now you can also do other shell things...
bash -c 'xdotool type "`date +"%Y-%m-%d_%T`"'
Note that the "date" command used in that last includes a newline! and "xdotool" will output it.
NOTE: if you are doing this as a keyboard macro I would also add a few more options to "xdotool" to make this work better...
bash -c 'xdotool type --clearmodifiers -delay 0 "`date +"%Y-%m-%d_%T`"'
Related videos on Youtube
janot
Updated on September 18, 2022Comments
-
janot almost 2 years
I'm trying to run
xdotool type word
thenxdotool key Return
from Startup Aplications Preferences.
But if I use&&
or;
, xdotool evaluates it as continuation of input.-
Admin over 7 yearsIn my use-case it works with
xdotool type $'word\r'
, where the$''
escapes the\r
to something like return (\n
doesn't work here).
-
-
steeldriver almost 10 yearsA literal newline seems to work i.e.
xdotool type 'word
followed by the 'Enter' key and then the closing'
, as does$'word\n'
(in which bash expands the\n
to a literal newline before passing the argument toxdotool
). -
muru almost 10 years@steeldriver hit-and-miss. That was one of the first things I tried and didn't work.
-
muru almost 10 years@steeldriver What did work was two newlines (two Enters before closing the
'
or two\n
s with the$
trick). -
steeldriver almost 10 yearsthat's odd, the extra newline doesn't seem to be necessary for me - I wonder if it depends on the application that owns the target window (I was using an empty document in geany)?
-
muru almost 10 years@steeldriver thanks for tip. Glad to know I wasn't hallucinating about need for
sleep
(see the note in small text). I wasn't changing windows and just trying it out on the same window, but with asleep 1
, both methods worked with both the same window and after switching. I guess my own Enter keypress somehow interfered with the xdotool newline. However, for the more complex requirement of a desktop file, I'd still recommend the script. -
Eliah Kagan almost 10 yearsThe script way (suggested at the top of this answer) didn't work for me when I tried it in a terminal emulator--
word
is typed, but the Return keypress is not sent (or not registered). Unless I made a mistake (or misunderstood your recommendation), I'm guessing this means it's also hit-and-miss, and not a reliable way to make this work from a desktop file either. -
muru almost 10 years@EliahKagan can you try
sleep 0.1; ./xd
- you seem to have run into the same problem as I have - running the command in the same terminal which is executing it. My assumption with the script is that OP would do other things, such as select a window, etc. along with these commands. See the script towards the end of the answer (where asleep 1
to allowed me switch to a program) worked fine. -
will about 8 yearsG'day fellows. I wonder if it is either bash and xdotool version combinations that give odd result. I found commands I tested :
xdotool type $'date\n'
;xdotool type $'echo "xx"\n
andxdotool type $'ls \n
. All worked with Bash 4.3 and XDoToolversion 3.20140217.1
. I didn't need thesleep
. It also worked from a script.