One point for sure where Williams was lucky was working with Spielberg who was a soundtrack geek himself. And he was, at least up to the early 90s was a director who left a lot of space in his movies for music. That means longer scenes with hardly dialogue or sound effects that leave room for music. Apart from Montage scenes (where today often directors rather use songs) there are usually not many scenes like that. But Spielberg provided that space.