Get monitor resolution

Denzel,

I see you cleaned up my code a bit. I got most of it by recording the Finder, then trying to trim it back as much as possible. The rest was mostly guesswork from Filemaker scripting, which I have an idea about.

As for the window sizes. I have the dock on the right of all my Macs, and I find that it’s better to keep the window clear of the dock.

Grant

Have a look at this script Grant, and tell me what you think:
– Script Edited: Post #16

-- Get Dock size and position
tell application "System Events"
	tell first UI element of application process "Dock"
		set {dockWidth, dockHeight} to size
		set {dockX, dockY} to position
	end tell
	tell (get size of menu bar of application process "Finder") to set {screenWidth, menubarHeight} to beginning
end tell

-- Get main display's size
set screenHeight to word 4 of (do shell script "system_profiler SPDisplaysDataType | grep Resolution") as number

-- Get path to front application, works from script menu
set frontmostApp to path to frontmost application
set currentAppName to displayed name of (info for frontmostApp without size)

if currentAppName is "Finder" then set menubarHeight to menubarHeight + 22

if dockX is 0 then
	-- Left
	set displayBounds to {dockWidth, menubarHeight, screenWidth, screenHeight}
else if dockY + dockHeight is screenHeight then
	-- Bottom
	set displayBounds to {0, menubarHeight, screenWidth, screenHeight - dockHeight}
else if dockX + dockWidth is screenWidth then
	-- Right
	set displayBounds to {0, menubarHeight, screenWidth - dockWidth, screenHeight}
else if dockY is 22 then
	-- Top
	set displayBounds to {0, menubarHeight + dockHeight, screenWidth, screenHeight}
else if -dockX is dockWidth then
	-- Left Hidden
	set displayBounds to {4, menubarHeight, screenWidth, screenHeight}
else if dockY is screenHeight then
	-- Bottom Hidden
	set displayBounds to {0, menubarHeight, screenWidth, screenHeight - 4}
else if dockX is screenWidth then
	-- Right Hidden
	set displayBounds to {0, menubarHeight, screenWidth - 4, screenHeight}
else if dockY - 22 is -dockHeight then -- or 22 - dockY is dockHeight
	-- Top Hidden
	set displayBounds to {0, menubarHeight, screenWidth, screenHeight}
end if

-- Try and set front window to fill available screen area
try
	tell application (frontmostApp as Unicode text) to set bounds of window 1 to displayBounds
end try

Ha! :slight_smile:

Very neat Denzel.

I had already created a script for a single large window, but yours is much better. I might just ‘pinch’ some of it. :slight_smile:

Grant

Works with Safari too, if you change “front window” to “window 1”

Now should work for the resizing for the front window of the active application (if it supports applescript). Can be used in script menu. Note the different (vanilla) method for getting the screen size used to the previous version.

I’ll post it to code exchange when you’re happy with it.

Querty - The problem with your vanilla get bounds is that for dual screens it spreads the window over both. The Finder returns the full desktop as it sees it.

For a full exploration of dual screens see this topic in Code Exchange on exploring dual screens

Hi Adam,
What exactly does it return? I changed it mainly because the shell script was slowing the script down. It appeared as though running the script from the script menu extra was far slower than inside script editor. I didn’t want that added delay.

Denzel,

the ‘application’ one doesn’t work for me. I tried it with several apps. I did a ‘Save As’ for the script and saved it as an app.

Grant

Have you got the script menu enabled? If you have OS X 10.4 open the AppleScript Utility (in the same folder as Script Editor) and enable it, otherwise navigate to /System/Library/CoreServices/Menu Extras and open Script Menu.menu.
The problem is that if you save it as an application and run, it is then read as the front application in the script.

Yes I had the Script menu enabled. Changing the script from app to script, fixes it. Thanks!

Grant

Your get screen size (on my machine) returns: Height 870, Width 2176

I have two active screens:
Menubar on the LEFT screen, Dock at left top
Primary resolution: 1152 x 870 (active “face” 1130 x 840 - disk icons not displayed on desktop)
Secondary resolution: 1024 x 768
Top of Second 102 BELOW First. (The bottoms even within two pixels, should be 104 - close enough)
Left of Second: 1152 to RIGHT of First (ignoring the bezel gap, of course)

Your dock details are correct:
Mine is on the left at the top (under the menu bar)
Height 655
Width 41
DockX 0
DockY 23

Jon’s commands includes “screen list” which returns a list of records.

Okay, I’ve edited the script, how does it work now?

Keeps everything on the main screen except any open drawers (which poke into the right), but for Finder windows, it tucks the top of the title bar under the menu bar.

Doesn’t work in Eudora for some reason - it doesn’t listen.

Adam, are these metal or non-metal Finder windows? (or both)
Are you sure the script you are using contains the line:

if currentAppName is "Finder" then set menubarHeight to menubarHeight + 22

Removing that line I get the window-under-menubar behaviour.

I didn’t think about drawers since just about nothing I use today has them! Totally forgot.

Yup - that line is present.

As for Drawers, I first tried it from within Script Debugger which had a side drawer open.

Coming back to this after many years … :slight_smile:

If I’m using this only for Finder windows and I have a dual monitor setup, is there any way for the monitor resolution/size to be taken into account according to which of my two monitors, the Finder window is on at the time I run the script?

IOW, if the front-most Finder window is on my MacBook Pro’s screen, can it be made to resize for that resolution and conversely, if the front-most Finder window is on my external monitor’s screen, can it be made to resize for that?

Grant

Hello.

You can figure out where your finder window is, according to the screen coordinates that Finder gives by bounds of desktop window. You can read about it here

As for scaling, it is easy to figure out, once you have the monitor resolutions. If you need the hardware resolution, then you can go a couple of posts below, to the heading that contains iTunes Visualizer, there I have posted a full script regarding screen resolutions. Time is sparse, so you are on your own however.

If you’re running Mavericks or later, you can get the main screen – that is, the screen containing the window that is currently receiving keyboard events – like this:

use AppleScript version "2.3"
use scripting additions
use framework "AppKit" -- for NSScreen

on mainScreenSize()
	return (current application's NSScreen's mainScreen()'s visibleFrame()'s |size|()) as list
end mainScreenSize

If you want the screen with the menu bar, use:

use AppleScript version "2.3"
use scripting additions
use framework "AppKit" -- for NSScreen

on mainScreenSize()
	return (current application's NSScreen's screens()'s firstObject()'s visibleFrame()'s |size|()) as list
end mainScreenSize

In both cases, the first two values are the origin, with the Y axis running from bottom to top, and the last two are the width and height. They exclude the area used for the menu bar and Dock.

Thank you for the replies. :slight_smile:

Shane, should I be seeing the result as list when I run this in the Script Editor? (I’m not very sure about how I would implement it into my script, as it doesn’t appear to return anything). I’m running Yosemite GM4.

Actually, I made a mistake – they return just the size.

Try this:

use scripting additions
use framework "AppKit" -- for NSScreen

set {theX, theY, theWidth, theHeight} to (current application's NSScreen's mainScreen()'s visibleFrame()) as list