-
Notifications
You must be signed in to change notification settings - Fork 2
/
anki-latest-edit-field.sh
executable file
·90 lines (77 loc) · 2.12 KB
/
anki-latest-edit-field.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
#!/usr/bin/env bash
#
# Latest Anki card: edit specified field in a popup window
#
# Display a popup window with a form allowing to edit the text content of a specified field of an
# Anki note corresponding to the most recent card. The field name is read from the first
# positional argument to the script.
#
# Dependencies:
# jq, curl, yad, Anki Connect, libnotify
#
# Config
ANKI_CONNECT_ENDPOINT='localhost:8765'
WINDOW_WIDTH=300
WINDOW_HEIGHT=200
anki_cmd() {
local cmd='{ "action": "'"$1"'", "version": 6, "params": { '"$2"' } }'
curl "$ANKI_CONNECT_ENDPOINT" -s -X POST -d "$cmd"
}
notify() {
notify-send 'Anki note field edit' "$1"
}
main() {
local field_name="$1"
if [ ! "$field_name" ]; then
notify "Unspecified field name"
exit
fi
local last_note_id
last_note_id=$(anki_cmd 'findNotes' '"query": "added:2"' | jq '.result | max')
if [[ -z $last_note_id || -n ${last_note_id//[0-9]/} ]]; then
notify 'Could not get last note ID'
return
fi
local last_note_info
last_note_info=$(anki_cmd 'notesInfo' '"notes": ['"$last_note_id"']' | jq -r ".result[0]")
if [[ $last_note_info == "{}" ]]; then
notify 'Got empty last note info'
return
fi
local current_value
current_value=$(echo "$last_note_info" \
| jq -r ".fields[\"$field_name\"].value" \
| jq -Rr @html)
if [[ $current_value == "null" ]]; then
notify 'Field does not exist'
return
fi
local new_value
new_value=$(yad --title="Edit Anki Note" \
--width $WINDOW_WIDTH --height $WINDOW_HEIGHT --center \
--form \
--field="$field_name:TXT" --separator="" "$current_value")
# shellcheck disable=SC2181
if [ "$?" -ne 0 ]; then
# Aborted
exit
fi
if [ "$new_value" == "$current_value" ]; then
notify "Field unchanged"
exit
fi
local params
params=$(printf '%s' \
'"note": { '\
'"id": '"$last_note_id"', '\
'"fields": { "'"$field_name"'": "'"$new_value"'" }' \
'}')
local error
error=$(anki_cmd 'updateNoteFields' "$params" | jq '.error')
if [ "$error" = "null" ]; then
notify 'Updated field'
else
notify 'Failed to update field'
fi
}
main "$@"