expand and xclip

Here is where you can post tips and tricks to share with other users of MX. Do not ask for help in this Forum.
Message
Author
User avatar
b3ta
Posts: 97
Joined: Mon Aug 20, 2018 4:34 am

expand and xclip

#1 Post by b3ta »

Continual learning: even though I knew about expand, xclip was a useful discovery.

I wanted to copy & paste a multi-screen script into LibreOffice for pretty printing and documentation purposes and used these two commands as follows:

Code: Select all

expand -t4 consume-lists | xclip -selection clipboard
I generally write my scripts in "vim", where I have set tabs to be four spaces, as the default eight assumes at least one of a really wide screen or a tiny font or very few levels of indentation, so when I paste code into a document where the font is mono-spaced, I want to replace the tabs in the source properly. That is what "expand -t 4" does.

Since I'm already at the CLI to do the tab expansion, I might as well use the X clipboard to paste the result into my document so that I don't have to fiddle around with temporary files that I must remember to delete in order to avoid future confusion.

So, running the above intelligently [!] expands the tabs in the "consume-lists" script and puts the result in the clipboard, allowing me to Alt+TAB to LibreOffice, where I simply paste it.

thomasl

Re: expand and xclip

#2 Post by thomasl »

I really love automation and I have automated so much that I sometimes forget it's already been done :bagoverhead:
For the use case you have outlined, you could extend the shown command with two more and so automate the whole process. First use wmctrl to switch to the LibreOffice window:

Code: Select all

wmctrl -a "some string that identifies the LO window"
and then use xdotool to paste the clipboard contents:

Code: Select all

xdotool sleep 1 key --clearmodifiers --delay 75 Ctrl+v
I use AutoKey scripts (basically just some Python code) for many of the these things, here's an example which copies a website URL from Palemoon, starts or activates Vivaldi and opens the URL.

Code: Select all

system.exec_command("xdotool sleep 0.4 key --clearmodifiers --delay 75 Alt+d Ctrl+a Ctrl+c")
wnds=system.exec_command("wmctrl -xl")
if not "vivaldi-stable" in wnds:
    system.exec_command("vivaldi "+clipboard.get_clipboard())
    window.wait_for_exist(".+Vivaldi$",7)
else:
    system.exec_command("wmctrl -xa \"vivaldi-stable\"")
    system.exec_command("xdotool sleep 1 key --clearmodifiers --delay 75 Alt+d")
    system.exec_command("xdotool sleep 1 key --clearmodifiers --delay 75 Ctrl+v")
Automation is fickle though, so YMMV.

User avatar
b3ta
Posts: 97
Joined: Mon Aug 20, 2018 4:34 am

Re: expand and xclip

#3 Post by b3ta »

In this case that would have been over-kill, but the general principle is very powerful.

A few years ago I used xdotool as part of a proof of concept to allow a script running on an Android phone to control the display of PowerPoint slides (in LibreOffice) on a PC. The reason was to obviate the need to use a clicker, which simply sends the same key presses to whichever program happened to have focus. That becomes a problem when presenters ignore it when they're told not to use the clicker until they're given the all clear signal from the tech booth. In one case the presenter messed up the auditorium lights because that program had focus at the time. It would also allow tech staff to continue to use the PC while a presentation was going on, which is crucial if (when) someone wants you to download something for them to use in their (usually the next) presentation.

Post Reply

Return to “Tips & Tricks by users (not for help)”