Setting NSTextView tabulators programmatically


One thing about the default tabs on the ruler of the NSTextView: when you pass “Show ruler”, you get a multiple-tabbed default ruler which is not always appropriated.

If you paste tabbed records (say value1 value2 value2), you get a text which zigzags, depending of the length of each value. This is a known problem: you have to delete, add, move your tabs yourself.

I read about NSParagraphStyle, NSTab, tried a few things but didn’t make it work: the ruler obstinately keeps its numerous default tabs.

I want a ruler with only two tabs (I could fix their position programmatically later), and these tab to apply by default to any text I could put into the text view (and the user can edit).

I have only plain text into the view, not RTF/RTFD (I’m not sure it makes things simpler, but.).

I’m sure it can be done, but I need your help. Thank you for any hints!

I answer my own question:

put a single tab at position 160, then activate the text to update the ruler.

Not precisely a one-liner, huh?

This is what I’m looking for but I don’t know how to convert this into ASOC. I currently have 1 text view that will need a single tab stop and I’ll be creating a second text view that will need 4 tab stops. Would you be willing to convert this to ASOC for me?

Thanks in advance,


Try something like this;

set parStyle to current application's NSMutableParagraphStyle's alloc()'s init()
parStyle's setTabStops_({})
set tabStop to current application's NSTextTab's alloc()'s initWithType_location_(current application's NSLeftTabStopType, 160)
parStyle's addTabStop_(tabStop)
set theAttrs to current application's NSMutableDictionary's dictionaryWithObject_forKey_(parStyle, current application's NSParagraphStyleAttributeName)
tell theTextView's textStorage() to addAttributes_range_(theAttrs, theTextView's textStorage()'s |length|())

Thanks Shane.