You are pretty close. Here is the code that you are looking for and I’ll break down what’s going on below:
let sketch = require('sketch')
var document = sketch.getSelectedDocument()
var text = document.selectedLayers.layers[0]
// "this is my string"
let mstextlayer = text.sketchObject
let baseFont = mstextlayer.font()
let boldFont = NSFontManager.sharedFontManager().convertFont_toHaveTrait(baseFont, NSBoldFontMask)
let boldRange = NSMakeRange(1,2)
let italicBoldFont = NSFontManager.sharedFontManager().convertFont_toHaveTrait(baseFont, NSItalicFontMask|NSBoldFontMask)
let italicBoldRange = NSMakeRange(3,4)
let underlineBoldRange = NSMakeRange(7,6)
mstextlayer.addAttribute_value_forRange(NSFontAttributeName, boldFont, boldRange)
mstextlayer.addAttribute_value_forRange(NSFontAttributeName, italicBoldFont, italicBoldRange)
mstextlayer.addAttribute_value_forRange(NSUnderlineStyleAttributeName, NSUnderlineStyleSingle, underlineBoldRange)
mstextlayer.addAttribute_value_forRange(NSFontAttributeName, boldFont, underlineBoldRange)
Gets you something like this:

Explanation
Like you deduced, we will want to get down to the native sketch object, MSTextLayer
(https://github.com/abynim/Sketch-Headers/blob/master/Headers/MSTextLayer.h). That object has a few useful methods:
- (void)addAttribute:(id)arg1 value:(id)arg2;
- (void)addAttributes:(id)arg1 forRange:(struct _NSRange)arg2;
- (void)setAttributes:(id)arg1 forRange:(struct _NSRange)arg2;
- (void)addAttribute:(id)arg1 value:(id)arg2 forRange:(struct _NSRange)arg3;
I’m not entirely sure how to use the first three* but I recognized the last method from NSMutableAttributedString
(https://developer.apple.com/documentation/foundation/nsmutableattributedstring/1417080-addattribute?language=objc) since I’ve used it in the past. What this lets you do is specify a font attribute name (a NSAttributedStringKey
), a value for that key, and a range.
To figure out the right key, value pairings you will want to look at the NSAttributedStringKey
documentation (https://developer.apple.com/documentation/foundation/nsattributedstringkey?language=objc)
The attribute NSFontAttributeName
takes a value type of NSFont
. To mutate your currently selected font into the bold and italic variants you can do that through NSFontManager
and its convertFont_toHaveTrait
method.
*Perhaps someone else can comment how to use the first 3.