If the borders are solid then you can make an action that uses Image-Trim from the menu. Then you can batch that action on all the images that need correcting. A script is not really needed for a task like this.
I have been having a similar discussion on the Photoshop Windows forum about designing an Action to resolve my problem, but the suggested solution I have just received there (which was not as slick as your very simple Image/Trim.. method) failed for the same reason that your solution fails - it has become clear that what appears to be a black border on the two images I have tested so far is not in fact a solid colour. Grrrr.
That inevitably makes the problem much more complicated.
Yes, if the borders are not solid that is a problem. Script can have logic based on simple tests like is the image vertical or horizontal. I don't think a script could decide if the edge is a fussy border or part of the image itself. I would think that a script that tried to do so would miss some borders and remove some that where not really borders.