Hello,
maybe this script will be helpful (at least some its parts ) :
https://sites.google.com/site/dtpscripting/indesign-scripts/makerectan gle-jsx
There are options Size > Selection and Content > Text, so you can select the rectangle you need and use the script. Or you can take some functions or blocks of the one. Unfortunately, all the comments there are in Russian
Best wishes,
Sergey