I tried to do it this way but was unable to obtain an undefined value. Instead AE always crashed and gave me an error.
text box -> can use both boxtextSize and sourceRectAtTime. (sourceRectAtTime -> wrong value)
unboxed text -> can only use sourceRectAtTime, otherwise will crash AE and not return undefined.
That's why I used a try catch.
If you know way that works to check if the text object has the boxTextSize property without crashing Ae and without using a try/catch, I'll sure use it 
For some reason, the script I pasted earlier is missing the first line and I can't edit it, so there it is again:
function centerAnchorPoint( layer ){
var comp = layer.containingComp;
var curTime = comp.time;
var layerAnchor = layer.anchorPoint.value;
var x;
var y;
try{
x = layer.sourceText.value.boxTextSize[0]/2;
y = layer.sourceText.value.boxTextSize[1]/2;
x += layer.sourceText.value.boxTextPos[0];
y += layer.sourceText.value.boxTextPos[1];
}catch(e){
x = layer.sourceRectAtTime(curTime, false).width/2;
y = layer.sourceRectAtTime(curTime, false).height/2;
x += layer.sourceRectAtTime(curTime, false).left;
y += layer.sourceRectAtTime(curTime, false).top;
}
var xAdd = (x-layerAnchor[0]) * (layer.scale.value[0]/100);
var yAdd = (y-layerAnchor[1]) * (layer.scale.value[1]/100);
layer.anchorPoint.setValue([ x, y ]);
var layerPosition = layer.position.value ;
layer.position.setValue([ layerPosition[0] + xAdd, layerPosition[1] + yAdd, layerPosition[2] ]);
};