Skip to content

Instantly share code, notes, and snippets.

@0xdeadbeer
Created June 29, 2023 20:53
Show Gist options
  • Save 0xdeadbeer/a654a502cd7e9f583e8f558450280042 to your computer and use it in GitHub Desktop.
Save 0xdeadbeer/a654a502cd7e9f583e8f558450280042 to your computer and use it in GitHub Desktop.
Simple and efficient way to add vocabulary to Anki through AnkiConnect (using dmenu for input)
#!/usr/bin/sh
# CONSTANTS
deck_name="deck name"
card_type_name="card type name"
anki_server="http://localhost:8765/"
front_part=$(dmenu -p "Word: " < /dev/null)
front_part_escaped=$(printf '%s\n' "$front_part" | sed 's/"/\\"/g')
# if either of them is empty, exit
if [ -z "$front_part_escaped" ]; then
exit 0
fi
back_part=$(dmenu -p "Definition: " < /dev/null)
back_part_escaped=$(printf '%s\n' "$back_part" | sed 's/"/\\"/g')
# if either of them is empty, exit
if [ -z "$back_part_escaped" ]; then
exit 0
fi
curl $anki_server -X POST -H 'Content-Type: application/json' \
-d '{
"action": "addNote",
"version": 6,
"params": {
"note": {
"deckName": "'"$deck_name"'",
"modelName": "'"$card_type_name"'",
"fields": {
"Front": "'"$front_part_escaped"'",
"Back": "'"$back_part_escaped"'"
},
"options": {
"allowDuplicate": false
}
}
}
}'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment