Adding a modifier to the top of the stack of multiple objects without overwriting previous modifiers
Heres one, this works on all selected mesh objects:
import bpy

class AddTri(bpy.types.Operator):
    &quot;&quot;&quot;Add triangulate modifier on top of stack&quot;&quot;&quot;
    bl_idname = &quot;object.add_tri&quot;
    bl_label = &quot;Add Triangulate Modifier&quot;

    @classmethod
    def poll(cls, context):
        return context.active_object is not None

    def execute(self, context):
        act_obj = context.active_object #record active object
        
        for obj in context.selected_objects:
            if obj.type == 'MESH':
                obj.select_set(True)
                context.view_layer.objects.active = obj # make object active for bpy.ops to work on specific object
                
                mod = obj.modifiers
                md = mod.new(&quot;Triangulate&quot;, &quot;TRIANGULATE&quot;)
                # quad_method: 'BEAUTY' 'FIXED' 'FIXED_ALTERNATE' 'SHORTEST_DIAGONAL'
                md.quad_method = 'SHORTEST_DIAGONAL'
                # ngon_method: 'BEAUTY' 'CLIP'
                md.ngon_method = 'BEAUTY'
                md.min_vertices = 4
                md.keep_custom_normals = False
                
                # modifier UI settings
                md.show_expanded = False
                md.show_in_editmode = True
                md.show_on_cage = True

                bpy.ops.object.modifier_move_to_index(modifier=md.name, index=0) # bring modifier on top of stack
            
        context.view_layer.objects.active = act_obj # make old active object active again
        return {'FINISHED'}


def register():
    bpy.utils.register_class(AddTri)


def unregister():
    bpy.utils.unregister_class(AddTri)


if __name__ == &quot;__main__&quot;:
    register()

    # test call
    bpy.ops.object.add_tri()