How to override copy-paste functionality in Windows/Linux? - linux

I want to override the copy-paste functionality in either Windows/Linux. How can I do that?
Most applications support simple copy-paste functionality, where the text copied is the one that is currently selected. I want to modify this functionality. Instead of just copying the text, I want to also copy to clipboard: the name of the process/application from where the text was copied, datetime of the copy-paste and user of the system.
For example, when a user selects a text (say "Hello World") from a browser and pastes it into say notepad, the pasted text should be something like "Hello World" (author: , source: chrome.exe, datetime: ....)
How can I do this (either in Windows or Linux)?

It's been a while since I looked at the clipboard but basically, you just create list to clipboard change events which the OS will send to all interested applications.
When the clipboard changes, look at the current content. When it's text (it can also be HTML or application specific binary data), then get the current content, append the information you want and set the new clipboard content.
If you use Java, you can use Toolkit.getDefaultToolkit().getSystemClipboard(); to get access to the clipboard and then add listeners for the various events.
Related:
Copying to Clipboard in Java

Related

Script to paste a specific string into a text field with a hotkey

I am trying to find a way to paste a predefined string upon entering a specific keyboard sequence, on any app.
For example if I have to paste an url or a password into a field, I can have said password in a hidden script and when I press, say, [ctrl] + [5], it would write "example123" on the text field where my cursor is.
Ideally without copying to the clipboard (I'd prefer keeping what I have on my clipboard and also avoiding to paste a password or such by mistake elsewhere).
I have tried every solution I've found so far that include xclip, xdotool and xvkdb. All of them either do not work or are really inconsistent: They only paste the string sometimes, and when they do, it's usually only part of the string ("ample123" instead of "example123").
I thought of using compose key, which I heavily use anyway to write in french on an us keyboard, but it seems it only supports 1 character sequences, as nothing is printed when I modify my .XCompose to include custom output sequences of len > 1.
I am using Ubuntu 18.04 with Gnome as a DE. Ideally something that also works when logging back (like compose keys).
You need to walk the Document Object Model for either Gnome or your web-page. My concern is that with a desktop script you wont be able to access the web page because you will need to be able to establish a target to send string to. I see in your question that you tried using using "x{tool-name}" to grab the text field element. Delivering the sting really isn't the problem. The problem is getting the GUI element of text box pragmatically. The easiest way to get access to this in a user loaded web-page is with WebExtensions API which is how to make extensions for most modern browsers. Otherwise, if you can get away with only having access to Gnome's GUI I would try LDTP, it's a library used for testing, but it looks like it can be used for automation too.
For keyboard shortcuts:
It really shouldn't matter what the script is doing to how you want to activate it. I would just go to Gnome/Settings/Keyboard and set the path to where I saved the script to be the Command. If you go the WebExtension route, you will want to build the shortcut into your extension.

Is there a way to access files or folders in the clipboard via nodejs?

I am working on a little electron project to get started with that particular environment. Right now I am testing the clipboard and it seems like I can only read and write text, HTML, RTF and a Native Image from / to the clipboard. But is there a way to read in files via fs.readSync(path) and then copy it to the clipboard to simply paste it somewhere on the desktop etc. ?
Note: I have already taken a look at a bunch of clipboard packages on npms.io but I didn't find one that has the desired capability.

IBM Mainframe copy/paste

Disclaimer: I'm new to using Rumba to access IBM Mainframe.
I have currently set up a library for personal use and I have some code that I want to store in a member of this library, how can I copy/paste from a .txt file on my desktop into this program??? As of right now I can successfully copy/paste one line at a time from documents outside of Rumba.
There are various ways. The best one will depend upon the size of the file/amount of data to be transferred.
If it's only a few lines, block copy and paste should work, but you might have to play with Rumba's 'paste' edit settings such as how to handle new lines, etc.
Bigger files can be transferred with the TSO file transfer program indĀ£file (maybe ind$file on your system) which essentially copies a file to the screen and then Rumba 'scrapes' the screen for data to put into a file (this is for a mainframe-to-PC transfer; for going the other way the operation is reversed). This can be surprisingly quick.
Lastly there's FTP - either from the command line or via a program such as WinSCP.
Edit:
Based on your comment that the files are about 300 lines long, I'd look into using Rumba's file-transfer option using the ind$file utility. Once you have the files on one system, speak to your mainframe tech support team about the best way to get them to the other systems.
If you need help uploading the files, then the tech support team should be your first point of call.
What mainframe editor are you running? TSO/ISPF?
I copy and and paste from ".txt" files into ISPS all the time with no problem.
Select the text you want to copy (in the ".txt" file)
Press CTRL-C
Open the mainframe file using ISPF Edit (option 2).
Enter line command "Inn" at the line where where you want the copy to start.
(This inserts "nn" empty lines to receive the copied data. Personally, I usually use "nn"=20)
Position your cursor at the first character of the first empty line.
Press CTRL-V

How does X11 clipboard handle multiple data formats?

It probably happened to you as well - sometimes when you copy a text from some web page into your rich-text e-mail draft in your favorite webmail client, you dislike the fact that the pasted piece has a different font/size/weight.. it somehow remembers the style (often images, when selected). How is it than that if you paste the same into your favorite text editor like Vim, there's no HTML, just the plain text?
It seems that clipboard maintains the selected data in various formats. How can one access data in any one of those formats (programmatically or with some utility)? How does the X11 clipboard work?
The app you copy from advertises formats (mostly identified by MIME types) it can provide. The app you paste into has to pick its preferred format and request that one from the source app.
The reason you may not see all style info transferred is that the apps don't both support a common format that includes the style info.
You can also see issues because an app may for example try to paste HTML, but not really be able to handle all HTML. Or the apps may be buggy, or may not agree on what a particular MIME type really means.
Almost all apps can both copy and paste plain text, of course, but beyond that it's touch and go. If you don't get what seems to make sense, you could file a bug vs. one of the apps.
You may notice that if you exit the app you're copying from, you can no longer paste. (Unless you're running a "clipboard manager" or something.) This is because no data actually leaves the source app until the destination app asks for a format to paste.
There are "clipboard managers" that ask for data immediately anytime you copy and store that data, so you can paste after the source app exits, but they have downsides (what if the data is huge, or is offered in 10 formats, etc.)
The following python code will show available formats for the currently-copied data, if you have pygtk installed. This app shows the ctrl+c copied data, not the middle-click easter egg. (See http://freedesktop.org/wiki/Specifications/ClipboardsWiki)
#!/usr/bin/python
import gtk;
clipboard = gtk.clipboard_get()
print("Current clipboard offers formats: " + str(clipboard.wait_for_targets()))
The code in Havoc P's answer to show the formats of the current clipboard sadly no longer works due to an API change in PyGTK. Here's an updated version as a one-liner:
python -c 'import gi; gi.require_version("Gtk", "3.0"); from gi.repository import Gtk, Gdk; print(*Gtk.Clipboard.get(Gdk.atom_intern("CLIPBOARD", True)).wait_for_targets()[1], sep = "\n")'
In Arch Linux, you can install PyGTK using sudo pacman -S pygtk.
Below are some examples.
Text from Chrome:
TIMESTAMP
TARGETS
SAVE_TARGETS
MULTIPLE
STRING
UTF8_STRING
TEXT
text/html
text/plain
Text from Gnome Terminal:
TIMESTAMP
TARGETS
MULTIPLE
SAVE_TARGETS
UTF8_STRING
COMPOUND_TEXT
TEXT
STRING
text/plain;charset=utf-8
text/plain

Clipboard viewer for programming purposes

I need a clipboard viewer in order to understand the type and contents of the data I'm receiving. Is there any such program available, (for Windows) that lets you explore any type of data currently in the clipboard?
ClipSpy: Unfortunately the only workable multi-format viewer, ClipSpy, shows me the string data wrapped every 10 characters, and expands the hex and binary views which I'm not concerned about.
Start -> Run -> clipbrd
I would use the command-line clipboard tool to send the clipboard contents to a file. Then you view/parse it using any old tool.
I use Ditto which uses an SQL lite database. I am sure you could figure out a way to manipulate the stored data for syntax highlighting or modify the program so that when editing clips it would open with syntax highlighting or in an editor that has it.

Resources