You should use the replaceCharacters(in:with:)
method of NSText
(whose subclass NSTextView
is).
@IBAction func clickBold(_ sender: NSButton) {
let range = content.selectedRange()
let selectedText = (content.string as NSString).substring(with: range)
content.replaceCharacters(in: range, with: "**\(selectedText)**")
}