
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:
read query
goldendict -s "$query"
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:
wl-paste --watch sh /path/to/script.sh
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:
STATEFILE="/path/to/statefile"
# If state file doesn't yet exist, create it.
if [ ! -f $STATEFILE ]; then
touch $STATEFILE
fi
# Fetch the content of the state file
STATUS=$(<$STATEFILE)
# If the content matches our conditions, then send the query
if [ "$STATUS" == "1" ]; then
goldendict -s "$query"
fi
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:
STATE_FILE_PATH = "/path/to/statefile"
print_status = false
new_status = ""
# Create state file if not yet exists
unless File.exist? STATE_FILE_PATH
File.new(STATE_FILE_PATH, "wx").close
end
# Load the initial status
status = File.read(STATE_FILE_PATH)
# Loop through each command line arguments and evaluate them
ARGV.each do |arg|
case arg
when "-0"
# Disable
new_status = "0"
when "-1"
# Enable
new_status = "1"
when "-t"
# Toggle
if status == "1"
new_status = "0"
else
new_status = "1"
end
when "-s"
print_status = true
end
end
# If the user wants to assign new states, open up the
# file and do so.
unless new_status.empty?
state_file = File.open(STATE_FILE_PATH, "w+")
state_file.write(new_status)
state_file.flush
state_file.close
end
# Read the new status
status = File.read(STATE_FILE_PATH)
if print_status
# Write your own conditions here
puts "" if status == "1"
puts "" unless status == "1"
else
if status == "1"
input = gets
unless input == nil # In case clipboard is empty
IO.popen("goldendict -s #{input}")
end
end
end
Some explanation for the arguments here:
-0
: Set state to0
(disable)-1
: Set state to1
(enable)-t
: Switch state between0
and1
-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:
...
"modules-right": [
...
"custom/goldendict",
"tray",
...
],
...
"custom/goldendict": {
"format": "{}",
"exec": "ruby /path/to/script -s",
"on-click": "ruby /path/to/script -t",
"interval": "once",
},
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