ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
I'm looking for a way to send the output from a script or shell command into the currently-focused x-window; specifically so that I can "type" something automatically.
For example, I'd like to run a script from a hotkey while focused in the firefox text dialog I'm using now, and have the output print here.
So far, I've found I can use xdotool or xvkbd to "type" some text into x, like this:
Code:
#!/bin/bash
text_string="Foo to the bar to the bat"
xdotool type "$text_string"
#or
xvkbd -text "$text_string"
#xdotool seems to handle odd characters better than xvkbd.
When the script is run through a hotkey, either command will print the text into any X application, just like I want.
The problem is that both of them are very slow, especially when the text string is longer than just a sentence or two. They both print each character separately, which can take forever if the string is many paragraphs long.
Is there command that will do the same kind of thing, but print the text all at once, as if you were pasting it from the clipboard something? I've looked around but I haven't found any function like that.
Yes, I know about how the clipboard and selection work. What I hope is to find some function or program that lets me send the output from the script/shell directly into the text area of another program, so that I hopefully won't need to go through them.
I'd already looked at xclipboard, and it doesn't seem to be able to do what I want. It only moves the data in the clipboard to and from a save buffer. It doesn't do any actual pasting, at least not as far as I can determine.
Most macro programs suffer from the same problem as in my first post. They "type" the text one character at a time. Besides, there are only a handful of macro recorders I know of for Linux; xmacroplay, which is too limited to use here, cnee, which has the above problem (I can't find any way to speed it up), and JW_Rrecord_Playback, which seems to be gui-only. Unless you know of another one that can "paste" selections all at once and can be used in a script, I don't think this is an option.
I've found the following workaround (which kind of does what you suggested). It copies the text to the x-selection and then uses xdotool to "press the button" and paste it. But again, I'd rather have something that works directly if I could find it.
Code:
#!/bin/bash
text_string="Foo to the bar to the bat"
echo "$text_string"|xsel -i
xdotool click 2
Personally, I think you may be trying to solve the problem in the wrong way. I've never had to do such a thing as paste output into a GUI program, there was always a simpler easier CLI way. Are you sure this is what you want ? Because I think it's gonna be messy and unreliable.
Yes, this is what I need, because I'm trying to create an all-purpose launcher where, among other options, I can select and paste predefined text clips into any program. Kind of a permanent clipboard set, as it were.
Some of the better clipboard extenders on windows provide this function, but I haven't seen any good implementations of it on Linux.
Perhaps there are better ways to go about it program-wise, but at the moment I only know how to do shell-scripting. Maybe eventually in the future I'll know enough about some other language to implement it more efficiently.
The problem is that both of them are very slow, especially when the text string is longer than just a sentence or two. They both print each character separately, which can take forever if the string is many paragraphs long.
xdotool has a --delay option, did you try lowering it?
Quote:
Originally Posted by http://www.semicomplete.com/projects/xdotool/xdotool
--delay milliseconds - Delay between keystrokes. Default is 12ms.
My solution is a text file, email address phone number etc.
I open it on my desktop and copy and paste middle button.
simples!
i don't think you'll have much luck.
window managers apps, they can all grab the keyboard.
i don't think may app programmers will have predicted a future
when you come along and place an input procedure for your app
unless they are Philip K. Dick
That's interesting. The Debian version of xdotool does not have a delay option. It's not listed in the man page and gives an "unknown command" error if I try to use it. I certainly would've been using it if it were there. Maybe it's a new function that hasn't made it here yet.
bigearsbilly, As I mentioned, I'm trying to replicate something I miss from my old Windows days. Cut and paste can get quite tedious, and what you do requires opening up a separate window to do it. Besides, this is just the first step. If I can get the basic stuff working satisfactorily I may go on to bigger and better, like a popup that allows me to edit the current contents of the clipboard before pasting.
Maybe there's something I can use, maybe not. But that's why I'm asking. There might have been something I missed. I don't see it as an unusual request. Why shouldn't there be tools for controlling output to x-window text areas? There are programs like xdotool and xdialog and macro recorders and such, why not something that handles text more directly? It can't be too difficult to do.
In any case, at the very least my xsel workaround will do for the time being.
Why shouldn't there be tools for controlling output to x-window text areas? There are programs like xdotool and xdialog and macro recorders and such, why not something that handles text more directly? It can't be too difficult to do.
Unfortunately there is no such thing as an X window text area. There are only GTK text areas, QT text areas, XAW text areas, FLTK text areas, etc...
Quote:
The Debian version of xdotool does not have a delay option.
Yes, and the Debian version says:
Code:
type something to type
Types a series of letters. In order, as fast as possible.
QT, GTK and such all run on x-windows, and take their text input from x, as opposed to the direct keyboard input used in terminal windows (I admit my understanding of how it all works is rather vague). Applications like xdotool, xvkbd, xmacro and such don't care if the program is QT or GTK or whatever, they simply tell x to simulate the regular keyboard input. Various programs and libraries written languages like C can also manipulate text input and output, I suppose by calling xlib or something. So if you can do all that, why can't you have a simple cli tool that uses x to send blocks of text straight from a shell to another program as well?
I was really only hoping that there might be something like a more advanced cli wrapper for xlib or something that I didn't know about, or maybe a way to call up another language like perl or python to do it (if they have that ability). Having to work through a one-character-at-a-time keyboard simulation just seems like an inefficient way to go about it.
Well, in any case, it looks like there's nothing available that does what I want, so I'm just going to have to live with what I have now. At least the "selection paste" solution I found gets around the speed problem.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.