xdotool

Miscellaneous tools
Post Reply
Message
Author
User avatar
technosaurus
Posts: 4853
Joined: Mon 19 May 2008, 01:24
Location: Blue Springs, MO
Contact:

xdotool

#1 Post by technosaurus »

xdotool is one of those invaluable tools for automating desktop tasks. but it can also be used for much more demanding works such as providing an interface to keyboard only viewer such as in vlc-gtk

here is an example that will activate firefox/seamonkey and paste the clipboard contents into the url bar:

Code: Select all

#!/bin/sh
xdotool search --classname Navigator \
     windowactivate --sync key --delay 250 ctrl+t ctrl+k ctrl+v Return
Attachments
xdotool.tar.gz
(30.27 KiB) Downloaded 2287 times
Check out my [url=https://github.com/technosaurus]github repositories[/url]. I may eventually get around to updating my [url=http://bashismal.blogspot.com]blogspot[/url].

User avatar
sc0ttman
Posts: 2812
Joined: Wed 16 Sep 2009, 05:44
Location: UK

#2 Post by sc0ttman »

Fancy hacking the source to get the following command working?

Code: Select all

xdotool search --name "VLC" behave %@ mouse-click exec 'myscript'
It should run 'myscript' when you click on a VLC (nogui) playback window... But it doesn't... :(
[b][url=https://bit.ly/2KjtxoD]Pkg[/url], [url=https://bit.ly/2U6dzxV]mdsh[/url], [url=https://bit.ly/2G49OE8]Woofy[/url], [url=http://goo.gl/bzBU1]Akita[/url], [url=http://goo.gl/SO5ug]VLC-GTK[/url], [url=https://tiny.cc/c2hnfz]Search[/url][/b]

User avatar
miriam
Posts: 373
Joined: Wed 06 Dec 2006, 23:46
Location: Queensland, Australia
Contact:

another nice use for xdotool, improving text editors

#3 Post by miriam »

I'd grown progressively more and more impatient with geany (well, to more accurate, all text editors) and lack of extensibility. Today I was editing a fairly long text file and adding some simple html markup. After a while, the inability to easily put bold, or other tags around a bit of highlighted text was really starting to bug me.

I knew I could use xclip to fiddle with the clipboard (I use it in a nice little script to rename files from the context menu, but more of that another time), however I was unable to paste the clip back into the text in the editor... until I realised xdotool could be used to do that. Yay! :D

So, I wrote the tiny, 2-line script below:

Code: Select all

#! /bin/sh
# ht_b
# miriam  2012-09-06
# adds bold tags around a selection of text in any text editor
xclip -o | sed 's/^/<b>/' | sed 's/$/<\/b>/' | xclip -selection clipboard
xdotool type "`xclip -out -selection clipboard`"
The script is put in the command path and I drop the icon for it on my desktop. Now, when I want to put bold tags around any text in any editor, I highlight the text and click on the "ht_b" icon on the desktop and it replaces the highlighted text with the same text but with html bold tags around it.

It probably isn't necessary, but I'll briefly explain how it works:
"xclip -o" takes the current selection (what I've highlighted in the text editor window) and pipes it to sed where "<b>" is added to the beginning ("^" is the regular expression for start-of-line), then this is piped to another sed command where "</b>" is added to the end ("$" is the regular expression for end-of-line), then this is piped to xclip where it is put into the clipboard.
The next line uses xdotool to type into the active window at the cursor, replacing the currently selected text. What it types in is taken from the clipboard through xclip again. Simple, huh? Well, it looks simple, but you wouldn't believe the hair-pulling it took me to get to this. :)

I've made a number of other small scripts to do the same for other tags (italic, headlines 1/2/3, and blockquote) and I've put their icons on the desktop too. Clicking on them is a lot easier than hand-editing editing arbitrary text. (It would be even easier if I could get keyboard shortcuts to work properly too instead of having to mouse-click on an icon, but I can't stop the keyboard shortcuts from producing multiple events.)

Another itch scratched. :)
[color=blue]A life! Cool! Where can I download one of those from?[/color]

seaside
Posts: 934
Joined: Thu 12 Apr 2007, 00:19

Re: another nice use for xdotool, improving text editors

#4 Post by seaside »

miriam wrote:
I've made a number of other small scripts to do the same for other tags (italic, headlines 1/2/3, and blockquote) and I've put their icons on the desktop too. Clicking on them is a lot easier than hand-editing editing arbitrary text. (It would be even easier if I could get keyboard shortcuts to work properly too instead of having to mouse-click on an icon, but I can't stop the keyboard shortcuts from producing multiple events.)

Another itch scratched. :)
miriam.

Here's another way to scratch :)

Code: Select all

#!/bin/sh
# substitute a selection with <tag>selection</tag>
# assign script to hotkey

SEL=`Xdialog --no-tags --stdout --title "List of Tags" \
        --menu "Please Choose  a Tag:" 20 51 4 \
        "b"  "Bold" \
        "i" "Italic" \
        "h2" "Large Heading" \
        "p" "Paragraph" \
        "hr"  "Horizontal Rule" \
        "br"  "Line Break"`

xclip -o  | sed 's/^/<'"$SEL"'>/' | sed 's/$/<\/'"$SEL"'>/' | xclip  -selection clipboard
       
xdotool key ctrl+v 
Cheers,
s

User avatar
miriam
Posts: 373
Joined: Wed 06 Dec 2006, 23:46
Location: Queensland, Australia
Contact:

#5 Post by miriam »

Nice idea!
Multiple choice slows my editing, but is much more compact than having several icons on the desktop.

I think I'll use it for less commonly needed tags. Thank you! :)
[color=blue]A life! Cool! Where can I download one of those from?[/color]

User avatar
sc0ttman
Posts: 2812
Joined: Wed 16 Sep 2009, 05:44
Location: UK

#6 Post by sc0ttman »

sc0ttman wrote:Fancy hacking the source to get the following command working?

Code: Select all

xdotool search --name "VLC" behave %@ mouse-click exec 'myscript'
yeah sc0ttman, good idea!
[b][url=https://bit.ly/2KjtxoD]Pkg[/url], [url=https://bit.ly/2U6dzxV]mdsh[/url], [url=https://bit.ly/2G49OE8]Woofy[/url], [url=http://goo.gl/bzBU1]Akita[/url], [url=http://goo.gl/SO5ug]VLC-GTK[/url], [url=https://tiny.cc/c2hnfz]Search[/url][/b]

User avatar
technosaurus
Posts: 4853
Joined: Mon 19 May 2008, 01:24
Location: Blue Springs, MO
Contact:

#7 Post by technosaurus »

for more commonly used actions, I like to set up a jwm hotkey (for instance Ctrl+g to google the clipboard in default browser)

defaultbrowser https://www.google.com/search?q=${CLIPBOARD// /+}
Check out my [url=https://github.com/technosaurus]github repositories[/url]. I may eventually get around to updating my [url=http://bashismal.blogspot.com]blogspot[/url].

seaside
Posts: 934
Joined: Thu 12 Apr 2007, 00:19

#8 Post by seaside »

sc0ttman wrote:
sc0ttman wrote:Fancy hacking the source to get the following command working?

Code: Select all

xdotool search --name "VLC" behave %@ mouse-click exec 'myscript'
yeah sc0ttman, good idea!
scOttman,

He sure does have good ideas :D

Would this do-

Code: Select all

xdotool search --name "VLC" behave %@ focus exec echo Yes
Strickly speaking, it's not "mouse-click, but it does fire whenever clicked...

technosaurus:,

That's a handy technique. Also Parcellite can perform actions with clipboard/selection items and have them assigned to it's own hotkey.

Regards,
s

User avatar
technosaurus
Posts: 4853
Joined: Mon 19 May 2008, 01:24
Location: Blue Springs, MO
Contact:

#9 Post by technosaurus »

annoying mouse fun

Code: Select all

while :; do xdotool mousemove_relative -- -$(($RANDOM % 10)) $(($RANDOM % 10)); xdotool mousemove_relative -- $(($RANDOM % 10)) -$(($RANDOM % 10)); done
Check out my [url=https://github.com/technosaurus]github repositories[/url]. I may eventually get around to updating my [url=http://bashismal.blogspot.com]blogspot[/url].

goingnuts
Posts: 932
Joined: Sun 07 Dec 2008, 13:33
Contact:

#10 Post by goingnuts »

technosaurus wrote:annoying mouse fun
Yeah - kind of shaking...attached - for now quite untested - static build of xdotool-2.20110530.1

musher0
Posts: 14629
Joined: Mon 05 Jan 2009, 00:54
Location: Gatineau (Qc), Canada

#11 Post by musher0 »

technosaurus wrote:annoying mouse fun

Code: Select all

while :; do xdotool mousemove_relative -- -$(($RANDOM % 10)) $(($RANDOM % 10)); xdotool mousemove_relative -- $(($RANDOM % 10)) -$(($RANDOM % 10)); done
People with Parkinson's won't think it's funny !!! :lol:
Cure here is simply cntrl-C, luckily. :)
musher0
~~~~~~~~~~
"You want it darker? We kill the flame." (L. Cohen)

User avatar
greengeek
Posts: 5789
Joined: Tue 20 Jul 2010, 09:34
Location: Republic of Novo Zelande

#12 Post by greengeek »

Thanks for the xdotool file. I have tried placing it in /usr/bin and /usr/sbin and it seems to work regardless of which of those two places I put it. Which location would you recommend please? Does it matter? Thanks

User avatar
miriam
Posts: 373
Joined: Wed 06 Dec 2006, 23:46
Location: Queensland, Australia
Contact:

#13 Post by miriam »

Hi greengeek. I'm not sure how much of the following you know, so I'll just rattle on about it and you can take what you want from it. :)

The $PATH variable shows all the places your computer looks for executable programs that you can run without having to type their location, for example the "echo" command is probably in /bin but you don't need to tell the machine where it is in order for Linux to find it. You can show the $PATH variable by typing

Code: Select all

echo $PATH
into a terminal, and you can see that /bin is listed there, along with /usr/bin and /usr/sbin and a number of other places.

The $PATH variable is usually defined in your /etc/profile file, though it can be added to and modified at any time, which can be dangerous. A while back I installed a program which wrecked my $PATH and caused my system to suddenly not recognise many of the programs on my machine. It took me quite a while to track down. Instead of using the standard method of extending the PATH

Code: Select all

PATH=$PATH:/the/new/dir/of/programs
they simply gave the new dir without including the old $PATH but luckily it defined the new $PATH in an additional file called /etc/profile.local. I just commented the damaging lines out and my machine was good after the next reboot.

Why do some programs go in some directories and not others? It seems to be a mix of history and whim of the programmer who writes the installer script. I think /sbin and /usr/sbin and /usr/local/sbin are supposedly reserved for "system" programs, but you can find any kind of program in there. (After all, how exactly does one define a "system" program?) All the bin directories (and there are lots of them) hold all kinds of executables and script programs. It is a bit of a mess. I think the base level executables (/bin and /sbin) hold files that the system needs during boot-up. The deeper level ones, such as those in /usr and /usr/local are supposedly related to files made available for multiple users. The main focus of Puppy is as a single-user OS so this is less important for us.

For the standard rationalisations, see http://en.wikipedia.org/wiki/Filesystem ... y_Standard

For the real reasons, see http://www.osnews.com/story/25556/Under ... bin_Split/

I've long felt the Linux filing system needed an overhaul. It is not likely to happen though. It surprises me how conservative Linux enthusiasts can be and how much anger can be provoked by such suggestions. My personal feeling is that Linux is simply a tool -- a brilliant tool. It seems to me that the better a tool is, the more important it should be that we not let it get rusty. I think this is the great value of Puppy Linux. Barry Kauler, and the community of people who've come together with Puppy, have produced an efficient, fast, tool that works far more effectively than most Linuxes, but there will always remain more to be done.
[color=blue]A life! Cool! Where can I download one of those from?[/color]

User avatar
greengeek
Posts: 5789
Joined: Tue 20 Jul 2010, 09:34
Location: Republic of Novo Zelande

#14 Post by greengeek »

Oh My Gosh. Thanks Miriam. I had no idea of the complexity behind what I was asking. I wondered why no-one had chimed in with a quick answer...
Thanks for taking the time to expand my knowledge base. :-)

EDIT:
OK, so it seems that echo $path and echo $PATH are different things...Now I have to worry about lowercase versus uppercase??? Tccchhh! I'll never get the hang of this stuff...

EDIT again:
Ha! I had to laugh at his description of the Linux filesystem:
This is like buying a beautiful car, only to realise the engine is made of cake and rotting brocolli. ..Mac OS X is especially egregious in this regard - open a terminal and check the directory listing at root - it's an even bigger mess than regular UNIX.

User avatar
miriam
Posts: 373
Joined: Wed 06 Dec 2006, 23:46
Location: Queensland, Australia
Contact:

#15 Post by miriam »

Don't worry. It is surprising how quickly you'll get used to the operating system being case-sensitive. Case-insensitive operating systems cause all kinds of subtle problems.

Some commands in Linux allow you to look for things in a case-insensitive way, for example:

Code: Select all

find ./ -iname "*txt"
looks in the current directory (./) and below for files ending in "txt", regardless of uppercase or lowercase. It will find hello.txt, SomeTxt, BLAH.TXT, and so on in any directory deeper than the directory the command was issued from.

Coming from an operating system that doesn't see the distinction between upper and lower case feels confusing at first, but after a little while you'll wonder how you put up with it. It becomes particularly annoying when dealing with VFAT-formatted thumbdrives. If you try to put two different files, "readme" and "README" on it then VFAT can't see the difference and will want to overwrite one of them. Be careful.

VFAT also imposes annoying limitations on the letters you can use in filenames (no ? or : or various other characters). Infuriating when giving files to MSWindows friends.
[color=blue]A life! Cool! Where can I download one of those from?[/color]

Post Reply