GoldenDict-ng is a dictionary program that I have been starting to use more often lately. It has a pretty cool feature that monitors your system clipboard that automatically pops up a window and looks up the copied text. Unfortunately, this feature is only available for X11 if you’re a Linux user.
Thankfully, we can port this feature fairly easily by utilizing existing programs that you may already have installed on your machine right now!
Basic approach
The wl-paste command from wl-clipboard has a --watch flag that allows you to run a
command with the copied text passed in as input. That alone already
makes things a lot easier. Now we just need a command that
transfers the copied text to GoldenDict. We can “create” a command by
writing a shell script. Here is a simple example:
|
|
This script simply reads the standard input (which will be our copied text) and send it to GoldenDict. Now to start clipboard monitoring, you just run:
|
|
This is working but it could be better. In case you don’t want to look up words on the dictionary anymore, you may want to be able to easily disable the script or enable it again. So it is a wise idea to implement some form of control over the script.
The simpliest approach to this is to use a file to save the script’ state. Basically, we’ll make the script to look into a file and decide whether or not the script should send the copied text to GoldenDict:
|
|
Integration with Waybar
Unless you’re a true minimalist, you should have a desktop panel available on your screen. I use Waybar as my panel and I’ll be using it to control the script because the panel doesn’t hide from you.
The idea is to add a button that toggles the script between two
states: enable or disable. By using the custom module, we can
easily do that, but not without some modifications to our script since
Waybar needs some output in order to print something.
I’m gonna be honest here, but I kinda suck at Bash and I don’t really want to bother myself crawling on Stack Overflow so I rewrote the script to Ruby instead:
|
|
Some explanation for the arguments here:
-0: Set state to0(disable)-1: Set state to1(enable)-t: Switch state between0and1-s: Prints an icon that indicates the state- nothing: Asks for input and send it to GoldenDict if state is
1
Now let’s create our Waybar module and assign it to somewhere on your panel. I put it to the left of my system tray:
|
|
Don’t forget the main part of the story: run wl-paste -w ruby /path/to/script to trigger the script on every clipboard changes (you
should run this on boot).
And your panel should now look something like this after a restart.
Also try clicking the icon to switch the state and copy some text:
I kept this very simple because this feature could be done by GoldenDict itself and a fix for Wayland could pop up at any time. Our next goal is to improve GoldenDict for Japanese text.
← Go to parent