If you want to write efficient python scripts, you must try to find low-level API methods to do the same things as operators. At first it is a very good idea to check the info editor to actually see what operators are called, but it is very error-prone since all the operators execution depend on a particular context (in short, where your mouse is hovering at any given time).
If you want to do things with a script in Blender, you want to manipulate data, not operators, which are a level of abstraction upon the data.
This script will add and apply a boolean for each selected object, to the active one.
sel_objs = bpy.context.selected_objects
obj = bpy.context.active_object
# Make sure we have at least one selected object and one active object of mesh type :
if not sel_objs or not (obj and obj.type == 'MESH'):
print("Please select at least two objects and set one as active")
for sel_obj in sel_objs:
# Make sure the current object is a mesh object and not the active one :
if obj == sel_obj or sel_obj.type != 'MESH':
# Add a modifier to the object (context-insensitive - no operator needed)
new_mod = obj.modifiers.new(type='BOOLEAN', name="boolean")
new_mod.object = sel_obj
# Here we kind of have to use an operator since your object may have other modifiers
# That you don't wish to apply.
# We can't use the evaluated dependency graph :
# We change the display type so that the holes appear
# (The boolean objects will still be rendered though)
sel_obj.display_type = 'WIRE'
# You can also hide the object :
# sel_obj.hide_viewport = True
# sel_obj.hide_render = True
if __name__ == '__main__':