How to select two objects add a boolean modifier and enter name for object to be unioned So currently when using the boolean modifier you have to set the type and then select the object manually.

I am curious how one could automate this like this:

select two objects
select Add Boolean Union button

the button will do this:
add boolean modifier to active object
set type to union
select other object as to be unioned to the active object

Ideally the object selection should be done via mouse selection.
First click is the tool object and last click is the active object to add the modifier to.
Modifiers are stored in an object's modifier list and a new one can be added by calling obj.modifers.new(name, type). To adjust the modifier settings you can either collect the created modifier object returned by the new method or access it through the list.

import bpy

target_obj = bpy.data.objects['Cube']
cutter_obj = bpy.data.objects['Sphere']

bool_mod = target_obj.modifiers.new('cutter', 'BOOLEAN')

bool_mod.object = cutter_obj
bool_mod.operation = 'DIFFERENCE'

# or directly -
bpy.data.objects['Cube'].modifiers[0].object = bpy.data.objects['Sphere']