JayJay
JayJay

Reputation: 1

TypeError: setCurrentWindow(): 1st arg can't be coerced to ij.gui.ImageWindow

Hi I am a very novice when it comes to scripting. I am trying to write a Jython script that will take an image that is not at the front, in imageJ, and bring it to the front. I have tried using the WindowManager but routinely run into a similar error.

TypeError: setCurrentWindow(): 1st arg can't be coerced to ij.gui.ImageWindow

or some other form of this error. It seems as though activating an image that is not at the front shouldn't be too difficult.

Here is the code I'm using:

from ij import IJ
from ij import WindowManager as WM

titles = WM.getIDList()

WM.setCurrentWindow(titles[:1]) 

Upvotes: 0

Views: 2709

Answers (1)

ctrueden
ctrueden

Reputation: 6982

The WindowManager.setCurrentWindow method takes an ImageWindow object, not an int image ID. But you can look up the ImageWindow for a given ID as follows:

WM.getImage(imageID).getWindow()

Here is a working version of your code:

from ij import IJ
from ij import WindowManager as WM

print("[BEFORE] Active image is: " + IJ.getImage().toString())

ids = WM.getIDList()
win = WM.getImage(ids[-1]).getWindow()
WM.setCurrentWindow(win)
win.toFront()

print("[AFTER] Active image is: " + IJ.getImage().toString())

Notes

  • I renamed your titles variable to ids because the method WindowManager.getIDList() returns a list of int image IDs, not a list of String image titles.
  • The WM.getImage(int imageID) method needs an int, not a list. So I used ids[-1], which is the last element of the ids list, not ids[:1], which is a subarray. Of course, you could pass whichever image ID you wish.
  • I added the call win.toFront(), which actually brings the window to the front. Calling WM.setCurrentWindow(win) is important in that it tells ImageJ that that image is now the active image... but it will not actually raise the window, which is what it seems like you want here.

Upvotes: 1

Related Questions