Thanks Carlos. You're right it works great on the sample file, the only problem is when there is no text inside a rectangle then all the texts will be shuffled (sometimes centered in a different rectangle than the one they were inside originally). It's already a big help, just a bit scary to run on a file with hundreds of rectangle/texts where I might miss an empy one!
"it seems to no work on the rectangles that have a rotation?"
That's because the script relys upon the bounding boxes, and the bounding boxes of the rotated rectangles overlap. Try this. It worked on a simple test doc, but it has not been rigorously tested. (I cannot open your file becuase of my Illustrator.)
var doc = app.activeDocument;
for (var i = 0; i < doc.pathItems.length; i++) {
var shapeItem = doc.pathItems[i];
for (var j = 0; j < doc.textFrames.length; j++) {
var textItem = doc.textFrames[j];
var centre = [
textItem.position[0] + textItem.width / 2,
textItem.position[1] - textItem.height / 2
];
if (pointIsInPoly(shapeItem.pathPoints, centre)) {
textItem.position = [
shapeItem.position[0] + shapeItem.width / 2 - textItem.width / 2,
shapeItem.position[1] - shapeItem.height / 2 + textItem.height / 2
];
}
}
}
// Jonas Raoni Soares Silva
// http://jsfromhell.com/math/is-point-in-poly
function pointIsInPoly(poly, pt){
for(var c = false, i = -1, l = poly.length, j = l - 1; ++i < l; j = i)
((poly[i].anchor[1] <= pt[1] && pt[1] < poly[j].anchor[1]) || (poly[j].anchor[1] <= pt[1] && pt[1] < poly[i].anchor[1]))
&& (pt[0] < (poly[j].anchor[0] - poly[i].anchor[0]) * (pt[1] - poly[i].anchor[1]) / (poly[j].anchor[1] - poly[i].anchor[1]) + poly[i].anchor[0])
&& (c = !c);
return c;
}