Skip to content

Linux recipes

Piotr Grabowski edited this page Oct 1, 2023 · 13 revisions

For more, see Mining recipes and OCR recipes.

Cleanly launch/close a VN + Textractor + Kamite in a single terminal window

A template of a bash script to launch multiple programs that does not leave behind background processes when terminated with Ctrl + C.

#!/usr/bin/env bash

trap "kill 0" EXIT

export WINEPREFIX=/path/to/wineprefix
export WINEARCH=win32
export LANG="ja_JP.UTF-8"

TZ="JAPAN" \
  wine start /exec "/path/to/vn/VN.lnk" &
kamite --profile=vn --controlWindow=no &
wine "/path/to/textractor/Textractor.exe" &

wait

Credit: https://spin.atomicobject.com/2017/08/24/start-stop-bash-background-process/.

Watch clipboard, send text to Kamite, automatically look up with DeepL (wlroots)

#!/usr/bin/env bash

CMD_ENDPOINT="localhost:4110/cmd"

wl-paste --watch bash -c \
  "xargs -r -0 -I{} curl -i -X POST -d '{\"chunk\":\"{}\"}' $CMD_ENDPOINT/chunk/show;
   sleep 0.1;
   curl -i -X POST -d '{\"targetSymbol\":\"DEP\"}' $CMD_ENDPOINT/misc/lookup"

Read text from Kamite aloud using Microsoft’s TTS

The contrib/edge-tts-cached.sh script is a wrapper for the edge-tts Python module, which can send text to Microsoft’s freely-accessible TTS API and receive an audio file with the text spoken by a synthesized voice. The script then plays the audio using mpv.

The added value of the script over bare edge-tts is that it saves locally the most recent audio response with the correspoding request text, so that subsequent consecutive requests for the same string of text are served directly from the local audio file instead of being re-requested from the API.

Setup:

  1. Install mpv and edge-tts (e.g., pipx install edge-tts or paru -S python-edge-tts)

    Test the TTS service and playback:

    edge-playback --voice ja-JP-NanamiNeural --text "かみて"
  2. Download contrib/edge-tts-cached.sh and set it as executable.

  3. Set up a Kamite custom command that will run the wrapper script with Kamite’s effective text as an argument:

    In Kamite config:

    commands: {
      custom: [
        ${CUSTOM_COMMANDS.say}
      ]
    }
    
    CUSTOM_COMMANDS: {
      say: {
        symbol: SAY
        name: Say with TTS
        command: ["/path/to/edge-tts-cached.sh", "ja-JP-NanamiNeural", "{effectiveText}"]
      }
    }

With this setup, a button labelled SAY will appear in Kamite's command palette, that, upon click, will cause the current chunk to be read aloud (or a part of it, if text selection present; or multiple chunks combined, if there is a selection in the Chunk History tab).

Translate text with DeepL through DeepLX

This is a faster and cleaner alternative to embedding the DeepL website as a Kamite lookup target. It uses DeepLX, but it could easily be adapted to use another, similar tool, as long as this tool can be called from a Bash script.

  1. Download the contrib/kamite-deeplx.sh script (and the required dependencies—read the bottom of the script header) and set it as executable.

  2. Set up a Kamite custom command that will run the script with Kamite’s effective text as an argument:

    In Kamite config:

    commands: {
      custom: [
        ${CUSTOM_COMMANDS.deeplx}
      ]
    }
    
    CUSTOM_COMMANDS: {
      deeplx: {
        symbol: DEP
        name: DeepL lookup
        command: ["/path/to/kamite-deeplx.sh", "EN", "{effectiveText}"]
      }
    }
  3. Start DeepLX in the background.

With this setup, a button labelled DEP will appear in Kamite's command palette, that, upon click, will cause a DeepL translation of the current effective chunk to appear next to the current chunk.

Alternatively, the script can be made to run automatically each time a new chunk is added (see the kamite-deeplx.sh script header for instructions).

When explicitly requesting each translation, you might be interested in disabling the mechanism where the translation must be revealed by hovering it with the mouse. Currently, this can be achieved through styling customization.