I'm working on a tower defense game and i had a similar problem needed to know which of the enemy / ai that walk the path is the 1st or last... so i used the bullet behavior, set the speed to random values based on type of enemy, so some enemies are faster some are slower, and at some point if my tower shooting sprite has a target already and a speedy AI passes the 1st current target becoming the 1st target, i need to select that now since is the 1st. Bullet behavior has a "distance traveled" parameter, which counts the pixels they move around. and i used that as my distance calculation to automatically target the first AI whenever gets in front of others. but for you not sure if this is a solution it all depends on how u make the AI move, are they moving grid based? etc
but a simple solution would be to calculate the left and right distance to the edge of the screen of the AI since that is what u need. to identify which is closest to left or right side ...
hope it helps.
to pick an enemy u have to run a for each loop
and compare the distance => distance(enemy.x,0,viewportleft,0)
which would calculate the distance of enemy ai to left side of screen or something like that. not sure if the viewportleft is correct code but u should see the proper suggestion in the event sheet if u type it in.