Try this.
// script ""
var w = wnd.find(0, "window name", "applicationname").Activate();
IFImage[] image = {
@"image:", //confirmation field y
@"image:", //popup button
};
int primaryImageCount = 0;
while (primaryImageCount < 6) {
var im = uiimage.wait(0, w, image);
if (im.ListIndex == 0) {
primaryImageCount++;
keys.send("y"); // Primary image found, send "y".
} else {
// Secondary image found, you can close the popup or perform any other action.
im.MouseClick();
}
//wait until the image disappears, else the scrit may find the same image instance again
print.it("waitNot start"); //debug
uiimage.waitNot(0, w, image[im.ListIndex]);
print.it("waitNot end"); //debug
}