Thank you, it worked perfectly. Here is the final script.
Code:
targetComp = comp("as_claydog");
temoin = thisComp.layer("temoin");
curClosestIndex = null;
timeIndex = time;
fps = 1.0 / thisComp.frameDuration;
while (curClosestIndex == null && timeIndex > temoin.startTime)
{
curClosestIndex = evalPos(targetComp, temoin, timeIndex);
timeIndex = timeIndex - framesToTime(1, fps );
}
frames = curClosestIndex -1;
timeRemap = framesToTime(frames, fps );
function evalPos(targetComp, temoin, timeIndex)
{
for (var i = 1; i <= targetComp.numLayers; i++)
{
layerPos = targetComp.layer(i).position.valueAtTime(timeIndex);
layerWidth = targetComp.layer(i).width/2*(targetComp.layer(i).scale.valueAtTime(timeIndex)[0]/100);
layerHeight = targetComp.layer(i).height/2*(targetComp.layer(i).scale.valueAtTime(timeIndex)[1]/100);
minX = layerPos[0] - (layerWidth);
maxX = layerPos[0] + (layerWidth);
minY = layerPos[1] - (layerHeight);
maxY = layerPos[1] + (layerHeight);
curPos = temoin.position.valueAtTime(timeIndex);
if ( curPos[0] > minX && curPos[0] < maxX)
{
if ( curPos[1] > minY && curPos[1] < maxY)
{
return(i);
}
}
}
return(null);
}