Center window on screen
Center window on screen
- Subject: Center window on screen
- From: David Crowe <email@hidden>
- Date: Thu, 17 Oct 2002 20:47:16 -0600
These utility scripts can give you the tools to center a window (for
well-behaved applications) on the screen. They obtain the bounds of
the window and use the "Jon's Commands.osax" scripting addition to
get the bounds of the monitor (note that this currently only works
for the first monitor of multiple monitors). You can center the
window by doing a little arithmetic. I'll post an example script that
uses these later.
- David Crowe
(* Paste the following into the Script Editor *)
on GetFrontmostWindowBounds()
set {wLeft, wTop, wRight, wBottom} to {0, 0, 0, 0}
tell application "Finder"
set theAppName to name of (every process whose
frontmost is true) as string
end tell
try
if theAppName = "Acrobat 5.0" then
-- Acrobat uses "PDF window"
tell application "Acrobat 5.0"
set {wLeft, wTop, wRight, wBottom} to
bounds of front PDF Window
end tell
else if theAppName starts with "FrameMaker" then
-- FrameMaker has coordinates rotated and
uses "document" and doesn't guarantee that document 1 is the front
window
tell application theAppName
set {wTop, wLeft, wBottom, wRight} to
bounds of front document
end tell
else
-- Most other documents use window 1 for front window
tell application theAppName
set {wLeft, wTop, wRight, wBottom} to
bounds of front window
end tell
end if
end try
return {theAppName:(theAppName as string), theLeft:wLeft,
theTop:wTop, theRight:wRight, theBottom:wBottom}
end GetFrontmostWindowBounds
on SetFrontmostWindowBounds(BoundsRecord)
if wLeft 0 or wRight 0 then -- Indication that 'bounds of
front window' succeeded, otherwise best to do nothing
set {minX, minY, MaxX, MaxY} to the bounds of item 1
of (screen list)
set minY to 75 -- Preserve top of screen for toolbar
set MaxX to MaxX - 50 -- Space for right-hand side dock
set theAppName to theAppName of BoundsRecord
set theLeft to theLeft of BoundsRecord
set theRight to theRight of BoundsRecord
set theTop to theTop of BoundsRecord
set theBottom to theBottom of BoundsRecord
-- Left right of right, or right left of left
if theLeft > theRight - 100 then
set theRight to theLeft + 100
end if
-- Top below bottom, or bottom above top
if theTop > theBottom - 100 then
set theBottom to theTop + 100
end if
-- Note: The above may result in off-screen windows,
which are fixed by the following
-- Too far left, adjust left and right margins
if theLeft < minX then
set theRight to theRight + (minX - theLeft)
set theLeft to minX
end if
-- Too far right
if theRight > MaxX then
set theLeft to theLeft - (theRight - MaxX)
set theRight to MaxX
end if
-- Too high
if theTop < minY then
set theBottom to theBottom + (minY - theTop)
set theTop to minY
end if
-- Too low
if theBottom > MaxY then
set theTop to theTop - (theBottom - MaxY)
set theBottom to MaxY
end if
(* This doesn't always work, e.g. if there is no open
window or the application doesn't support AppleScript
There isn't anything we can do about this, so we may as well
do nothing rather than create an annoying
error message. *)
try
if theAppName = "Acrobat 5.0" then
-- Acrobat uses "PDF window"
tell application "Acrobat 5.0"
set bounds of front PDF
Window to {theLeft, theTop, theRight, theBottom}
end tell
else if theAppName starts with "FrameMaker" then
-- FrameMaker has coordinates rotated
and uses "document" and doesn't guarantee that document 1 is the
front window
tell application theAppName
set bounds of front document
to {theTop, theLeft, theBottom, theRight}
end tell
else
-- Most other documents use window 1
for front window
tell application theAppName
set bounds of front window to
{theLeft, theTop, theRight, theBottom}
end tell
end if
end try
end if
end SetFrontmostWindowBounds
_______________________________________________
applescript-users mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/applescript-users
Do not post admin requests to the list. They will be ignored.