Friday, July 30, 2010

#1 2006-06-18 05:33:54 am

Grant Symon
Member
Registered: 2004-06-01
Posts: 20

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. sad

Many thanks for any info you can provide. 

Grant

Offline

 

#2 2006-06-18 08:17:29 am

Kim Hunter
Member
From: Australia
Registered: 2004-09-18
Posts: 214

Re: Get monitor resolution

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

Applescript:

do shell script "system_profiler SPDisplaysDataType | grep Resolution"

Offline

 

#3 2006-06-19 07:49:27 am

Grant Symon
Member
Registered: 2004-06-01
Posts: 20

Re: Get monitor 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. hmm

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.

Applescript:

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. smile

Grant


Filed under: Finder

Offline

 

#4 2006-06-19 09:38:52 am

Qwerty Denzel
Member
Registered: 2005-06-11
Posts: 337

Re: Get monitor resolution

This works slightly better in terms of layout for me.

Applescript:

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


Filed under: Finder

Offline

 

#5 2006-06-19 10:57:41 am

Grant Symon
Member
Registered: 2004-06-01
Posts: 20

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

Offline

 

#6 2006-06-19 05:24:37 pm

Qwerty Denzel
Member
Registered: 2005-06-11
Posts: 337

Re: Get monitor resolution

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

Applescript:

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

Last edited by Qwerty Denzel (2006-06-24 10:51:08 am)


Filed under: System

Offline

 

#7 2006-06-20 03:37:40 am

Grant Symon
Member
Registered: 2004-06-01
Posts: 20

Re: Get monitor resolution

Ha! 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. smile

Grant

Offline

 

#8 2006-06-20 08:31:16 am

Adam Bell
Administrator
From: Nova Scotia, Canada
Registered: 2005-10-04
Posts: 4250

Re: Get monitor resolution

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


Scripts are tested on a PowerMac dual-core G5/2.3 running OS X 10.5.8 or MacBook Pro Intel Core 2 Duo running OS X 10.6.4

Offline

 

#9 2006-06-20 09:19:33 am

Qwerty Denzel
Member
Registered: 2005-06-11
Posts: 337

Re: Get monitor resolution

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.

Last edited by Qwerty Denzel (2006-06-24 10:49:35 am)

Offline

 

#10 2006-06-20 10:52:01 am

Adam Bell
Administrator
From: Nova Scotia, Canada
Registered: 2005-10-04
Posts: 4250

Re: Get monitor resolution

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


Scripts are tested on a PowerMac dual-core G5/2.3 running OS X 10.5.8 or MacBook Pro Intel Core 2 Duo running OS X 10.6.4

Offline

 

#11 2006-06-21 01:28:47 am

Qwerty Denzel
Member
Registered: 2005-06-11
Posts: 337

Re: Get monitor resolution

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.

Last edited by Qwerty Denzel (2006-06-21 05:06:36 am)

Offline

 

#12 2006-06-21 03:30:03 am

Grant Symon
Member
Registered: 2004-06-01
Posts: 20

Re: Get monitor resolution

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

Offline

 

#13 2006-06-21 05:05:19 am

Qwerty Denzel
Member
Registered: 2005-06-11
Posts: 337

Re: Get monitor resolution

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.

Offline

 

#14 2006-06-21 05:15:58 am

Grant Symon
Member
Registered: 2004-06-01
Posts: 20

Re: Get monitor resolution

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

Grant

Offline

 

#15 2006-06-21 08:46:40 am

Adam Bell
Administrator
From: Nova Scotia, Canada
Registered: 2005-10-04
Posts: 4250

Re: Get monitor resolution

Qwerty Denzel wrote:

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.

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.


Scripts are tested on a PowerMac dual-core G5/2.3 running OS X 10.5.8 or MacBook Pro Intel Core 2 Duo running OS X 10.6.4

Offline

 

#16 2006-06-24 10:46:50 am

Qwerty Denzel
Member
Registered: 2005-06-11
Posts: 337

Re: Get monitor resolution

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

Offline

 

#17 2006-06-24 12:09:19 pm

Adam Bell
Administrator
From: Nova Scotia, Canada
Registered: 2005-10-04
Posts: 4250

Re: Get monitor resolution

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.


Scripts are tested on a PowerMac dual-core G5/2.3 running OS X 10.5.8 or MacBook Pro Intel Core 2 Duo running OS X 10.6.4

Offline

 

#18 2006-06-24 12:22:16 pm

Qwerty Denzel
Member
Registered: 2005-06-11
Posts: 337

Re: Get monitor resolution

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

Code:

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.

Offline

 

#19 2006-06-24 12:47:28 pm

Adam Bell
Administrator
From: Nova Scotia, Canada
Registered: 2005-10-04
Posts: 4250

Re: Get monitor resolution

Yup - that line is present.

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


Scripts are tested on a PowerMac dual-core G5/2.3 running OS X 10.5.8 or MacBook Pro Intel Core 2 Duo running OS X 10.6.4

Offline

 

Board footer

Powered by FluxBB

[ Generated in 0.364 seconds, 10 queries executed ]

RSS (new topics) RSS (active topics)