How to access clipboard history from an external application?

Asked by Oliver Sauder

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

English Edit question
Diodon Edit question
No assignee Edit question
Last query:
Last reply:
Revision history for this message
masavini (msavini) said :

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?


Revision history for this message
Oliver Sauder (sao) said :

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.

Revision history for this message
masavini (msavini) said :

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...

Revision history for this message
Oliver Sauder (sao) said :

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

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.

Revision history for this message
masavini (msavini) said :

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?

Revision history for this message
masavini (msavini) said :

just picked the post i wrote on linuxquestions to solve this issue BEFORE knowing diodon and its wonderful storage.xml:

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

Revision history for this message
Oliver Sauder (sao) said :

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.

Revision history for this message
Oliver Sauder (sao) said :

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

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.

Revision history for this message
masavini (msavini) said :

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...

Revision history for this message
Oliver Sauder (sao) said :

You can do that already with the following command:

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.