Tutorial: How to create smoothly resizing windows (TESTED)

Hey guys

My first Post in here is going to be be a little tutorial. If you find it useful, Feedback would be nice!

After i searched a long time and didnt find any good solution, i created my own (with a friend of mine) on how to smoothly resize windows. I’ll explain it in Steps so it should be easy to redo.

1.) Create new class

In X-Code go to “File > New File” and create a Objective-C Class under “Cocoa”.

Call it “resizemywindow” and make sure the box below (also create .h file) is checked. Then you’ll see two new files in your files list in Xcode.

resizemywindow.h
resizemywindow.m

2.) Fill with code

Open the .h file and replace the code under the comments (comments start with “//”) with this code:

#import <Cocoa/Cocoa.h>


@interface resizemywindow : NSObject {
}
+ (void)resizeWindow :(NSWindow *)window toHeight:(int)height toWidth:(int)width;
@end

Open the .m file and replace the code under the comments (comments start with “//”) with this code:

#import "resizemywindow.h"


@implementation resizemywindow
+ (void)resizeWindow :(NSWindow *)window toHeight :(int)height toWidth:(int)width;

{
NSRect windowFrame,newWindowFrame;
windowFrame = [NSWindow contentRectForFrameRect:[window frame]styleMask:[window styleMask]];
newWindowFrame = [NSWindow frameRectForContentRect:NSMakeRect(NSMinX(windowFrame),NSMaxY(windowFrame)-height,width,height) styleMask:[window styleMask]];

[window setFrame:newWindowFrame display:YES animate:YES];
}

@end

3.) Applescript Handler

in your applescript, add the following handler:

on resizewindow(mywindow, utilitywindow, windowwidth, windowheight)

	log "HANDLER: resizewindow(" & mywindow & "," & utilitywindow & "," & windowwidth & "x" & windowheight & ")"	

	if utilitywindow is true then
		set mymenueheight to 16
	else
		set mymenueheight to 22
	end if
	
	-- Get current window size
	set theSize to size of window mywindow
	
	-- change width only
	if windowwidth is false then
		set windowwidth to (item 1 of theSize)
	end if
	
	-- change height only
	if windowheight is false then
		set windowheight to (item 2 of theSize)
		set windowheight to windowheight - mymenueheight --menübalkenhöhe
	end if
	call method "resizeWindow:toHeight:toWidth:" of class "resizemywindow" with parameters {window mywindow, windowheight, windowwidth}
end resizewindow

4.) Call the handler

To call the handler, you need 4 Informations. The name of your window to be resized (lets call it mywindow here); is it a utiliy window (true) or not (false) and the final height and/or width. You need to separate if its a utility window (see “attributes” of your window in Interface Builder) or not, because the title bars have different heights.

this example would change only the height of the window to 400 (pixels), which is a utility window in this case


--resizewindow(mywindow, utilitywindow, windowwidth, windowheight)
resizewindow("mywindow", true, false, 400)

Just try out different values!

5.) Prepare window

You might have recognized until now that things on ur window move by resizing the window. If you want them to stay at their place, click each item (one after another) and go to “size” in Interface Builder. There you can see 4 lines going into an object. Click the one at the bottom so that it seems broken (kinda swirly thing). Do this with every item you want to stay in place.
If you have items in a box, you only need to do this for the box!

By the way: In the “size” tab of Interface Builder you can see the actual size of your window! There you can also change limitations (sometimes you can’t make a window smaller, change the min values there)

Have fun optimizing your apps!

It’d be easier if you just made a category of NSWindow or NSApplication and not a new object.