Scripts on the knee - taming klipper Part 2
In a previous article, we discussed klipper, a great clipboard program in KDE. We learned how to extend its functionality with 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 a 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, in which several unpleasant errors have been fixed.
Of course, the days when you had to
/etc/apt/sources.listmanually edit and then access the key server to sign the repository are long gone, and now all this can be done with one command
sudo apt-add-repository ppa:kile/stable
but there is no limit to human laziness! After all, after adding the repository, you still need to update the package lists and after that update kile. At the same time, you also have to open the terminal, and this, like the Chinese, has a thousand li path, the movement of a finger to the F12 key!
Well, no, I’m definitely not capable of such feats. In addition, since we still have to copy the repository address to the clipboard, then let him handle it himself. I think this is fair.
Add a new action to klipper and call it “Manage apt reository”. We require that this action be performed automatically if the contents of the buffer match the regular expression, that is, it contains the repository name. Next, add a command whose output we will ignore. Let's name it “Add apt repository and update your system”
kdesudo --comment "Do you want to add repository?
$(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"
kdesudoto graphically request a password to execute a set of commands with superuser privileges. The parameter
-caccepts a list of commands separated by
&&. We add the repository using
apt-add-repository, update the system and call muon-updater to confirm the update of packages (on your system, you may want to use kpackagekit or even update without confirmation
apt-get upgrade -y).
Now just select the text containing the name of the repository to see the drop-down menu,
enter the password
and confirm the update packages
You can delete the repository in the same way, using the command
“Now playing” - share a link to the music we listen to
The plugins that add the name of the song that you are listening to now in IM status will not 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 take through dbus using the mpris interface metadata of the listening song
- We form a link to the youtube page from them
- We write the received link using dbus to the clipboard
at the same time, we inform the user about what is happening with kdialog.
To work with dbus, we use the convenient qdbus program with an intuitive interface and convenient prompts. The general qdbus syntax is as follows
qdbus [servicename] [path] [method] [args]
for example, obtaining metadata for a 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 supports this interface) you just need to replace cantata with the name of the player.
Pasting content into klipper is as easy with one line
qdbus org.kde.klipper /klipper org.kde.klipper.klipper.setClipboardContents "New bufer entry"
Now it remains to hang the script execution on some key combination ( Alt + Y seems to me quite convenient). To do this, go to "System Settings-Key Combinations and Strokes-Special Actions" and add a global key combination to run our script. The
finished script can be found at the end of the article.
“What are you watching there? I want it too! ” - share video
How often does it happen that you sit comfortably in an armchair while watching a new series of futurama and here your brother, friend, girl, guru and spiritual mentor (underline as appropriate) writes and asks you to share the link. This happens to me quite often. And here the problem is not that it is long and inconvenient, often I don’t even remember where I downloaded this wonderful creation of American cinema from. So it was decided to write a small script that will allow you to keep up with the 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 only works if your external IP matches the internal one or you can forward the port if you are sitting at 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 at futurama).
I’m used to the VLC video player, and therefore you first need to configure it to work through dbus. For this, I corrected
/usr/share/applications/vlc.desktopin which the line beginning with Exec was modified in this way. After these manipulations, the VLC starts using dbus.
Exec=/usr/bin/vlc --control dbus %U
Since I use a script on a laptop and this changes the external IP address (although almost everywhere I can forward port 3333 to my machine),
HOSTI will not explicitly specify the external IP address in the variable , but I will determine it automatically using the request
HOST=`wget -qO - icanhazip.com`
The overall script strategy is as follows
- We take through dbus using the mpris interface metadata which contains the name of the file being played
- Convert the resulting name from the format URI to text
- Open netcat on port 3333 to transfer our video using the command
nc -l 3333 < /full/name/of/the/video/file.avi
- We write the command to receive the file on the receiving side to the clipboard using dbus
It remains only to hang the script execution on some global key combination (I use Alt + N ). Now when you are asked to share a video, simply press the selected key combination and paste the received command using Ctrl + V into the messaging client. On the receiving side, you just need to copy this command and execute it in the terminal ...
"Terminal? No, I haven’t heard! ” - on the receiving side
But why do we need to open a terminal if the command we need inevitably ends up in the clipboard? Create a new action in klipper and call it NetCat. We demand that the action be performed automatically if the contents of the buffer contain a substring corresponding to the regular expression. Then we add a new command, call it “Receive a file with netcat”. The output of the command will be ignored. The command itself will be nothing more than executing a string matching the regular expression and then notifying the user
nc -q 0 .* > \~\/\".*\"
eval $(echo %s | grep -o "nc -q 0 .*\~\/\".*\""); kdialog --title "NetCat" --passivepopup "File transfer complete:
\"$(echo %s | grep -o "\~\/\".*\"")\"" 2
We use the bash function
evalto execute a line with I / O redirection.
Now just 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 in the home directory.
Well, that's all for today. Next time we will continue to develop the scripting theme in KDE.
Links to scripts
Look 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.