
In the
previous article, we discussed klipper - a wonderful program for managing the clipboard in KDE. We learned how to extend its functionality using scripts and showed a couple of simple examples that can make life easier for users.
Today we will continue to write scripts and make klipper even more convenient.
')
A set of useful scripts for klipper
Add ppa repository
Users of Debian-based distributions have to add ppa repositories from time to time, for example, to update the version of your favorite TeX editor kile, which fixed some unpleasant bugs.
Of course, the times when it was necessary to edit
/etc/apt/sources.list
manually, and then contact the key server to sign the repository, are long gone, and now all this can be done with a single command
sudo apt-add-repository ppa:kile/stable
but there is no limit to human laziness! After all, after adding the repository, you need to update the list of packages and update the kile after that. At the same time, it is also necessary to open the terminal, and this, like the Chinese, is a thousand-way path, a finger movement to the F12 key!
Well, no, I'm definitely not capable of such exploits. In addition, since we still have to copy the address of the repository to the clipboard, then let him cope with it. I think it is fair.
Add a new action to klipper and call it “Manage apt reository”. Require that this action be performed automatically if the contents of the buffer matches the regular expression.
ppa\:[A-Za-z0-9\_\-]*\/[A-Za-z0-9\_\-]*
that is, it contains the name of the repository.
Next, add a command that will ignore the output. Give it the name "Add apt repository and update your system"
kdesudo --comment "Do you want to add repository?<br>$(echo -n %s | grep -o "ppa\:[A-Za-z0-9\_\-]*\/[A-Za-z0-9\_\-]*")" -c "apt-add-repository -y "$(echo -n %s | grep -o "ppa\:[A-Za-z0-9\_\-]*\/[A-Za-z0-9\_\-]*")"; apt-get update; muon-updater"
We use
kdesudo
to graphically request a password to execute a set of commands with superuser privileges. The
-c
takes a list of commands separated
;
or
&&
. We add a repository using
apt-add-repository
, update the system and call muon-updater to confirm the package updates (you may want to use kpackagekit on your system or even update without confirming
apt-get upgrade -y
).
Now you just need to select the text containing the name of the repository to see the drop-down menu.

enter password

and confirm the update packages

You can delete the repository in the same way using the
ppa-purge
command
ppa-purge
"Now playing" - share a link to the music that we listen
With plug-ins that add the name of the song you are listening to now to the IM status you won’t surprise anyone, but we will go a little further, and by clicking on the desired key combination we will share a link to the youtube page with the search results for the song you are listening to. We will act like this:
- We pick up through dbus using the mpris interface the metadata of the song you are listening to
- We form a link to the youtube page
- Write the resulting link using dbus to the clipboard
at the same time we inform the user about what is happening with the help of kdialog.
To work with dbus, we use a convenient qdbus program with an intuitive interface and handy hints. The general qdbus syntax is as follows.
qdbus [servicename] [path] [method] [args]
so, for example, receiving metadata for the song played in the cantata player
qdbus org.mpris.MediaPlayer2.cantata /org/mpris/MediaPlayer2 org.mpris.MediaPlayer2.Player.Metadata
and thanks to the mpris interface for any other player (which this interface supports), you only need to replace cantata with the name of the player.
Inserting content into klipper is just as easy with a single line.
qdbus org.kde.klipper /klipper org.kde.klipper.klipper.setClipboardContents "New bufer entry"
Now it remains to hang up the execution of the script on any key combination (
Alt + Y seems to me quite convenient). To do this, go to "System Settings-Keyboard Shortcuts and Strokes-Special Actions" and add a global key combination to run our script.

The finished text of the script can be found at the end of the article.
“What are you looking at? And I want it too! ”- share the video
How often does it happen that you are sitting comfortably in a chair, watching a new series of futurama, and then your brother, friend, girlfriend, guru, and spiritual mentor (you need to underline) writes to you and asks you to share a link. I have this happen quite often. And here the problem is not even that it is long and inconvenient, I often don’t even remember where I downloaded this wonderful creation of American cinema. So it was decided to write a small script that will allow without interrupting viewing - share this video with others.
We will use netcat-openbsd which is installed by default in all Kubuntu distributions. It is important that this method will work only if your external IP coincides with the internal one or you have the opportunity to forward the port if you sit behind the router (we will use port 3333, I configured the router so that it redirects all requests to this port to my laptop, on which I look futurama).
I'm used to the VLC video player, and therefore you need to first configure it to work via dbus. For this, I corrected
/usr/share/applications/vlc.desktop
in which the line beginning with Exec was modified in this way
Exec=/usr/bin/vlc --control dbus %U
After these manipulations, VLC starts using dbus.
Since I use the script on the laptop and this is what changes the external IP address (although almost everywhere I have the opportunity to forward port 3333 to my machine) in the
HOST
variable I will not explicitly specify the external IP address, but will automatically determine it using the request
HOST=`wget -qO - icanhazip.com`
The overall strategy of the script is
It remains only to hang up the execution of the script on any global key combination (I use
Alt + N ). Now, when you are asked to share the video, simply press the selected key combination and paste the received command using
Ctrl + V into the messaging client. On the receiving side, you only need to copy this command and execute it in the terminal ...
"Terminal? No, I have not! ”- on the receiving side
But why do we need to open a terminal if the command we need inevitably falls into the clipboard? Create a new action in klipper and call it NetCat. Require that the action be performed automatically if the contents of the buffer contain a substring matching the regular expression
nc -q 0 .* > \~\/\".*\"
Then we will add a new command, let's call it “Receive a file with netcat”. The output of the command will be ignored. The command itself will be nothing but the execution of the string corresponding to the regular expression and the subsequent notification of the user.
eval $(echo %s | grep -o "nc -q 0 .*\~\/\".*\""); kdialog --title "NetCat" --passivepopup "File transfer complete:<br>\"$(echo %s | grep -o "\~\/\".*\"")\"" 2
We use bash's
eval
function to execute a string with I / O redirection.
Now simply select the line containing the command to receive the file and select the appropriate item in the drop-down menu.

The received file will be saved to your home directory.
Well, that's all for today. Next time we will continue to develop the theme of scripts in KDE.
Links to scripts
now_playing.shnow_watching.shLook for beautiful knees, write scripts and stay with us!
PS: If you evaluate the article negatively - please argue why. It is important for me to know what I did wrong.