I'm developing a script in Gatan DigitalMicrograph (DM-Script) and need to determine whether a specific image is currently being displayed in the software UI.
So far, I've tried several approaches, but none seem to reliably indicate whether the image is actually open in the display window.
What I’ve tried:
Attempt 1: Using Image Object and IsOpen()
I created an image, retrieved its unique ID, and then checked IsOpen() on the allocated image object. However, it always returns 1 (true) as long as the image is assigned to a variable — even if it's not displayed. Only when I explicitly nullify the image variable does IsOpen() return 0.
Image img := RealImage("", 4, 256, 256)
Number n0, n1, n2, n3
img.ImageGetUniqueID().GetValues(n0, n1, n2, n3)
Object img_obj = Alloc(ImageID).init(n0, n1, n2, n3)
Result(img_obj.IsOpen()) // returns 1, even if not shown
img := ImageNullify(NULL)
Result(img_obj.IsOpen()) // returns 0
Attempt 2: Using Image.ImageIsValid()
Same behavior — the function returns 1 once the image exists, regardless of whether it's displayed or not.
Image img
Result(img.ImageIsValid()) // 0
img := RealImage("", 4, 256, 256)
Result(img.ImageIsValid()) // 1
Attempt 3: Using ImageGetImageDisplay(0)
This seemed promising, but when I try to get the image display, an error is immediately thrown if the image is not being displayed. Worse, this error cannot be caught using try {} blocks — the script halts.
Image img := RealImage("", 4, 256, 256)
ImageDisplay img_disp = img.ImageGetImageDisplay(0) // Error
What I’m looking for:
Is there any safe and reliable way to check whether an image is currently displayed in the UI, without triggering an uncatchable error?
Any guidance or workaround would be greatly appreciated.
The answer to this question hinges on exactly what is meant by the phrase "is the image displayed in the DM UI." There are various ways in which a valid Image object might not be visible within the UI. The most straightforward is the case of an Image that is created for utility purposes within a script and is never made part of an enclosing ImageDocument object. Alternatively, an Image might be part of an ImageDocument, but the latter may never have been displayed (again via script activity) and so does not appear in the DM Window menu. Finally, an Image might be part of a valid ImageDocument that appears in the DM Window menu, but its window might not be visible in the current workspace, either because it is in a different workspace, it has been hidden (via WindowHide()), or is obscured by other windows. The full solution depends on how many of these cases need to be covered.
The script below covers the cases where an Image object's enclosing ImageDocument object either does not exist, has never been shown, or has had its window hidden ...
Image target := RealImage("Test", 4, 512, 512);
ImageDocument targetDoc = target.ImageGetOrCreateImageDocument();
DocumentWindow targetWindow = targetDoc.ImageDocumentGetWindow();
Number isShown = 0;
if (targetWindow.WindowIsValid())
isShown = targetWindow.WindowIsShown();
if (isShown)
OkDialog("Image is shown in UI");
else
OkDialog("Image is not shown");
For cases where the image is shown in a different workspace or is obscured by other images, additional code would need to be added, either to determine whether its ImageDocument is in the current workspace or whether its DocumentWindow is frontmost in the current workspace.