How do I selectively replace all the shaders on all the objects in my scene?

I want to replace all the shaders on all the objects in my game which have specular shaders already attached. I have the following script but this only seems to replce the first material in each object. How do I get it to replace ALL the materials of type Legacy Shaders/Lightmapped/Specular in an object??

note this code only uses selected objects too...ideally I want to auto select everyobject in the scene.

@MenuItem("MIHT/Mass Material Replace")
static function MassMaterialReplace() {
    Undo.RegisterSceneUndo("MassMaterialReplace");

    var materialOld = Shader.Find ("Legacy Shaders/Lightmapped/Specular");
    var materialNew =Shader.Find ("Legacy Shaders/Lightmapped/Diffuse");

    for (var obj : GameObject in Selection.gameObjects) 
    {
        if(obj.renderer.sharedMaterial.shader==materialOld)
        {
        obj.renderer.sharedMaterial.shader=materialNew;
        }
    }
}

P..S..this code is modified from a function on the wiki.

I suspect that RegisterSceneUndo won't work because the material shaders are saved outside of scene data. You might have to try building the array of all the different materials in the scene first, registering undo states on the material array, then swapping the shaders.

Okay this seems to do the job, but the undo doesnt work. Anyone know why not?

@MenuItem("MIHT/Mass Material Replace")
static function MassMaterialReplace() {
    Undo.RegisterSceneUndo("MassMaterialReplace");

    var materialOld = Shader.Find ("Legacy Shaders/Lightmapped/Specular");
    var materialNew =Shader.Find ("Legacy Shaders/Lightmapped/Diffuse");

    for (var obj : GameObject in Selection.gameObjects) 
    {
        var mats : Material[] = obj.renderer.sharedMaterials;

        for (var mat : Material in mats) 
        {
            if(mat.shader==materialOld)
            {
            mat.shader=materialNew;
            }
        }
    }
}