2

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?

Mike T
  • 1,163
  • 1
  • 11
  • 27

1 Answers1

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