I have a NSTextField where I would like to enter text at the insertion point. I have a code that don't generate errors:
let str = "Abc"
myTextField.insertText(str)
[edit] When, in the field there is 123|45 (the "|" represents the text insertion point), the output should be 123Abc|45.
Unfortunately don't work because it's deprecated. Here is the documentation. What is the replacement?
Please note that this question is for NSTextField not NSTextView, which has a question here: NSTextView's insertText method is deprecated in OS X v10.11. What is the replacement?
Get the field editor for the window that the text field is in, then use insertText(_:, replacementRange:)
on that:
let textfield = NSTextField()...
if let fieldEditor = textfield.window?.fieldEditor(false, for: textfield) as? NSTextView {
fieldEditor.insertText("abc", replacementRange: fieldEditor.selectedRange())
}
To clarify: this requires that the text field has input focus. If some other field has input focus the field editor will insert text there instead.