main
1{
2 den.aspects.desktop.app.script-tools.ocr = {
3 hjem = {pkgs, ...}: let
4 languages = "eng+chi_sim+chi_tra";
5
6 ix-ocr-script = pkgs.writeShellApplication {
7 name = "ix-ocr";
8 runtimeInputs = [
9 pkgs.coreutils
10 pkgs.grim
11 pkgs.libnotify
12 pkgs.slurp
13 pkgs.tesseract
14 pkgs.wl-clipboard-rs
15 ];
16 text = ''
17 geom="$(slurp -d)" || exit 0
18
19 img="$(mktemp --suffix=.png)"
20 trap 'rm -f "$img"' EXIT
21
22 grim -g "$geom" "$img" || {
23 notify-send -u critical "OCR" "Screenshot failed (grim)"
24 exit 1
25 }
26
27 text="$(tesseract "$img" stdout -l ${languages} --psm 6 2>/dev/null)" || {
28 notify-send -u critical "OCR" "Text recognition failed (tesseract)"
29 exit 1
30 }
31
32 if [ -z "$(printf '%s' "$text" | tr -d '[:space:]')" ]; then
33 notify-send "OCR" "No text recognized in the selection"
34 exit 0
35 fi
36
37 printf '%s' "$text" | wl-copy
38 printf '%s\n' "$text"
39 '';
40
41 meta.mainProgram = "ix-ocr";
42 };
43
44 ix-ocr-desktop-entry = pkgs.makeDesktopItem {
45 name = "ix-ocr";
46 exec = "ix-ocr";
47 icon = "scanner";
48 desktopName = "IX Tools - OCR";
49 comment = "Select a screen region and copy its text";
50 categories = ["Utility" "Graphics"];
51 keywords = ["ocr" "text" "scan"];
52 terminal = false;
53 startupNotify = false;
54 };
55
56 ix-ocr = pkgs.stdenv.mkDerivation rec {
57 name = "ix-ocr";
58 buildCommand = ''
59 mkdir -p $out/bin
60 install -Dm755 ${ix-ocr-script}/bin/${name} $out/bin/${name}
61 mkdir -p $out/share/applications
62 install -Dm644 ${ix-ocr-desktop-entry}/share/applications/${name}.desktop \
63 $out/share/applications/${name}.desktop
64 '';
65 dontBuild = true;
66 };
67 in {
68 packages = [ix-ocr];
69 };
70 };
71}