How to apply a specific modifier on selected objects? I'm trying to achieve this:

I select all objects with the same type of modifier (Armature) and want to "click" Apply on all of them. I searched different scripts on here and badly put together this one:

import bpy
sel = bpy.context.selected_objects

for ob in sel:
    for i, mod in enumerate(ob.modifiers):
        if mod.type == 'ARMATURE':
            bpy.context.scene.objects.active = ob
                for x in range(0, i):
                    bpy.ops.object.modifier_apply(modifier=mod.name)


It will work for some of the selected objects then Blender will say "Python Script fail". It's obvious I don't know what I'm doing, but why is it failing after working at first? Thanks

EDIT:
I found something that does exactly what I was trying to do. A user from Blender Artists named VincentG created an addon called "Massive Editor" from here. I selected all objects, typed Armature in the custom field and all objects that had that specific modifier applied. The script may be old, but it worked great. Thanks Vincent!

For the sake of learning though, I'd still want to know what went wrong...

UPDATE:
While searching around I found this answer here and tried it out to see if it would work for me:

import bpy

sel = bpy.context.selected_objects
act = bpy.context.active_object

for obj in sel:
    if obj != act:
        bpy.context.scene.objects.active = obj #sets the obj accessible to bpy.ops
        bpy.ops.object.modifier_apply(apply_as='DATA', modifier="Mirror")

bpy.context.scene.objects.active = act


It almost does what I would like but every time it would only apply to all selected objects except the active one.
Blender 2.79

This should work:

import bpy

for ob in bpy.context.selected_objects:
    bpy.context.scene.objects.active = ob
    for mod in [m for m in ob.modifiers if m.type == 'ARMATURE']:
        bpy.ops.object.modifier_apply( modifier = mod.name )


You may have some warnings though (like via the UI).

Blender 2.8

Use view_layer instead of scene:

import bpy

for ob in bpy.context.selected_objects:
    bpy.context.view_layer.objects.active = ob
    for mod in [m for m in ob.modifiers if m.type == 'ARMATURE']:
        bpy.ops.object.modifier_apply( modifier = mod.name )