Automating PDF Text Translation on Linux

A practical solution is to replace all line breaks within a copied paragraph with spaces. This transforms the fragmented text into a coherent block suitable for translation. On Linux systems, the X11 windowing system provides the clipboard functionality, and the command-line utility xclip allows for programmatic access to it.

First, install xclip from your distribution's package manager:

apt install xclip

The following script, written in Common Lisp, automates the process of reading the clipboard, joining lines with spaces, and writing the result back. It includes a toggle mechanism to enable or disable the processing.

(defun concatenate-with-space (string-list)
  "Concatenates a list of strings with a space separator."
  (if (null string-list)
      ""
      (let ((result (car string-list)))
        (dolist (s (cdr string-list))
          (setf result (concatenate 'string result " " s)))
        result)))

(defun is-active ()
  "Checks if the script should be processing the clipboard."
  (let ((config-path (merge-pathnames ".pdf-translate" (user-homedir-pathname))))
    (when (probe-file config-path)
      (string= (string-trim '(#\Space #\Newline) (read-file-into-string config-path)) "ON"))))

(defun read-clipboard ()
  "Reads the current clipboard content."
  (uiop:run-program "xclip -o" :output '(:string) :ignore-error-status t))

(defun write-clipboard (text)
  "Writes text to the clipboard."
  (uiop:run-program "xclip" :input (list text)))

(defun process-clipboard-data ()
  "Reads, processes, and writes back clipboard content if active."
  (when (is-active)
    (let ((clipboard-content (read-clipboard)))
      (when clipboard-content
        (let ((lines (loop for line = (read-line (make-string-input-stream clipboard-content) nil nil)
                          while line collect line)))
          (when (> (length lines) 1)
            (let ((joined-text (concatenate-with-space lines)))
              (write-clipboard joined-text))))))))

(defun main-function ()
  "Main execution loop."
  (format t "PDF translation script is running.~%")
  (loop
     (process-clipboard-data)
     (sleep 0.5)))

(handler-case
    (main-function)
  (error (e)
    (format t "An error occurred: ~a~%" e)
    (uiop:quit)))

This script checks for the existence of a configuration file, .pdf-translate, in the user's home directory. If the file contains the text "ON", the script will process clipboard content; otherwise, it will do nothing. A separate shell script can be used to toggle this setting.

#!/bin/bash

CONFIG_FILE="$HOME/.pdf-translate"

# Ensure the config file exists
touch "$CONFIG_FILE"

# Read the current status, stripping whitespace
current_status=$(cat "$CONFIG_FILE" | tr -d ' \t
')

# Toggle the status
if [ "$current_status" = "ON" ]; then
  echo "OFF" > "$CONFIG_FILE"
else
  echo "ON" > "$CONFIG_FILE"
fi

The typical workflow is: 1. Select a paragraph of text in the PDF with the mouse. 2. Execute the toggle script (e.g., via a keyboard shortcut). 3. Paste the processed text into a translation tool using the middle mouce button.

Tags: xclip Linux Common Lisp PDF translation

Posted on Tue, 13 Oct 2026 16:32:02 +0000 by richiec