How to fill first field in a newly created note - with some string

Hi

I tried few solutions with ChatGPT, however it doesn’t work.

What add-on I would like to have? Right after choosing “Add” i would like to have some string in the first field, added there automatically. Also, during editing any note, if the first fild is empty, I want to put there that string. How to do that? What approche to have?

Thanks for any replies

I use Keyboard Maestro (https://www.keyboardmaestro.com) to add certain cards and fill fields. It is much more flexible than adapting an add-on.

on the edit card, you click on cards, in front model, type the string, put the field name {{Front}} below it and save

Have you tried Sticky Fields (built-in to Anki)? Adding/Editing - Anki Manual

1 Like

Hi Danika, I didn’t try, however thank you for telling me that, because I was not aware of this functionality. In my first post I forgot to write that I would need every time different string returned by my function.

I the meanwhile a preliminary solution has been found. Here it is:

from aqt import editor, gui_hooks
import random

def update_field(editor):
    #print('Hello world!')

    note = editor.note
    note_type = editor.note.note_type()

    if 'field_name' not in note:
        print('Field not found')
        return
    
    if note['field_name']:
        print(f'Field is not empty')
        return
    
    value_to_set = random.randint(0, 100)
    note['field_name'] = f'{value_to_set}'
    
    editor.loadNote()

gui_hooks.editor_did_load_note.append(update_field)

1 Like