Resizing Window with Cocoa

Hi guys!

I want to resize a window smoothly. - Just as Apple does it in their Project Example “WindowResize”.

Here is the code they use:



What I now need is have the code above modified so that I can simply resize windows by a line like this:

{x_one, y_one, x_two, y_two,} would be the new bounds of the window.

Could any of you Cocoa geeks help me with this?

Thanx so far

Jacques, thank you very much!

I’ll test it as ASAP.

Hi Jacques!
I’m a bit confused with the bound values:
How do I have to modify your method so that the bounds I give to the method are really equal to those which the window will have after being resized?
At this time the bounds don’t equal each other.

The bounds I send to the method should be in this format and should reflect the resulting bounds:

I just tried out your code, but couldn’t get it quite to work.

When using it straight, the window would resize to all sorts of weird shapes. I eventually worked out that it was because I was sending it the sizes I wanted it to send up, and it was subtracting the position (bounds?) from the size. I removed that part of the code and it started resizing as I wanted. It’s resizing just as I’d like it to now, but for two things:

  1. It’s resizing from the top down, rather than the bottom up. This wouldn’t be a big deal, if it weren’t for 2…

  2. When the window resizes, it moves (well, appears to). Since the bottom left corner is static, the top part of the window changes position when it resizes. This means the toolbar buttons are constantly moving, etc. Now, I could work around this by calculating the needed changes in position, but that only works if the window is only ever going to be in one place. After I manually move the window, it’ll pop back to the predefined position as soon as it’s resized again. I thought about getting the position of the window prior to the resize, resizing the window and calculating the difference between where it was, the size of the window and where it needs to be to appear that the title bar hasn’t moved. This would work okay with a fixed screen size. Ther problem with this, it seems, is that it appears to be an absurdly complicated way of doing it.

  1. Get window position.
  2. Note the currently selected tab.
  3. Calculate the position of the top left corner based on the position of the bottom left corner and the size of the current window.
  4. Resize window.
  5. Calculate the position the position the bottom left corner needs to be, based on the window size, in order to ensure the top left corner is in the same position as before.
  6. Resize the window.

Steps 3 and 4 would be multiple steps themselves. I’m really hoping I’m either oveanalysing or don’t know about a simple and spiffy way to achieve the same result.

Well, I ended up writing the handler to deal with it anyway. This works out where the top left corner is, looks at where the bottom left was, how much the size of the window has changed, and where this means the bottom left needs to be now to keep the top left where it was.

It isn’t too pretty, but it does the job. “windowheight” is the new height we want to resize the window to. The window, for my uses, doesn’t change width, but it wouldn’t be much to include a width change too.

on resizewindow(windowheight)
	-- Get current window position
	set thePosition to position of window "window"
	-- Get current window size
	set theSize to size of window "window"
	-- Determine position of top left corner / bottom left corner + window height
	set bottompos to item 2 of thePosition
	set topPos to bottompos + (item 2 of theSize)
	set leftPos to item 1 of thePosition
	-- Calculate the difference between the old  window height and new window height
	set newheight to windowheight - (item 2 of theSize)
	-- Calculate previous bottom left +/- difference in windows 
	set newbottompos to (bottompos - newheight)
	-- Resize window to new size
	-- Resize the window to fit the contents
	set ThisWindow to window "window"
	call method "resize:withFrame:" of class "ResizeWind" with parameters {ThisWindow, {leftPos, newbottompos, 504, windowheight}}
end resizewindow

Hey I stumbled across this article, and this is exactly what I am looking for. I’ve managed to integrate the Obj-C code and AppleScript into my AppleScript studio project, and the function call works, but I’m not getting the expected functionality, the window resizes and also moves about a bit.

What I’m looking for is to resize a window so that it grows longer to accomodate more content.

This is so that an error message which may contain multiple rows of text, the number of which is unknown until after execution, can resize a window and reveal the extra text.

Any idea on how to do this relatively easily and quickly would be greatly appreciated!