centering a window and remembering position

how does one make the window that remembers position? also how do you center a window when it first opens, and not where you saved in in IB

To center a window…

tell window "theWindow"
	center
end tell

Place it in a handler that occurs before the window is visible, like ‘will open’ handler, so it centers every time it opens and so it doesn’t move AFTER the window appears. Also, make sure you connect it to the window’s event, not just add the code to the proj. :wink:

To save the current window location on close you could use a ‘will close’ window event to capture the current window bounds and save them to defaults…


on will close theObject
	if name of theObject is "theWindow" then
		set {pixLeft, pixBottom, pixRight, pixTop} to bounds of theObject
		set currentWindowBounds to {pixLeft, pixBottom, pixRight, pixTop} as list
		
		(* Write the defaults to file *)
		tell user defaults
			if default entry "windowBounds" exists then
				set contents of default entry "windowBounds" to currentWindowBounds
			else
				make new default entry at end of default entries with properties {name:"windowBounds", contents:currentWindowBounds}
			end if
		end tell
	end if
end will close

To get your bounds back and set them, use something like…


on will open theObject
	if name of theObject is "theWindow" then
		(* Option 1*)
		set savedWindowBounds to (contents of default entry named "windowBounds" of user defaults) as list
		set pixLeft to item 1 of savedWindowBounds
		set pixBottom to item 2 of savedWindowBounds
		set pixRight to item 3 of savedWindowBounds
		set pixTop to item 4 of savedWindowBounds
		
		(* Option 2 : Untested *)
		set {pixLeft, pixBottom, pixRight, pixTop} to ((contents of default entry named "windowBounds" of user defaults) as list)

		(* Set the window size : Use for both options above *))
		tell theObject
			set bounds to {pixLeft, pixBottom, pixRight, pixTop}
		end tell
	end if
end will open

I know the first option above works. I added option 2 because it’s much cleaner, but I didn’t test it.
j

To save your window position and size, just add an “Auto Save Name” in the Attributes panel of the Info palette in IB. Using this method you can also save the column widths of tables and other user adjustable settings without writing a single line of code (no need to save and then reload the settings).

Jon

Jon, I thought that was the case, but I’ve never used the ‘auto save name’ feature, so obviously didn’t want to say it actually worked. Thx for the EASY alternative… :smiley:
j

The auto save name appears to be thisclose to what I need.

But I’m using a floating panel and want it to remember its screen position. When I add an auto save name to the IB attributes, the panel does not come to the front until I double-click the application icon twice (that’s 4 clicks).

I 'spose I could go the long way round and save/restore the bounds, but I thought I’d check here first to see if anyone else has gotten auto save name to work with a floating panel.