I’m a layman Applescripter, mostly using a few short scripts triggered with a Quickeys keystroke to accomplish some single task. But one the truly hardest jobs I’ve tackled is writing a ‘universal’ script that centers a window.
How hard can it be? There’s the window: center it! I had no idea how difficult it is to do this, and I still haven’t done it.
Yes, there are a couple of good window centering scripts around, but they only work in about half of my applications: just the ones that understand the window ‘bounds’ concept. The rest, I think, will respond to positioning commands, but I’m having a lot of trouble with my current version of my script getting position to work.
The main problem with the ‘non-bounds’ apps is figuring out how wide the front window is. I figured I’d get the upper left corner co-ordinates by using 'position. And then I’d get the coordinates of the window’s grow area to determine the width. With those two numbers it should be straightforward to use existing script techniques to center it.
But, man, is it hard to get the grow area co-ordinates. And, once I finally figured that out, I was stymied in trying to use them with position.
Here’s my current version of the center windows script. It tries to use bounds concept first and if that produces an error, then it switches to the position concept. But I keep getting errors in that section that Applescript can’t get the position or can’t set the position. Arrgh!
Can we solve this? Can we help the world that’s waiting for a ‘Center Window’ command that just works without complaint?
tell application "Finder"
set screenSize to bounds of window of desktop
set screenWidth to item 3 of screenSize
end tell
tell application "System Events"
-- set myFrontMost to name of application "Safari" -- I turn this and the next stop off or on, for testing specific apps
set myFrontMost to name of first item of (processes whose frontmost is true)
tell process myFrontMost to set winName to name of (first window whose grow areas is not {})
end tell
try
tell application myFrontMost
set windowSize to bounds of winName
set windowXl to item 1 of windowSize
set windowYt to item 2 of windowSize
set windowXr to item 3 of windowSize
set windowYb to item 4 of windowSize
set windowWidth to windowXr - windowXl
set bounds of window 1 to {(screenWidth - windowWidth) / 2.0, windowYt, (screenWidth + windowWidth) / 2.0, windowYb}
end tell
-- on error errMsg number errorNumber -- This is for the final
on error the error_message number the error_number -- Testing: to ensure right error message triggers
display dialog "Error: " & the error_number & ". " & the error_message buttons {"OK"} default button 1
tell application "System Events"
tell application myFrontMost
-- get postion of winName -- why can't I get position?
tell winName
set windowSize to position of winName -- why can't I set position
set p to position of grow area 1
set windowXl to item 1 of windowSize
set windowYt to item 2 of windowSize
set windowXr to item 1 of p
set windowYb to item 1 of p
set windowWidth to windowXr - windowXl
set size of front window to {(screenWidth - windowWidth) / 2.0, windowYt, (screenWidth + windowWidth) / 2.0, windowYb}
end tell
end tell
end tell
end try
Model: G5
AppleScript: 2.0.1
Browser: Safari 533.16
Operating System: Mac OS X (10.5)