Skip to content

Mining recipes

Piotr Grabowski edited this page Mar 22, 2023 · 17 revisions

Anacreon’s mpv script

Kamite works well with Anacreon’s mpv script used for automatically enhancing Anki cards with video-derived content. After creating a card, with the script installed, simply press Ctrl + C in the Kamite tab, followed by Ctrl + V in the mpv window. If you want the card context to span not one but several chunks, select them in Kamite’s history window before pressing Ctrl + C.

Troubleshooting “ERR: Line not found…”

Sometimes this error can be caused by a slight formatting correction that Kamite can apply to incoming text. However, every time Kamite makes such modifications, it also makes it possible to copy the original, unchanged text. In such cases, this is available under the “Copy original” buttons in the main and the chunk history action palettes or under the keyboard shortcut Ctrl + Alt + C.

The corrections on the side of Kamite can also be disabled by setting the config key chunk.correct to no.

If the error persists, make sure you are copying the entire chunk, or, if there are multiple of them, that they are consecutive in the video’s subtitles.

For Wayland users

Anacreon’s mpv script needs a slight modification to work on Wayland. In function get_clipboard(), replace

res = utils.subprocess({ args = {
  'xclip', '-selection', 'clipboard', '-out'
} })

with

res = utils.subprocess({ args = { 'wl-paste' } })

This requires wl-clipboard to be installed.

Screenshot script (Linux/wlroots and Xorg)

The contrib/anki-screenshot.sh script is convenient for enhancing mined cards with images. When executed, it uses an external program to take a screenshot of a screen area, scales it down to a configured size, converts it to the storage-efficient WebP format, and adds it to the latest Anki card.

The script also takes an optional parameter whose value is inserted into the latest card’s Sentence field. This can be used to automatically update the card with text present in Kamite. Below is a config excerpt illustrating how to achieve this.

commands {
  custom = [
    ${CUSTOM_COMMANDS.ankiScreenshot}
  ]
}

CUSTOM_COMMANDS {
  ankiScreenshot {
    symbol = ASS
    name = Anki screenshot
    command = ["/path/to/anki-screenshot.sh", "-s", "{effectiveText}"]
  }
}

This configuration will add a button labeled “ASS” to the command palette that will execute the Anki Screenshot script, supplying it with Kamite’s effectiveText (see Custom commands).

See near the top of the script file for the list of required dependencies and for the configuration options.

Screenshot area definition modes

Manual selection

By default, the script prompts for a screenshot area selection each time it is invoked.

Pre-defined geometry

With the -g option, a definition of the screenshot area rectangle can be supplied to the script. See near the top of the script file for details.

Capture window by name

Note On Wayland, the following option is only available for Sway. (Xorg is also supported.)

The screenshot area can also be determined automatically at each invocation, based on the provided name of the program window that is to be captured: The -w option takes a regular expression used to pick a window whose geometry determines the screenshot area. (See near the top of the script file for details of all the options.)

Trim black bars

For programs whose content does not fully adapt to their window size, capturing the entire window might result in capturing undesirable black bars. To deal with this, there is the option -r, which takes the aspect ratio of the actual content; the content is assumed to cover a rectangle of the specified proportions centered within the program window. When the option is present, the screenshot area is narrowed to just that content area.

The offset option

There is also the -o option, which narrows the window area using the specified offsets, allowing, e.g., for omitting menu bars from the screenshots.

Quick Anki field edit script (Linux)

Quick Anki field edit window

The contrib/anki-latest-edit-field.sh script will display a window allowing to edit the text in the specifield field of the most recently added Anki card. For example, the following invocation:

./anki-latest-edit-field.sh "Glossary"

will let you edit the field named Glossary.

Combined use example

Below is an example script that combines this script with the anki-screenshot.sh script, to be used directly after mining from a VN / game. The example script updates the Anki note picture with a screenshot, sets the Sentence field to the text provided as the first argument (the script is intended to be executed as a Kamite custom command with the first positional parameter set to {effectiveText}), and lastly, displays a window allowing to edit the Glossary field.

#!/usr/bin/env bash

SCREENSHOT_GEOMETRY="X,Y WxH" # !!!
EDIT_FIELD="Glossary"

if anki-screenshot.sh -g "$SCREENSHOT_GEOMETRY" -s "$1"; then
  sleep 0.5
  anki-latest-edit-field.sh "$EDIT_FIELD"
fi