FIXED IT!
The problem seems to be that Task Scheduler can't run anything that uses Control Panel items (don't ask me why!). It starts the CPL item OK, but then the CPL takes over and subsequent commands from TS aren't passed to it. If the CPL appears at all on the screen, it will be flashing and asking for attention.
Batch files called by Task Scheduler can't bypass that, because TS keeps running until the batch file exits.
Microsoft offer a free app called "Mayhem" which will do things Task Scheduler won't, such as run a program if the stock market does something. It has a raft of other toggles. One such is "if a folder contents change". Mayhem doesn't care about the Control Panel niceties that bother Task Scheduler.
So I have two little apps, one to switch from speaker to headphones, and one to go the other way (the script above).
Task scheduler runs an "audio-on" or an "audio-off" batch file, which toggles the name of a file in subdirectories called "ON" and "OFF", and TS then exits.
Mayhem sees the subdirectory contents change, and runs the appropriate script.
It's messy, but it works. I've not found a way to do it without Mayhem.
Last edited by Keef; 1st March 2013 at 23:53.