omg! those IM commands are pure voodoo, but it almost worked.
it occurs to me that more info might help us get this nailed down. here is a progression of steps that i use to produce the final:
so A is the starting image. i add the pre-defined border graphic B. this produces a pre-C (no inner trim), though i have to erase by hand (in GIMP) any pixels of A that might stick out past the outer edge of B because B is gospel: i can't mess with its outer perimeter and don't want to mess with its inner perimeter.
then (still in GIMP) i add the trim with the idea that i don't want to make B any wider:
- select all non-transparent (via "Alpha to Selection")
- shrink the selection by 1 px
- add a 2px black border with feathering
the point being that shrinking the selection _then_ adding the border means you don't bloat out the image any, though you do eat some of your A pixels which i take as a reasonable sacrifice.
FTR, D is what i get when i run your IM command on the pre-trim C image, in other words A+B.