Find window's X and Y coordinates?

General discussion about Custom Buttons, including feature requests.

Find window's X and Y coordinates?

Postby steve.marks59 » Mon Feb 26, 2018 2:44 pm

On my large laptop I use this code in some buttons
to resize and move the window to a position I desire.
Code: Select all
  window.resizeTo(1595,810);
  window.moveTo(0,50);

I need to change this code to make windows fit a
smaller laptops screen. How could I find a window's
new X and Y coordinates after resizing and moving
it to a desired position on my smaller laptop?
steve.marks59
 
Posts: 455
Joined: Fri Dec 23, 2011 11:23 pm
Location: Claymont, Delaware

Re: Find window's X and Y coordinates?

Postby morat » Mon Feb 26, 2018 4:11 pm

Try these:

Code: Select all
/*Code*/
// run twice to see the values change
var out = [];
out.push(`outerWidth: ${window.outerWidth}, outerHeight: ${window.outerHeight}`);
out.push(`screenX: ${window.screenX}, screenY: ${window.screenY}`);
alert(out.join("\n"));
window.resizeTo(800, 600);
window.moveTo(50, 50);

Code: Select all
/*Code*/
// center window on screen
var xOffset = screen.availWidth / 2 - window.outerWidth / 2;
var yOffset = screen.availHeight / 2 - window.outerHeight / 2;
xOffset = xOffset > 0 ? xOffset : 0;
yOffset = yOffset > 0 ? yOffset : 0;
window.moveTo(xOffset, yOffset);

Code: Select all
/*Code*/
// center library window on browser window
// ensure library window is fully onscreen
var browserWindow = Services.wm.getMostRecentWindow("navigator:browser");
var libraryWindow = Services.wm.getMostRecentWindow("Places:Organizer");
var parentWindow = browserWindow;
var childWindow = libraryWindow;
var xOffset = parentWindow.outerWidth / 2 - childWindow.outerWidth / 2;
var yOffset = parentWindow.outerHeight / 2 - childWindow.outerHeight / 2;
var newX = parentWindow.screenX + xOffset;
var newY = parentWindow.screenY + yOffset;
if (newX < screen.availLeft) newX = screen.availLeft + 20;
if ((newX + childWindow.outerWidth) > (screen.availLeft + screen.availWidth))
  newX = (screen.availLeft + screen.availWidth) - childWindow.outerWidth - 20;
if (newY < screen.availTop) newY = screen.availTop + 20;
if ((newY + childWindow.outerHeight) > (screen.availTop + screen.availHeight))
  newY = (screen.availTop + screen.availHeight) - childWindow.outerHeight - 20;
childWindow.moveTo(newX, newY);
childWindow.focus();
morat
 
Posts: 959
Joined: Wed Dec 28, 2011 2:38 pm

Re: Find window's X and Y coordinates?

Postby steve.marks59 » Tue Feb 27, 2018 8:56 am

THANKS Morat for your quick response.
I really appreciate your willingness to always help me.
What I was looking for was a quick way to determine
the coordinates needed to make windows resize and
move to where I wanted them. I ended up using a trial
and error method, changing the numbers until the
window was resized and moved as I desired.
Knowing you, I am sure the codes you provided would
have worked but it was easier for me just to use the
trial and error method than to figure out how to use
those codes.

This is what worked for my smaller laptop:
Code: Select all
  window.resizeTo(1360,685);
  window.moveTo(0,50);
steve.marks59
 
Posts: 455
Joined: Fri Dec 23, 2011 11:23 pm
Location: Claymont, Delaware


Return to General Discussion

cron