I've attempted to address 2 issues with the MIDI script below:
(1) If you stop playback in the middle of sustained MIDI notes, then you may have to manually send controller reset and/or note off messages to silence everything. And I think I've read that with some old synths, you may need to do both.
(2) I've seen many posts about trying to get around having a MIDI feedback loop with external synths - i.e., where you don't want to turn local mode off on your external synths. So with the track for the external synth selected in the arrange window, you hear doubling of notes.
This script tests to see if the transport is in play mode or not, (i.e., stopped), via testing info.playing. When the transport stops, this script will send send MIDI note off on all MIDI channels via the builtin MIDI object - and then it also sends the MIDI controller message (64) for sustain with an off value (I chose 10, but you could choose a different value) - on a range of MIDI channels.
Your external synths may be set to receive/transmit program change on MIDI channel 16, and then patch send/receive on maybe channels 14, 15. So the loop "while (i < 13)" only iterates thru channels 1-12, but you can change that to be whatever range you like.
If you add this script to a MIDI track that plays back MIDI (on an external synth), then with all the trace statements enabled, you should see something like the output from the (attached) screenshot of the MIDI console. But I suggest you save and use a version of the script with all the "Trace" "cc.trace" "event.trace" statements commented out. I also found that with too many trace statements the MIDI script console cannot keep up with it, and you don't see all the output. (Hmm - may be a bug).
Since MIDI events are only being sent if info.playing is true, then with the transport stopped and the track for an external synth selected in the arrange window - then no MIDI events are transmitted and the MIDI feedback loop to external synths should be broken.
But as it turns out, info.playing is apparently also true during recording. Hence the MIDI loop will not be broken for an external synths track selected in the arrange window during recording.
...And Then With External Instrument Plugin...
With the use of the external instrument plugin to set the I/O to your external synths, you can specify no output for tracks you record on and conversely no input for playback only tracks in the arrange window. Then you have to drag MIDI from the "record" tracks to the "playback" tracks (again for external synths setups w/o local mode off). Along with this workaround, this script on playback only MIDI tracks should eliminate MIDI loops for external synths.
Or at least this is the best workaround I have for now; I hope something here helps folks out.
I've run this on Mac OS 10.10.5 (Yosemite) with Logic Pro 10.2.3.