Goal: Universal window centering script

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

	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)

Already done:

When asked, just set the viewing point to 50 (centered).

Thanks, Caleb531. It was one of the first centering scripts I tried when I began this quest. Unfortunately, when run on my system and my version of:

  • SimpleImage (5.3.3), it can’t get the bounds of the window 1, and occasionally it can’t get the text returned of application process “SimpleImage”

  • Quickeys (4.07), it can’t get the bounds of window 1, and occasionally it can’t get the text returned of application process “SimpleImage”

  • EasyFind (4,5), it can’t get the bounds of window 1

  • iTunes (9.1.1), the main window ‘disappears’ but the script doesn’t return it (although when I click on the iTunes icon on my Dock, the window reappears centered. In addition, if iTunes isn’t running – and this is weird – iTunes powers up whenever the script runs.

  • Firefox 3.6.3 - the same thing that happens to iTunes, but the window isn’t centered when I return Firefox to view.

There are others that give it problems, but these are the main issues.

Model: G5
AppleScript: 2.0.1
Browser: Safari 533.16
Operating System: Mac OS X (10.5)


it’s very difficult to consider all cases because

Some applications are scriptable, some are not.
In System Events some windows have bounds information, some have not
n System Events some bounds property is settable, some is not
In System Events sometimes window 1 is the main window of the application, sometimes it’s not.
In System Events sometimes the origin point of the main window is (0,0), sometimes it’s (22,0)

Actually you should make a list of applications which fulfill it’s special case


I have given this a little bit of thought, - pondering the “all applications” it struck to my mind then that you must
ensure that the different apps are perceived as scriptable. When script fails with certain applications, then you
must go into the applications bundle and check to see if there is a key named LSUIelement that has an integer value of 1. That may do the trick in many cases.

Best Regards


Hello Stefan!

If there exists such a list, I bet you know about it!

The finder and Itunes are both special cases, can’t remember if Safari also was a divergent one.
We should really have had a database for the apps and the window handling.

Greetings for sunny Southern Norway on its most beautiful!

Best Regards


I don’t have such a list. What is the benefit to center every window?
I prefer to use the screen space as effective as possible.

Lucky you, here in Switzerland it’s raining almost permanently for more than a week

Hello Stefan

Sorry for the weather. Well the list to tell you how to tackle the different windows might be good for other things than centering them. I’m working on some layout things my self. -Was working is more correct by now.
It divides the screen into eight and it should have enabled me to assign a window to one or more octants.

This kind of handling would be much easier if I had a database events table in the bottom of it, describing
how the different apps wants their window handling.

Best regards and hopes the weather will be better in Switzerland.


McUsr: I’m sorry, but it’s not clear to me how you feel this resolves the issue. What is that that I must do with the information? I understand that if the Info.plist of an app has a property LSUIElement set to 1, then it does not appear in the dock or app switcher. But that’s clearly not the case with the apps that fail under the “Window Auto-Place 1.1” script. They all appear normally.

Thank you, StefanK, that’s helpful to know, because it gives us a good start in figuring out work-arounds for each group of apps. But I’m just plain puzzled about the comment about why I (or others) would want a window centered. I would think there are lots of reasons but the point of the question escapes me.

To both: Thanks for responding, but so far the discussion is not helping to solve the problem of getting to a workable script, which, as the subject line of this topic says, is the goal of the thread. Obviously, if there are specific apps that don’t work, there should be an exceptions section to the script that gets them to work, or an error message that says, “This app’s windows can’t be centered.” but I don’t have anything close to the expertise to construct a portion of the script to do that. I was hoping readers of this forum could help me with that.

My script, amateur that it is, at least makes a stab at dealing with the many apps that ‘don’t do bounds.’ And as you can see, I’m having some trouble working around issues caused by very common apps. So, if they don’t do ‘bounds’ or ‘position’, how do we construct a third component, or fourth of fifth component of the script to deal with them?


Model: G5
AppleScript: 2.0.1
Browser: Safari 533.16
Operating System: Mac OS X (10.5)


I’m sorry about the LSUIElment -I have mixed a couple of properties. The property should have been NSAppleScriptEnabled and that property should have a value of true.

See this post.

Best Regards


You’re right. I just wrote as an answer to McUsr’s request that I personally don’t have such a list because
I personally don’t see the benefit to center every window.

@McUsr: A scripting-iilliterate app like FireFox doesn’t become scriptable even if you enable NSAppleScriptEnabled in info.plist


He he he I don’t bother much with FireFox personally atas Eclipse has a javascript debugger; any way (It is Chrome Omniweb and Safari which are important to me, in the opposite order). Chrome when there are getting to many exploits of Safari out public, Omniweb for “Structured offline browsing”, and Safari is a the browser of choice because its integration with Mac Os X

Firefox should be stuffed in the bags of impossible cases in such a register along with similar apps.

Preview I have learned, would go in the bag with possible cases.

It would be nice if there was a public register with the capabilities, I think Apple should provide such a list.
They would gain a lot for their end users, if they could provide some standard ways to tackle the different levels
of scriptability, at least providing a public list of the different apps, or have vendors document the level of scriptability they support along with direct capabilities about what works thru system events and what not.

The unification of window handling via scripting would gain Apple, as it would enhance the user experience grossly.

All in all I think that list could be good entertainment and valuable info.

Best Regards


the NSAppleScriptEnabled property in Preview.app is a relict from former versions, in which Preview had an AppleScript dictionary. I have no idea why Apple has dropped it

@Stefan: So, it is the only app where that trick will work?

I thought it would be possible to make that to work with other cocoa apps, as I have understood it, the cocoa framework provides a level of scripting capabilities by itself. You just have to manually gain access to it first.

Is that wrong?

Best Regards


I don’t know, normally the rule is: An application is basically scriptable, if it has a dictionary (a .sdef resource)

I have read that the standard dictionary is provided with an App that uses Cocoa as a default you just have to enable it.

I’ll find on and test it out one day, and post the results.

That is a cocoa app without any dictionary as default; I’ll enable the dictionary, and then what is in the standard suite should per se be usable. That would be a positive test.

Best Regards


This is actually the entire reason I got into AppleScript at all and now it has consumed a good portion of my life the past 6 months.

I spent probably a hundred hours trying to find an app that would handle Mac windows because they drove me nuts. Here is a script that I have slightly modified that I found a few years ago somewhere.

set display_y_size to 1200
set display_x_size to 1900

set curApp to (path to frontmost application as Unicode text)

tell application curApp
	tell front window
		set {x1, y1, x2, y2} to (get bounds)
		set winHeight to (y2 - y1)
		set winWidth to (x2 - x1)
		set center_y to ((display_y_size - winHeight) / 2)
		set center_x to ((display_x_size - winWidth) / 2)
		set y1 to center_y
		set x1 to center_x
		set y2 to (y1 + winHeight)
		set x2 to (x1 + winWidth)
		set bounds to {x1, y1, x2, y2}
	end tell
end tell

Change the numbers at the top of the script to your current resolution and then run the script via Quicksilver, QuickKeys etc.

I actually wrote a little utility that has several presets that works with QuickSilver to save the time and headache. It shows a few apps I have found that it works with in the one minute video I made.


I decided not to implement the centering of windows because I didn’t want to take the time and thought maybe in a later relase I might add that feature. I would be happy to send you a copy if you want to email me. I am not looking to be an advertisement in the slightest just something that I wanted to do as well.

I wondered that too because it use to work with Preview. I have found that with each new update with Firefox that the scripts either work or don’t work. With the latest version of Firefox (FF 3.6) it is currently working.

Another fantastic application is SizeUp that I have since found since I wrote my little utility.


I probably wouldn’t have taken the time otherwise because his works on pretty much every window and there is also an option to center the window and choose 4 window sizes for each corner so you can “use the screen space as effective as possible” :D. My app works with dual displays which the developer told me he probably will not do because of the way it was coded.

Cinch is also pretty cool but doesn’t center

Thanks StefanK and McUsr for the comments and thoughts.