Skip to content

Instantly share code, notes, and snippets.

@jchv
Created December 14, 2024 06:04
Show Gist options
  • Save jchv/0242f1ef4b8f659a0ffa433b9da03036 to your computer and use it in GitHub Desktop.
Save jchv/0242f1ef4b8f659a0ffa433b9da03036 to your computer and use it in GitHub Desktop.
A script I use to print labels using my Brother QL-800 label printer. Not currently integrated into my NixOS configuration, for one reason or another.
#! /usr/bin/env nix
#! nix shell nixpkgs#imagemagick nixpkgs#python3Packages.brother-ql nixpkgs#swayimg --command bash
PREVIEW=0
TEXTSIZE=85
while [[ $# -gt 0 ]]; do
case "$1" in
-p|--preview)
PREVIEW=1
shift
;;
-s|--text-size)
TEXTSIZE="$2"
shift 2
;;
*)
if [ ! -z "${CAPTION}" ]; then
>&2 echo "Too many arguments. Usage: $0 <caption>"
exit 1
fi
CAPTION="$1"
shift
;;
esac
done
if [ -z "${CAPTION}" ]; then
echo "Usage: $0 <caption>"
exit 1
fi
MAGICK_OPTIONS=(
-background white
-fill black
-pointsize "${TEXTSIZE}"
-font 'Noto-Serif-Bold'
-size 696x
-gravity Center
"caption:${CAPTION}"
-extent "696x%[fx:h<150?150:h]"
)
BROTHERQL_OPTIONS=(
--printer usb://0x04f9:0x209b
--model QL-800
print
--label 62
--threshold 50
)
if [ "${PREVIEW}" -eq 1 ]; then
PREVIEW_FILE="$(mktemp).png"
magick "${MAGICK_OPTIONS[@]}" "PNG:${PREVIEW_FILE}"
# Display image using swayimg. These terminal sequences will
# blank/restore the terminal while swayimg is running.
printf "\e[s\e[?1049h\e[2J\e[H"
swayimg "${PREVIEW_FILE}"
printf "\e[?1049l\e[u"
rm "${PREVIEW_FILE}"
read -p "Print the label? (y/n): " in
case $in in
[Yy]*) echo "OK, printing...";;
[Nn]*) echo "OK, aborting..."; exit 1;;
*) echo "Unknown response."; exit 1;;
esac
fi
magick "${MAGICK_OPTIONS[@]}" PNG:- | brother_ql "${BROTHERQL_OPTIONS[@]}" /dev/stdin
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment