2
votes

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?

1
It depends on why and from where you want to save. - Willeke
From an NSViewController - I've updated the question with more details. - Mike T
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
I want a new version - just like when the user selects File->Save from the menu. - Mike T

1 Answers

3
votes

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.