How to paste text from the clipboard into a text field - python

How can I paste a string into a text field, like for example the Windows search bar or a text editor using Python?
I searched a lot for this, but all I can find is hundreds of questions asking how to copy to the clipboard or getting a string from the clipboard. What I want to do is paste from the clipboard into the active Window, as if I were pressing ctrl+v. If possible, I want to avoid the seemingly complicated way of emulating the actual low-level keyboard press.

In Windows clipboard is considered IPC (Inter process communication).
You can read some of the details here.
In python you can use this library I think supporting major OS.
For linux specific are options like xclip but I think it's desktop environment dependent.

Related

How to copy text from IDLE?

Is there a way to copy text out of Python IDLE on a Mac? When I highlight text and copy then past into a text editor, I get the same text pasted. It is some of the first text I start with in IDLE. None of the other text will copy out.
From The Things I Hate About IDLE That I Wish Someone Would Fix (from 2011):
1.2) NEW FEATURE: Auto-Copy-On-Highlight
Once we get rid of being able to move the cursor off the last line, that opens a new opportunity to implement an automatic copy-on-highlight feature that many terminal and IRC client programs implement. Since this text is read-only, the only reason a person has for highlighting it is to copy it (they can’t delete it.) As soon as the user highlights text in the shell window, it is copied to the clipboard.
Looks like the best you can do is save as a .py file. Open that in a text editor and continue working in IDLE. With each save, the text editor will refresh with all updates, including errors. At least TextWrangler will.
Sometimes in things like a linux terminal you can't do a normal copy and paste, try holding Command/Control + Shift + C after highlighting things and seeing if that works, since you're on mac i'm not sure if it would be the Command or Control key so try both
EDIT: There is an apple discussion about the exact same thing. They discuss going into the options menu, then into configuration and navigating to the keys section. Then you use a built in key-set, their suggestion is using IDLE Classic Mac.
I cannot tell from your description exactly what you did and what happened. But I can recommend that you upgrade to 3.4.4 or 3.5.1 (or 2.7.11 for 2.7 users). Among other improvements, they all contain a patch to make right-click for context menu work on Mac Aqua. This was issue 24801 on the CPython bug tracker.
If there is still an actual problem on Mac, I would like to know so it can be fixed.

Copy and paste text in python

I'm currently developing a language transition software for linux using python GTK. it has two entries. what it basically does is, when the user type some word in a text entry 1, the translated text appears in text entry 2 and when the user press space bar, I want to paste the translated text to another application's text area. not to a text entry in my application. I think it needs to switch to the other application, paste the text and switch back to my application.
As an example, if gedit is opened in background, when a user type a word in my application and press the space bar, the translated word should be pasted in gedit.
Sometimes it may be possible to complete my task by set my application window as a popup window(type=WINDOW_POPUP) without set it as top level window(type=WINDOW_TOPLEVEL). but I'm not clear with that.
I think the problem is clear to you. If anyone can help me to solve this problem, it would be a great help for me. Thanks all.
this looks like a dbus solution and not a fun one. As for clipboard manipulation in GTK http://developer.gnome.org/gtk3/stable/gtk3-Clipboards.html will get you where you need to go, most of the C functions have a direct equivalent in python ( http://developer.gnome.org/pygtk/stable/class-gtkclipboard.html ).
Communication between applications in GTK+ is not a whole lot of fun and when I worked on a project that had to do so, I ended up using DBUS (C++) but there might be a good python port for dbus, I haven't checked.

Isn't there a PRIMARY selection clipboard in windows

First of all, its a windows question. Let me introduce you to the linux counterpart.
In Linux, when I select a text anywhere in X Windows System, its gets copied to PRIMARY clipboard.
Selecting a clipboard & explicitly copying it using Ctrl+C causes it to be copied to secondary keyboard.
In linux, I made a dictionary that automatically searches for word that is selected in current window. For that, I just copied the value of primary clipboard.
What's the equivalent of PRIMARY selection in windows? I want to retrieve the current selection in the current window using python.
There is no equivalent in Windows. There is just the single global clipboard which contains a single item, albeit in potentially multiple formats.
I want to retrieve the current selection in the current window.
That's not trivial in Windows. You can do it using, for example, UI Automation, for apps that support UI Automation. Most modern apps do support that but more obscure ones will not readily yield the information you are after.

Get content from open window in Linux

I want to collect data and parse it eventually from an open window in linux.
An example- Suppose a terminal window is open. I need to retrieve all the data that appears on that window. After retrieval, I would parse it to get specific commands entered.
So is it possible to do that? If so, how? I would prefer to use python to code this entire thing.
I am making a guess that first I would have to get some sort of ID for the open window and then use some kind of library to get the content from the window whose ID I have got.
Please help. I am quite a newbie.
You can (ab)use the assistive technologies support (for screen readers and such) that exist in the toolkit libraries. Whether it will work is toolkit specific—Gtk and Qt have this support, but others (like Tk, Fltk, etc.) may or may not.
The Linux Desktop Testing Project is a python toolkit for abusing these interfaces for testing GUI applications, so you can either use it or look how it works and do similar thing.
I think the correct answer may be "with some difficulty". Essentially, the contents of a window is a bitmap. This bitmap is drawn on by a whole slew of primitives (including "display this octet-string, using that encoding and a specific font"), but the window contents is still "just pixels".
Getting the "just pixels" is pretty straight-forward, as these things go. You open a session to the X server and say "given me the contents of window W" and it hands it over.
Doing something useful with it is, unfortunately, a completely different matter, as you'd potentially have to (essentially) OCR the bitmap for what you want.
If you decide to take that route, have a look at the source of xwd, as that does, essentially, that.
Do you have some sort of control over the execution of the terminal? In that case, you can use the script command in the terminal session to log all interaction to a file and then read and parse the file.
$ script myfile
Script started, file is myfile
$ ls
...
$ exit
Script done, file is myfile
$ parse_file.py myfile
If the terminal is running inside of screen, you have other options as well. Screen has logging built in, screen -X sends commands to a running screen session (man screen).

How to interact through vim?

I am writing an editor which has lot of parameters that could be easily interacted with through text. I find it inconvenient to implement a separate text-editor or lots of UI code for every little parameter. Usual buttons, boxes and gadgets would be burdensome and clumsy. I'd much rather let user interact with those parameters through vim.
The preferable way for me would be to get editor open vim with my text buffer. Then, when one would save the text buffer in vim, my editor would get notified from that and update it's view.
Write your intermediate results (what you want the user to edit) to a temp file. Then use the $EDITOR environment variable in a system call to make the user edit the temp file, and read the results when the process finishes.
This lets users configure which editor they want to use in a pseudo-standard fashion.
Check out It's All Text!. It's a Firefox add-in that does something similar for textareas on web pages, except the editor in question is configurable.
You can also think about integrating VIM in to your app. Pida does this

Categories