Simple addon to avoid persistent tags when adding cards in window "Add"

When adding cards through the window “Add”, I don’t want tags to persist betwen cards. Other users have this same use case: see post on Jun 2021 and post on Jun 2023.

I inspected the source code of Anki and found out this line of code is responsible of the persistent tags in the window “Add”. I don’t want this behavior and there doesn’t seem to be a setting in “Preferences” to disable it, so I decided to write my own add-on: the add-on should remove all tags after a card has been added in the window “Add”.

I searched some information on the Internet and I could write the addon shown below which calls an arbitrary function after a card had been added in the window “Add”. I saved the script into the file ~/.local/share/Anki2/addons21/my-addon/__init__.py.

import aqt

def my_function(note):
  aqt.utils.tooltip("my_function called.")

aqt.gui_hooks.add_cards_did_add_note.append(my_function)

I need to somehow clear the tags within the function my_function, but I don’t know which functions to call to accomplish that behavior. Could anyone point me in the right direction?

I’m using Anki 24.06.2 in Ubuntu 22.04.4 LTS.

Check out the tag related functions for the note class here:

1 Like

@jhhr Thanks for the reply. I looked into those functions and now I’m aware that I can remove all tags of an instance of the anki.notes.Note class by setting note.tags to [] and then calling aqt.mw.col.update_note. Here’s a minimal working example: I saved the script shown below to ~/.local/share/Anki2/addons21/my-addon/__init__.py. The addon removes all tags of the card that was just added. This behavior is not what most users would want, but I just wrote it for educational purposes.

import aqt

def my_function(note):
  note.tags = []
  aqt.mw.col.update_note(note)

aqt.gui_hooks.add_cards_did_add_note.append(my_function)

However, modifying the argument note, which is provided through the hook add_cards_did_add_note, doesn’t seem to be of much help for the behavior that I want (clear the tags in the editor after a card has been added), because note is a reference to the newly created note. Here’s how I confirmed it: I wrote the addon shown below, it shows the __dir__ representation of the argument note after the note has been created through the Add window. When you add a card, you will see in the popup window that the object note has the same attributes of the newly created note.

import aqt
import json

def my_function(note):
  aqt.utils.showInfo(json.dumps([(key, str(value)) for key, value in note.__dict__.items()], indent=2, ensure_ascii=False))

aqt.gui_hooks.add_cards_did_add_note.append(my_function)

One way I could accomplish the behavior that I’m looking for is by providing add_cards_did_add_note with editor, because I could then set editor.note.tags to [] and then call editor.loadNote(), but currently add_cards_did_add_note only receives the argument note which is an instance of anki.notes.Note (link to relevant part in the source code).

If anyone else has a simpler way to accomplish the behavior mentioned in my first message, please let me know.

Unfortunately I don’t think there’s a simple way to accomplish this at the moment, as the logic is currently spread across the Python and JS layers, and this use case wasn’t anticipated when that hook was added.

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