I need to programmatically save the active document of an NSDocument based app from within a method of the NSViewController that controls the document view. Menu items do this by sending save() to the first responder. What is the best way to do this programmatically? Should I A) get a reference to the NSDocument (somehow) and then call the save method or B) send a save: message to the first responder?
Asked
Active
Viewed 1,423 times
2
-
It depends on why and from where you want to save. – Willeke Nov 17 '16 at 15:32
-
From an NSViewController - I've updated the question with more details. – Mike T Nov 17 '16 at 16:36
-
Are you just trying to trigger an autosave or do want to create a new Version (you know, triggered when the _user_ uses the Save command)? – Bob Nov 17 '16 at 19:35
-
I want a new version - just like when the user selects File->Save from the menu. – Mike T Nov 18 '16 at 03:14
1 Answers
4
I'd say (B) is the easiest to do. All you have to do is call this line from any NSResponder
down the chain (like your view controller):
NSApp.sendAction(#selector(NSDocument.save(_:)), to: nil, from: self)
This will have exactly the same effect as choosing "Save" from the menu bar.

Bob
- 910
- 1
- 8
- 12