Save NSWindow Size on Resize & Close For User - objective-c

I've noticed that all applications on OS X seem to save the size you set it at. The next time you open it it's typically in the same position and size.
I'm making an app and I've noticed that after resizing, if I launch the application again it's just the size of what I've set in Xcode 4's IB and not the size that I resized it to on launch.
Do I have to manually save the window size each time its changed? Or is there an easier way to do this through IB? (My window does have a minimum size set if that changes anything.)

Apple makes it easy. In interface builder, for your window, just type in a unique name in the Autosave field and it will save it under that name in the global user defaults. E.G.

When you select your main window in the Interface Builder, there should be a property called Autosave in the attributes inspector. Put any name in there does the trick for me.

After setting the Autosave name in IB I could see that my window's frame was being saved to its preferences file, but still the auto restore would not work. Then I remembered to uncheck the Close windows when quitting an app checkbox in the System Preferences under the General settings, and the window position restored as expected.

If you did not use IB to create your window, you can set this property in Swift using:
setFrameAutosaveName(_ name: NSWindow.FrameAutosaveName)
(the frameAutosaveName property is get only)
For example:
windowController.window?.setFrameAutosaveName("appWindow")

Related

Getting window restored bounds

When you maximize a window in Macos, the window fills the screen.
What I want to be able to do is get the windows restored position and size, like when you press the maximize button again to restore the original position. How can you do this?
I need this for saving the window position on exit.
There is a method prepared for this task in NSWindow.
You could ask your ViewController for its NSWindow and set an AutosaveName for its Frame like..
[self.view.window setFrameAutosaveName:#"VerySpecialWindowAutoSaveName"];
which will end up in NSUserDefaults of your App as entry like...
"NSWindow Frame VerySpecialWindowAutoSaveName" = "300 100 1200 1005 0 0 2560 1289"
But the best place for this code is ... there is no best place because it depends on your apps approach.
The whole process can be challenging when you have multiple windows in a document based application but as you can set the AutosaveName per documents window you are able to recover the frame if needed, at least for the last document. Should be mentioned that you can set the AutosaveName in InterfaceBuilder and in Code as well - so keep an eye on it they follow the same name if you use both IB & code for one and the same window.
and an example in swift you can find in this gist

How can I save NSWindow programmatically?

Basically I got an array of allocated windows and want to save their state (position/size/subviews) when the app is closed. Is there any way to do this?
When I try to use a NSUserDefault i get this error message: Attempt to set a non-property-list. Which turns out to that you can't save NSWindows in a user default. Should i use this instead?
//customView
[self setFrameAutosaveName:[NSString stringWithFormat:#"%d",self.tag]];
It's not usually useful to save the positions and size of subviews in a window.
Using setFrameAutosaveName: will save the window's position and size for a given name. The next time your app opens a document and creates a window with that autosave name, its state should be restored.
As for the size and positions of subviews, they are usually automatically laid out based on the auto layout system, or using Struts and Springs. Either that, or their positions and sizes are derived from data in your document model. Either way, you don't generally need to save them yourself. Why do you think you should be saving them? What are you trying to accomplish by saving them?

NSPathControl and Auto Layout

I'm having some issues with NSPathControl and Auto Layout.
I want the path control to resize with the window. Which means, if I expand the width of the window, the NSPathControl should expand too. This works.
Now the problem is, that if I change the URL of the path control to a longer URL, the path control expands automatically. I don't want that. I want the path control width to stay the same, except when the user resizes the window.
I tried pinning the width it, but then I can't resize the window at all...
Also, when I don't pin the window of the path control, I can't make it smaller than the actual content of the sub controls, which represent the URL.
Hope that makes sense.
How can I fix this?
No need for subclassing. You can tell your view how much to resist compression. By default the priority is set to NSLayoutPriorityDefaultHigh. You are probably looking for NSLayoutPriorityDragThatCannotResizeWindow. You can set this in your XIB in the Size Inspector for the control or take a look at NSView's setContentCompressionResistancePriority:forOrientation:.
Solution
Sorry, I guess I asked to fast.
I got it working like this:
NSPathControl subclass
- (NSSize)intrinsicContentSize {
return NSMakeSize(NSViewNoInstrinsicMetric, NSViewNoInstrinsicMetric);
}

Programmatically change height of the window more than the height of the screen in Mac OS X

Respected low-level users of Mac OS, please, help.
I'm trying programmatically to change height of window of safari (or other window). I'm was trying used AppleScript and AXUIElementSetAttributeValue of Carbon, but none of these methods can't increase the window bigger than the height of the screen. But, the width changes without any problems.
I guess I'd be starting with something like SetWindowPos with SWP_NOSENDCHANGING flag under Win32.
Maybe, you can disable this functionality, which monitors the size of the window or completely shutdown the one who is responsible for it?
Note that I want to do this programmatically from an external process - I'm not asking how to control just my own app's window size and position.
Thanks.
It's not possible, search for "Note that any NSWindow with a title bar automatically constrains itself to the screen" in the Window Programming Guide.

Interface Builder won't allow drag/drop of text control to window

Noob dumb question, no doubt -- but here it is:
Trying to tutorialize myself with the Apple "Currency Converter" tutorial for Cocoa ( http://developer.apple.com/mac/library/documentation/Cocoa/Conceptual/ObjCTutorial/05View/05View.html)
Embarrassingly, I'm stuck very early on where the instructions ask me to drag-and-drop a text field onto the Currency Converter window. When attempting to do so, I release the mouse and the text field runs animatedly back to the library palette, refusing to "stick" on the client area of the Window.
Clearly the window is readonly or lacks some state or prerequisite to accept the text field, but what?
Are you sure you're dragging a view object and not a cell? Double check that you're dragging NSTextField and not NSTextCell.
Be sure you're not grabbing a text CELL.
When you select the text field from the Library, check that the description is NSTextField
Have you added a view to the window? Because if there is no view then you won't be able to add any controls.
Drag a "View" object over to your window.