I've always playeds that if threatened, and a caster makes their Cast Defensively check, that they can cast their spell (whatever it may be) without provoking any attacks of opportunity.
One group that I've been playing with uses the line of logic that if the spell is personal or a melee attack (Inflict Wounds), there's no AoO [edit] If the caster made the Cast Defensively check[/edit], but if the spell is a ranged attack
(Magic Missle, Melf's Acid Arrow, Fireball), the act of making the ranged attack provokes [edit] regardless of a Cast Defensively check[/edit].
(Now, admittedly, when the caster doesn't cast defensively, it shouldn't really matter, as they provoke for the spell, and the same action can only provoke 1 AoO.)
Similarly, activating a magic item that makes a ranged effect, in my experience, does not provoke (unless the item is a scroll). In this group, the activation does not, but the making the effect ranged does.
Opinions? I'd ove some examples, too.
(editted, to be clearer)