I would like to be able to paste multiple lines of text into a control in a MacOS Cocoa application (written in Objective C) running on Catalina using XCode 12.3. I have added an NSScrollView control from the Library and created an outlet of type NSTextView. I can type multiple lines of text, with line breaks inserted with enter key, and retrieve the text in code via the string property of the outlet. However, if I click in the control and try to paste multiple line data copied to the clipboard using (AppleSymbol)C, no text is pasted. Is it possible to paste multiple line data into NSScrollView?
-
Do you want to paste into a `NSScrollView` or a `NSTextView`? Do you want to paste in the app or in IB/Xcode? Where did you copy the text? – Willeke Mar 23 '21 at 00:15
-
I would like the user to be able to paste into the control from the app. I don't know if they see the NSScrollView or the NSTextView. Text was copied from Safari and pastes OK into TextEdit. – SimonKravis Mar 23 '21 at 00:20
-
The `NSScrollView` scrolls, the `NSTextView` handles text. Is the insertion point blinking? Does the app have a menu with a Paste/Command-V item? – Willeke Mar 23 '21 at 00:26
-
The insertion point does blink but there is no Paste menu item. I notice that I cannot paste into an NSTextField control either. – SimonKravis Mar 23 '21 at 02:51
-
1Does this answer your question? [Cocoa Keyboard Shortcuts in Dialog without an Edit Menu](https://stackoverflow.com/questions/970707/cocoa-keyboard-shortcuts-in-dialog-without-an-edit-menu) – Willeke Mar 23 '21 at 08:33
-
Looks as though it does - will come to it in a few days. – SimonKravis Mar 24 '21 at 10:14
1 Answers
The problem is that copy/paste functions are not supported by default. However, there is a simple fix described in one of the responses from Alex pointed to by Willeke
Create a menu (e.g. "Edit") which contains your Cut / Copy / Paste menu items Add the KeyEquivalent for the CMD key to your "Edit" menu Add the KeyEquivalents to these menu items (CMD + X and so on) Link the FirstResponder's cut: copy: and paste: functions to your corresponding menu items.
The linking of the First Responder functions to the menu items is done by right-clicking on Placeholders->First Responder (shown in viewing the interface resource - eg MainMenu.xib). This shows a long list of functions, including the ones you have just added. Drag this to one side and then click on the Objects-> Main Menu-> and select Edit->Menu and then one of the menu items you have added. This will show the new menu as it appears in the application within the interface window. The linking is done by selecting + for the first responder function for cut, paste or copy and ctrl-dragging onto the corresponding interface menu item in the interface window. A blue line appears to link them.

- 553
- 1
- 3
- 24