Resizing Windows... again

How can I resize a window without the content views and objects automatically resizing?

You need to set up the struts and springs in the Size panel of the Inspector. You’ll find all the details in Interface Builder User Guide under Xcode help.

I want it to resize things automatically, just not when I programatically resize the window.

You’ll probably have to programatically change the struts and springs for the duration of the resize.

That’s what I’ve bee looking for and can’t find.

It sounds like you want to resize the window based on user activity in that window that your classes (programmatically) don’t account for.

I’ve figured that out for a window which automatically resizes to contain text in a text field. I’ll show that below. If you want the window to resize for something besides text, it’ll just be a matter of finding the right class to respond to the user activity.

The class for responding to text as it is entered into a field (not after it is sent via hitting enter) is

on controlTextDidChange_(aTextField)

this handler will be run anytime the text in your text field changes (everytime you hit a key on your keyboard basically).

Just connect aTextField to the text field in your window in IB, and also set that text field’s delegate (in IB) to the same applescript class that your on controlTextDidChange_ handler is in.

Then it’s just a matter of doing some math based on the font size of your text to figure out how big to resize your text field.

I have a separate handler to do that. It accepts a string as input, checks the screen width of your monitor, then resizes the window based on some UI specific parameters I’ve set, like the maximum and minimum size of the window, and the general shape of the window/text field that I use.

on reeSaez(intxt)
		if scrnWdth is 1000 then
			set scrnWdth to (word 3 of (do shell script "defaults read /Library/Preferences/com.apple.windowserver | grep -w Width")) as number 
 -- get the width of your monitor
		end if
		set theFrame to aPanel's frame()  -- aPanel being the name of your window (I use a panel)
		set {{x:x1, y:y1}, {width:theWidth, height:theHeight}} to theFrame as list
		if intxt is "!reset!" then
			set dX to 404  --this is the minimum width of my window
			set dy to 126  --this is the minimum height of my window
		else
			set dX to 33
			repeat with i from 1 to (count of paragraphs of intxt)
				set thsPar to paragraph i of intxt
				if (count of characters of thsPar) > dX then
					set dX to (count of thsPar)
				end if
			end repeat

--calculates the width and height of my string in pixels based on Menlo font @12pt its hacky but works.
			set dX to (dX * 9) + 40   
			set dy to ((count of paragraphs of intxt) * 20) + 50
		end if
		if dy < 126 then
			set dy to 126
		else if dy > 575 then
			set dy to 575  -- this is the maximum height of my window
		end if
		if dX < 404 then
			set dX to 404
		else if dX > 900 then
			set dX to 900   -- this is the maximum width of my window
		end if
		set y2 to y1 + theHeight - dy
		if y2 < 0 then
			set y2 to 25
		end if
		set x2 to (x1 + dX)
		if x2 > scrnWdth then
			set x2 to (scrnWdth - 25)
		end if
		set x2 to x2 - dX
		aPanel's setFrame_display_animate_({{x2, y2}, {dX, dy}}, true, true)
		return ""
	end reeSaez
on controlTextDidChange_(aTextField)
set thisString to aTextField's stringValue()
set thisString to thisString as string   --thisString is now the string in your text field
set autoWin to my reeSaez(thisString)  --will call the reeSaez handler, sending it the string
end controlTextDidChange_

Ummmm, don’t think you get it, well anyway, I want to make a window bigger for when a disclosure button is clicked and unhides a view at the bottom of a window without the resize making a text view bigger (it has all struts and springs active), this could be achieved by disabling the bottom strut and vertical spring, but I don’t know how.

Try telling the window’s content view to setAutoresizesSubviews(false).

I tried that out before as default in IB to see what it does, but I’ll try programmatically anyway.

It seems to keep anything as though it has a bottom strut, you will see what I mean if you try.

Put it in its own subview.

I have 2 views in the content view, one for standard stuff inc. vertically autoresized text view, one for additional stuff. How do other apps handle disclosures?

If it’s a single view that you want to be fixed when disclosing, you can use setAutoresizingMask_() to modify its struts and springs temporarily. You could get the existing mask of it, change it to NSViewNotSizable, and return it to the original setting after making the change to the window size.

Can’t use 0 (not sizable) because it sticks to the bottom, but 8 (with max x) works well, I need some tinkering but it works! Thanks. Where have I seen that method before, it rung bells.

Got it working, somehow 55 is bottom strut on??? (binary- 111011) the 4th digit should be the min y or bottom border strut, I had been trying 63 (111111) but it didn’t stick, took 8 off 63 to get 55 and it was like having it on when by my understanding the 4th 0 (8) means it is off.

As of 10.6.2, you can use the enumerations themselves, like this:

setAutoresizingMask_((current application's NSViewMinXMargin as integer) ¬
	+ (current application's NSViewWidthSizable as integer) ¬
	+ (current application's NSViewMaxXMargin as integer) ¬
	+ (current application's NSViewHeightSizable as integer) ¬
	+ (current application's NSViewMaxYMargin as integer) ¬
	)

It’s more typing, but it’ll mean more when you look at it again in a few months’ time.