Setting window location dependent on screen size

Aditional Question.

I found this code from Wayne to Modify the window postion and size.

script = 
    "lw = WindowManager.getFrame('Log');\n"+
    "if (lw!=null) {\n"+
    "   lw.setLocation(10,50);\n"+
    "   lw.setSize(300, 600)\n"+
    "}\n";
  eval("script", script);

Is there a similiar procedure with percentage settings?
Right now the values are set for my screen.

Best

Tobias

You can use the macro variables screenWidth and screenHeight to calculate the new window position and size dependent on the screen dimensions.

If you use a full-fledged scripting language (instead of calling eval("script", ...) from an IJ1 macro), you can use the IJ utility class methods directly:

screen = IJ.getScreenSize();
2 Likes

Thank you Jan,

I would love to use phyton instead of the ijmacro language. But I just don’t know how. I really need to learn it!.

I did that but I’m not shure how to get the calculated demensions into my script.

h = screenHeight;
b = screenWidth;
print("some text");     
hpos = h/3*2;
 
script =
    "lw = WindowManager.getFrame('Log');\n"+
    "if (lw!=null) {\n"+
    "   lw.setLocation(0,hpos);\n"+                            
    "   lw.setSize(800, 310)\n"+                            
    "}\n";
  eval("script", script);

How do I get the variable hpos into the eval script?

Tobias

The script variable is just a String (i.e. text) variable that was concatenated from several strings enclosed in double-quotes (""). You can use the same string concatenation to place your hpos variable into the script code:

script =
    "lw = WindowManager.getFrame('Log');\n"+
    "if (lw!=null) {\n"+
    "   lw.setLocation(0," + hpos + ");\n"+                            
    "   lw.setSize(800, 310)\n"+                            
    "}\n";

But instead of using a macro to run the script, you can simple run the Javascript itself from the script editor (paste the code, select Language > JavaScript, and click Run):

importClass(Packages.ij.IJ);
importClass(Packages.ij.WindowManager);

hpos = IJ.getScreenSize().height * 2/3;
lw = WindowManager.getFrame('Log');
if (lw!=null) {
    lw.setLocation(0,hpos);
    lw.setSize(800, 310);
}

In Python, this looks very similar:

from ij import IJ, WindowManager

hpos = IJ.getScreenSize().height * 2/3
lw = WindowManager.getFrame('Log')
if lw:
    lw.setLocation(0,hpos)
    lw.setSize(800, 310)
3 Likes

Much thanks :slightly_smiling:

In my next hollidays, I’ll look into “real” scripting languages.

Best

Tobias