Friday, November 16, 2018

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

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

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: 4720

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: 96

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

 

#4 2018-08-07 03:19:15 pm

t.spoon
Member
From:: BFE, Massachusetts
Registered: 2013-01-13
Posts: 358

Re: Window Dimensions

I'm confused about what exactly you're looking for, and I'm not clear if Nigel's answer helped. It sounded to me more like you were trying to get the current bounds of the Chrome Window?

Applescript:


tell application "System Events"
   tell application process "Google Chrome"
       set frontmost to true
       set currentWindow to window 1
       set windowPositon to the position of currentWindow
       set windowSize to the size of currentWindow
   end tell
end tell


Hackintosh built February, 2012 |  Mac OS Sierra
GIGABYTE GA-Z68X-UD3H-B3 | Core i5 2500k | 16 GB DDR3 | GIGABYTE Geforce 1050 TI 4GB
250 GB Samsung 850 EVO | 4 TB RAID
Dell Ultrasharp U3011 | Dell Ultrasharp 2007FPb

Offline

 

#5 2018-08-14 01:06:53 pm

seanmurp
Member
Registered: 2018-08-14
Posts: 4

Re: Window Dimensions

Here is a little script you could use for getting the bounds of all Chrome windows.  Set the windows up on screen as you want them positioned and it will output a dialog with the name of the window and its bound values.  You could then copy those out to your script.

Applescript:

set dimensionsList to {}
tell application "Google Chrome"
   set windowList to every window
   repeat with win in windowList
       tell application "System Events"
           tell application process "Google Chrome"
               set frontmost to true
               set tabName to name of win
               set windowPosition to the bounds of win
               set lastBound to item 4 of windowPosition as text
               set boundList to ""
               repeat with a in windowPosition
                   set thisBound to a as text
                   if thisBound is not equal to lastBound then
                       set boundList to boundList & thisBound & ", " as text
                   else
                       set boundList to boundList & thisBound as text
                   end if
               end repeat
               set end of dimensionsList to {tabName, boundList}
           end tell
       end tell
   end repeat
   
   set dialogText to ""
   repeat with b in dimensionsList
       set windowName to item 1 of b as text
       set windowBounds to item 2 of b as text
       set dialogText to dialogText & "Window Name: " & windowName & return & "Window Bounds: " & windowBounds & return & return
   end repeat
   
   display dialog dialogText buttons {"Cancel", "OK"} default button "OK"
end tell

If you don't need it in dialog format and just want to read the values returned then you can eliminate the portion from "set dialogText to""" down.

Offline

 

Board footer

Powered by FluxBB

RSS (new topics) RSS (active topics)