Get monitor resolution

Hi,

can anyone tell me if there is a way to ‘get’ the monitor resolution of the ‘main’ monitor?

I have a simple script which sets some windows, but I use primarily a 17" portable, which sometimes has a 20" monitor attached and sometimes it doesn’t. I would like my script to be a little smarter and to set my Finder windows according to whichever monitor I’m using.

I’ve no doubt this is very simple, but after trawling the web for about an hour, I’ve found nothing to help. :frowning:

Many thanks for any info you can provide.

Grant

I just did a search through the applescript mailing list and found this very simple one.

do shell script "system_profiler SPDisplaysDataType | grep Resolution"

Hi Kim,

tremendous! Thanks very much … though I feel a bit dumb that I couldn’t find anything myself via google. I wouldn’t have known what a shell script was though. :confused:

I used the ‘get’ command you provided, to build a few tiny scripts for setting Finder Windows. If like me you often copy between HDs using the Column view, you may find them useful. They should work on any monitor.

set monitorWidth to word 2 of (do shell script "system_profiler SPDisplaysDataType | grep Resolution")
set monitorHeight to word 4 of (do shell script "system_profiler SPDisplaysDataType | grep Resolution")

tell application "Finder"
	activate
	select window of desktop
	if not (exists Finder window 1) then
		make new Finder window
	else
		select Finder window 1
	end if
	select Finder window 1
	set bounds of Finder window 1 to {2, 45, monitorWidth - 50, monitorHeight * 0.49}
	if not (exists Finder window 2) then
		make new Finder window
	else
		select Finder window 2
	end if
	set bounds of Finder window 1 to {2, monitorHeight * 0.5, monitorWidth - 50, monitorHeight - 2}
	select Finder window 2
end tell

Thanks again. :slight_smile:

Grant

This works slightly better in terms of layout for me.

set displaySize to do shell script "system_profiler SPDisplaysDataType | grep Resolution"
set displayWidth to word 2 of displaySize
set displayHeight to word 4 of displaySize

tell application "Finder"
	activate
	if Finder window 1 exists then
		select window 1 -- may be minimized
	else
		make new Finder window
	end if
	set bounds of front window to {0, 44, displayWidth, displayHeight / 2 + 11}
	if Finder window 2 exists then
		select window 2
	else
		make new Finder window
	end if
	set bounds of front window to {0, displayHeight / 2 + 33, displayWidth, displayHeight}
end tell

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