Script a Boolean difference of 2 given objetcs

Hello . I am new with Blender (but not with Programming !).
I found a lot of ‘piece of code’ … but still looking for the way to do.

I set up 3 cubes (CubeA, CubeB and CubeC).
I would like to substract ‘CubeB’ to ‘CubeA’

The only way to do the job :

#======================================================> Delete All !
bpy.ops.object.select_all(action=‘SELECT’)
bpy.ops.object.delete(use_global=False, confirm=False)

bpy.ops.mesh.primitive_cube_add(size=2, enter_editmode=False, location=(0, 0, 0))
bpy.context.object.name = “CubeA”

bpy.ops.mesh.primitive_cube_add(size=2, enter_editmode=False, location=(0, 0, 0))
bpy.context.object.name = “CubeB”
bpy.ops.transform.translate(value=(1, 1, 1), orient_type=‘GLOBAL’, orient_matrix=((1, 0, 0), (0, 1, 0), (0, 0, 1)), orient_matrix_type=‘GLOBAL’, mirror=True, use_proportional_edit=False, proportional_edit_falloff=‘SMOOTH’, proportional_size=1, use_proportional_connected=False, use_proportional_projected=False, release_confirm=True)

bpy.ops.mesh.primitive_cube_add(size=2, enter_editmode=False, location=(0, 0, 0))
bpy.context.object.name = “CubeC”
bpy.ops.transform.translate(value=(-1, -1, -1), orient_type=‘GLOBAL’, orient_matrix=((1, 0, 0), (0, 1, 0), (0, 0, 1)), orient_matrix_type=‘GLOBAL’, mirror=True, use_proportional_edit=False, proportional_edit_falloff=‘SMOOTH’, proportional_size=1, use_proportional_connected=False, use_proportional_projected=False, release_confirm=True)

#bpy.ops.object.select_all(action=‘DESELECT’)

!!! Here is THE problem : reference to CubeC is given by its order ‘2’ (0 based)

obj = bpy.context.window.scene.objects[2] # 0 est CubeA, 1 est CubeB, 2 est CubeC
bpy.context.view_layer.objects.active = obj # ‘obj’ is the active object now

new_mod = obj.modifiers.new(type=‘BOOLEAN’, name=“boolean”)
new_mod.object = bpy.data.objects[“CubeA”]
bpy.ops.object.modifier_apply(apply_as=‘DATA’, modifier=new_mod.name)

#bpy.ops.object.select_all(action=‘DESELECT’)
bpy.data.objects[“CubeC”].select_set(True)
bpy.ops.transform.translate(value=(-3, 0, 0), orient_type=‘GLOBAL’, orient_matrix=((1, 0, 0), (0, 1, 0), (0, 0, 1)), orient_matrix_type=‘GLOBAL’, mirror=True, use_proportional_edit=False, proportional_edit_falloff=‘SMOOTH’, proportional_size=1, use_proportional_connected=False, use_proportional_projected=False, release_confirm=True)

The result is perfect … But setting ‘CubeC’ as the active object is unacceptable. :-((

I Google a lot to try finding a solution … bad day !
All ways pieces of code does not work (often due to coded with 2.7 or older version)

Hope my ‘bottle in the sea’ will bring me THE solution.

Regards
Pascal

Why by order? Why not by name like you’re referencing other objects? Why is “setting ‘CubeC’ as the active object unacceptable”?

OOOOOpppppps

Thank for so quick response
although

obj = bpy.context.window.scene.objects[2]

I try

obj = bpy.context.window.scene.objects[“CubeC”]

And works like a charm …

Too simple … to stupid for me !

Best ragards
Pascal