How to access clipboard history from an external application?

Asked by Oliver Sauder on 2014-04-18

Since Diodon version 1.1.0 is there no storage.xml file anymore. How can I access the clipboard history from an external application such as a bash script?

Question information

Language:
English Edit question
Status:
Open
For:
Diodon Edit question
Assignee:
No assignee Edit question
Last query:
2014-04-18
Last reply:
masavini (msavini) said : #1

i have several bash scripts that process clipboard content...

in order to process only the last selection (between primary and secondary clipboards) i used to get the last record in diodon storage.xml file.

now that file has gone and my scripts do not work anymore...

any suggestion on how to get the last selection i made (primary or secondary) with a bash script?

thanks!

Oliver Sauder (sao) said : #2

Do I understand this correctly that what you need is pretty much what is currently copied into the clipboard and primary clipboard?

For this you do not really need Diodon but there is a tool called xclip available which can handle this quite easily.

Install it with the following:
sudo apt-get install xclip

For getting current content of the clipboard you can run the following:
xclip -selection clipboard -o

For primary selection you can use the following:
xclip -selection primary -o

Is this sufficient for you? There will be still possibilty to access the clipboard history from a bash script (actually more possibilities then before) I would need to write a script first to make it easily accessible from bash.

masavini (msavini) said : #3

the matter is the script can't know what was selected last between primary and secondary clipboards.
the method i used to know which was the newer was to simply get the last record of the xml file: it reported both primary and secondary clipboards entries sorted by time...

Oliver Sauder (sao) said : #4

I assume you are talking about CLIPBOARD (copying with Ctrl + C) and PRIMARY selection (selection by mouse). SECONDARY clipboard actually also exist is though a bit different see here http://standards.freedesktop.org/clipboards-spec/clipboards-0.1.txt

So as a content has to be selected first before it can be copied to CLIPBOARD is the content in the PRIMARY selection always newer or otherwise the same then in the CLIPBOARD (there are only some few rare cases where an application doesn't support the PRIMARY clipboard).

So this means if you want to get the newer one you can always simply get the content of the PRIMARY selection with the following command:
xclip -selection primary -o

Does this help or am I missing anything?

Do not get me wrong, I still like the idea of having easy access to the whole history in a script like bash. I will add this to the wishlist though as it will take a bit longer to create a good wrapper script.

masavini (msavini) said : #5

you right about the clipboards definititons: what i used to name "primary clipboard" is primary selection, while "secondary clipboard" is "ctrl+c" clipboard.

let's say you select some text on a firefox tab with the mouse. then you move to another program (let's say libreoffice calc) and press ctrl+c to copy the active cell.

if i run "xclip -selection primary -o" the firefox selected text will be printed, while the newest "copied" item was the calc cell... how would you get the same output without storage.xml? or am i missing something?

masavini (msavini) said : #6

just picked the post i wrote on linuxquestions to solve this issue BEFORE knowing diodon and its wonderful storage.xml:
http://www.linuxquestions.org/questions/programming-9/linux-clipboards-how-to-know-which-one-was-modified-last-in-bash-4175426767/#post4779485

in the last reply i posted a small script to obtain the same result without diodon, but the script was much worse than diodon... :)

Oliver Sauder (sao) said : #7

I see your point. Primary selection doesn't work in such a case such as LibreOffice calc cells. I will follow up with a wrapper script to access Diodon history per script.

Oliver Sauder (sao) said : #8

While writing a python script to access the clipboard history have I had to recognize that there is an issue in the python typelib binding library. I will fix this in a coming Diodon release so it is easier this script against Diodon again.

I for now created a small vala application though which returns last copied item accessing Diodon's clipboard history.

You can find the code here http://pastebin.com/gD6cxrmd

Save it to get_last_copied_item.vala and compile it with the following command:
valac --pkg gee-1.0 --pkg diodon get_last_copied_item.vala

Following needs to be installed on the machine where you compile it (binary can then be copied to other machines where those packages are not installed):
sudo apt-get install valac diodon-dev libgee-dev

When you run the application ./get_last_copied_item it will simply write the last item to the output.

As said this is only a workaround and I will add it on my feature list to add better bash scripting support to Diodon.

Hope this helps though.

masavini (msavini) said : #9

thank you so much for the code, i'll try it out immediately...
a silly question: why don't you just add a "-vv" option to diodon so that copied items are written to stdout (with a separator of your choice)? i'm not expert at all, but i don't think it should be that complicated...

then i can just run "diodon > clipboard.txt &" and get the last record...

Oliver Sauder (sao) said : #10

You can do that already with the following command:
G_MESSAGES_DEBUG=all diodon

This will print out all debug messages and also what has been copied (it shortens the content though when message are long as otherwise debug message will get huge).

In general is this approach quite messy to parse for the write message and not really meant for that. And also Diodon is not allowed to run before this command otherwise Diodon will close straight away.

I think the better approach would be as suggested in Bug #1309898 to add new command line arguments to Diodon like --get-recent-items etc. So in this case Diodon can simply run in the background and at any time it is possible to access the clipboard history with a command like:

diodon --get-recent-items 5

which will then return a text string {possible json) with the five last copied items. That is just an idea for the future though. Hope the script works for you for now though.

Can you help with this problem?

Provide an answer of your own, or ask Oliver Sauder for more information if necessary.

To post a message you must log in.