Dunno if this is gonna help you but this is from a very old script I made a few years back, not sure if it still works as SRL may have changed, and I'm sure it wont compile since it uses other functions and variables from my code but it should give you an idea.
And by all means, the idea is all I'm trying to give you here, don't just copy the code because you won't learn and because of previously said reasons.
function CountDTMs(dtm:integer):integer;
var i, x, y : Integer;
item : TBox;
Begin
Result := 0;
gametab(tab_inv);
For i := 1 to 28 do
Begin
Item := InvBox(i);
If FindDTM(DTM, x, y, Item.x1-5, Item.y1-5, Item.x2+5, Item.y2+5) then
Result := Result + 1;
end;
end;
procedure CountFood;
begin
ntrout := CountDTMs(trout);
nsalmon := CountDTMs(salmon);
ntuna := CountDTMs(tuna);
nlobby := CountDTMs(lobby);
nswordie := CountDTMs(swordie);
end;
function eat():boolean;
var x,y:integer;
begin
result := true;
if ntrout <> 0 then
begin
gametab(tab_inv);
FindDTM(trout, x, y, 560, 210, 560+42*4, 210+36*7);
mouse(x,y,10,9,true);
ntrout := ntrout - 1;
wait(100+random(100));
exit;
end;
if nsalmon <> 0 then
begin
gametab(tab_inv);
FindDTM(salmon, x, y, 560, 210, 560+42*4, 210+36*7);
mouse(x,y,10,9,true);
nsalmon := nsalmon - 1;
wait(100+random(100));
exit;
end;
if ntuna <> 0 then
begin
gametab(tab_inv);
FindDTM(tuna, x, y, 560, 210, 560+42*4, 210+36*7);
mouse(x,y,10,9,true);
ntuna := ntuna - 1;
wait(100+random(100));
exit;
end;
if nlobby <> 0 then
begin
gametab(tab_inv);
FindDTM(lobby, x, y, 560, 210, 560+42*4, 210+36*7);
mouse(x,y,10,9,true);
nlobby := nlobby - 1;
wait(100+random(100));
exit;
end;
if nswordie <> 0 then
begin
gametab(tab_inv);
FindDTM(swordie, x, y, 560, 210, 560+42*4, 210+36*7);
mouse(x,y,10,9,true);
nswordie := nswordie - 1;
wait(100+random(100));
exit;
end;
result := false;
end;