Can you attack and then do Total Defence in the same turn? I would think that's the intended use but I want to be sure.
The total defense action says
You can defend yourself as a standard action. You get a +4 dodge bonus to your AC for 1 round. Your AC improves at the start of this action. You can't combine total defense with fighting defensively or with the benefit of the Combat Expertise feat. You can't make attacks of opportunity while using total defense.
This is in lieu of taking a different standard action, like a standard attack. You can instead fight defensively while taking either the standard attack action or the full attack action.
Fighting Defensively as a Standard Action: You can choose to fight defensively when attacking. If you do so, you take a –4 penalty on all attacks in a round to gain a +2 to AC until the start of your next turn.
Fighting Defensively as a Full-Round Action: You can choose to fight defensively when taking a full-attack action. If you do so, you take a –4 penalty on all attacks in a round to gain a +2 dodge bonus to AC until the start of your next turn.
...But Exceptions Exist
The volume of stuff available to Pathfinder characters means that if you want to do something, you probably can, although maybe not to the degree you'd prefer. For example, the feat Desperate Swing 1/day permits a halfling who's taking the total defense action to make an attack; the halfling suffers a -4 penalty on the attack roll.
The magus skill to do contact melee spell and weapon attack, in combination with rapid spell permit to cast and attacck with a swift action, so is possible to do, but the official rules don't say anything about. Don't say anything about the penalty if you cast a rapid spell and similar condition.