Friday, July 20, 2018

#1 2018-07-06 08:33:51 am

daBee
Member
Registered: 2010-02-09
Posts: 86

Window Dimensions

Hi folks.

Just wondering what is the most convenient/best way to get window dimensions to put into my scripts.  I often work with browser windows that I want to put on a specific monitor (I have 4 on my Mac Pro), with dimensions and location.  Here's an example:

Applescript:

tell application "Google Chrome"
   activate
   set myw1 to (make new window)
   set the URL of tab 1 of myw1 to "http://hq.local"
   set bounds of myw1 to {1, 1, 1140, 1600}
end tell

It's a bit of a task getting those dimensions (I don't always use {1,1} as a TL corner), then setting the proper width, etc., without trial and error. 

Is there a better way?  I'm sure someone has a tip.  Fully appreciated. 

Cheers


Filed under: window, dimensions

Offline

 

#2 2018-07-08 04:45:59 am

Nigel Garvey
Moderator
From:: Warwickshire, England
Registered: 2002-11-20
Posts: 4604

Re: Window Dimensions

If you're looking for the usable bounds of all your screens and are running Yosemite or later, the current wisdom would be to use something like this. It returns a list of records, each record containing an ID number and the usable bounds for a screen ("usable" meaning not impinging on the menu bar or Dock). I only have one screen per computer myself, so apologies if I've failed to allow for anything.

Applescript:

use AppleScript version "2.4" -- Yosemite (10.10) or later
use framework "Foundation"
use framework "AppKit"
use scripting additions

-- Get the usable bounds of all attached screens, avoiding the menu bar and Dock.
on getUsableScreenBounds()
   set |⌘| to current application
   
   set theResult to {}
   set theScreens to |⌘|'s class "NSScreen"'s screens()
   repeat with thisScreen in theScreens
       set screenID to ((thisScreen's deviceDescription())'s valueForKey:("NSScreenNumber")) as integer
       set wholeFrame to thisScreen's frame()
       set visibleFrame to thisScreen's visibleFrame()
       
       -- In High Sierra, the frame rects are returned as lists instead of records, but the functions below work with either.
       set x1 to (|⌘|'s NSMinX(visibleFrame)) as integer
       -- The y origin in NSScreen is at the bottom of the screen. It needs to be converted to an origin at the top for AS.
       set y1 to ((|⌘|'s NSMaxY(wholeFrame)) - (|⌘|'s NSMaxY(visibleFrame))) as integer
       set x2 to (|⌘|'s NSMaxX(visibleFrame)) as integer
       set y2 to (y1 + (|⌘|'s NSHeight(visibleFrame))) as integer
       set end of theResult to {screenID:screenID, usableBounds:{x1, y1, x2, y2}}
   end repeat
   
   return theResult
end getUsableScreenBounds

getUsableScreenBounds()
--> {{screenID:69981780, usableBounds:{0, 23, 2560, 1440}}}


NG

Offline

 

#3 2018-07-14 03:09:53 am

daBee
Member
Registered: 2010-02-09
Posts: 86

Re: Window Dimensions

Thanks for the post. 

Ya I was thinking something more pragmatic, like a third party app of some sort. 

Now I'm just trying to copy the "measure first Chrome window's bounds" to the clipboard.

Offline

 

Board footer

Powered by FluxBB

RSS (new topics) RSS (active topics)