How to override Edit>Copy when first responder cannot copy

I have a program where I need to handle Edit>Copy when the first responder cannot.

I've added the following to my AppDelegate:
Code Block
@IBAction @objc func copy(_ sender: Any?) {
    // My code here...
  }


But Edit>Copy is greyed out on the menu unless a TextField has focus and it has text selected.

How do I get my copy function called when the text field has focus but does not have text selected (or any other case where the current view with focus cannot handle copy)?
Answered by oreman in 662207022
I've raised a TSI for this and the conclusion was that this is not possible.

My work-around was to add a specific menu item with its own shortcut. It is not as elegant as I would have liked, but it works.
What do you intend to copy if selection is empty ? The whole field ?
Remember that it is important for user to have predictable behaviour: when you ask for copy, what is to be copied must be known and visible.

However, if you want Copy to be enabled, set it in IB Attributes Inspector for the menu item Copy: set Enabled On.
My app is displaying a default value as the user enters data in the TextField. It is clear that this data that is being displayed will be copied.

The problem does not seem to be related to Enabled since it is set in IB. I got it to work by removing the view that had focus (in my case a TextField). When I did this, Copy was enabled and my copy function in code above was called.

My problem still seems to be related to providing and telling the system that copy is available when the current first responder cannot handle it.

I've tried various things like creating my own NSTextField and trying to override copy but without success. I've also created a new SwiftUI app and posted this thread since it more clearly shows my issue.

ps. Sorry for the late reply. It seems my notifications are not working.
Accepted Answer
I've raised a TSI for this and the conclusion was that this is not possible.

My work-around was to add a specific menu item with its own shortcut. It is not as elegant as I would have liked, but it works.
How to override Edit>Copy when first responder cannot copy
 
 
Q