Run console command when a new card is added

For my add-on I need to “catch” the event of a new card being added and have it trigger a function. However the basic code below does not work. There are no errors, but my debug print statement does not appear in the console when I add a new card. What am I doing wrong here?

from aqt import mw
from aqt import gui_hooks

def print_debug(note) -> None:
    print("New card added!") 

gui_hooks.add_cards_did_add_note.append(print_debug)
1 Like

I tried that code and it working fine.

After checking, it seems it does work when I manually add a card, but not when I add a card via Yomitan (through AnkiConnect). Not sure why.

1 Like

Maybe AnkiConnect is editing the card data directly and adding new cards, Anki functions are not used so gui_hooks are not called. You may need to detect the add cards by AnkiConnect or Yomitan in some other way or fork AnkiConnect. (I don’t know exactly)

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.