
// Script function: Create a list of image IDs and use the images by their ID. In this case, 
//                  any new images will not be in the existing list. This is a safe way to manage and use all images.
// Requirement: Open at least one image

// Unless named starting with GlobalSino, EM, My, Self, the function commands are defined 
//by Gatan DM, please see at link: http://www.globalsino.com/EM/page2597.html



taggroup MyTagGroup() // create a list of image IDs
{
taggroup MyImgList = NewTagList() 
Image MycurrentImg 
MycurrentImg := GetFrontImage()
While(MycurrentImg.ImageIsValid()) 
{
MyImgList.TagGroupInsertTagAsLong(Infinity(),MycurrentImg.ImageGetID()) 
MycurrentImg := FindNextImage(MycurrentImg)
}
return MyImgList
}
number My_Size(taggroup MyImgList) 
{
return TagGroupCountTags(MyImgList) 
}
// MyGetID returns the ID of the image stored at list position "MyImgPosition". It returns 0 if the position is invalid
number MyGetID(taggroup MyImgList, number MyImgPosition) 
{
number MyID
IF (MyImgList.TagGroupGetIndexedTagAsLong(MyImgPosition,MyID)) 
{
result ("My image ID is: "+ MyID)
result ("\n\n")

return MyID
}
return 0
}
image MyGetImage(taggroup MyImgList, number MyImgPosition)
{
number MyID
image FoundImg
MyID = MyGetID(MyImgList,MyImgPosition)
If (MyID) 
{
IF (GetImageFromID(FoundImg,MyID)) return FoundImg
}
result("\n Image '"+MyImgPosition+"' is not found!\n")
return RealImage("Unknown image",4,2,2)
}

number MyImgSize, i
// List all displayed images
MyImgSize = My_Size(MyTagGroup())
// Count images from 0 to MyImgSize-1
for (i=0;i<MyImgSize;i++)
{
image EMimg := MyGetImage(MyTagGroup(),i)
EMimg.Setname("Found image '" + EMimg.Getname() + "'")
}