The “Fade Ends” macro shows how to do that.
The reference documentation is here: Scripting Reference - Audacity Manual
It doesn’t go into much detail, so if there’s stuff you can’t work out, ask either here on this forum or on Audacity’s discord channel: Audacity