Thanks a lot, guys! This is what I was looking for. For the moment I will go with the following addition to my ending script:
G1 F1800 E-2 ; retract filament
This appears to be good enough for most applications. I will try the more sophisticated version next time I need to print PETG.