Caret handling in script

Is there a method to put the focus of a script-wise opened new edit tab right on the caret? What I already do is open a tab, move the caret/cursor several times to the right*, and set the point size. Since the string in the tab is very long, Glyphs’ default behaviour of centering the view gets in my way of focusing a certain position in that string. Any thoughts?

Is there another way to get the caret to a certain position in an edit tab? Index, glyph name, what ever?

And how can I set the tool to T (text) via code? I could find methods like setSelectedToolIndex_(), but this might not always be the best way if someone has plugins in the tool bar.


*)

for i in range(5):
	Glyphs.font.tabs[-1].graphicView().textStorage().moveRight_(True)

This is what I do here, .moveRight() doesn’t take an int, or at least doesn’t react differently with True, 1, or 5 as arguments.

moveRight: takes a sender ID as argument. You don’t need to set one, so you can just leave it at None.

Concerning this issue: that looks better. Thanks.

Concerning setting the caret: You do not need to resort to the Text Tool. You simply set the selectedRange of the current graphic view to an NSRange of your liking. :smile:

location = 2
length = 0
myRange = NSMakeRange( location, length )
Doc = Glyphs.currentDocument
GraphicView = Doc.windowController().activeEditViewController().graphicView()
GraphicView.setSelectedRange_( myRange )
1 Like

That’s nice.

And I reread your question 1 a few times now, but I still have no idea what you are trying to achieve. Or does accessing Graphic View directly solve your problems?

That is something I already thought about. Having no actual range, but only the position made me assume it could be too hacky. But it is a nice way.

Basically in the end I want to put the caret into the last line and at a certain position (which i could read out in the code), so the location won’t be so easily as defining it as 2 or 5 or so.

Well, just get the length of the display string, and calculate your position from that. I believe Yanone added a few convenience methods recently.

Oh, let me try to explain a bit better: Once I open a new tab with a very long string, Glyphs itself arranges the view to be somewhere in the middle of this long string. But I want to see the caret right on spot. Here is a sketch:

new Tab:

   *blablablabl    * = caret
   blablablabla
   blablablabla     <–– outside the screen
   blablablabla
··················
   blablablabla
   blablablabla     <–– inside the screen
   blablablabla
   blablablabla
··················
   blablablabla     <–– outside the screen
   blablablabla
   blablablabla
   blablablabla

What I want:

··················
   *blablablabl    * = caret
   blablablabla
   blablablabla     <–– inside the screen
   blablablabla
··················
   blablablabla
   blablablabla     <–– ouside the screen
   blablablabla
   blablablabla
   blablablabla
   blablablabla
   blablablabla
   blablablabla

:smile:

Oh yes, that is a legit way! Thanks.

Did my sketch help to illustrate my first question?

Yes. I’ll have a look later today.

I have a somewhat related question:

(in Text Mode) I is there is a way to access the next Glyph in a string, but instead of just moving the caret, to enter the editing mode (eg. double clicking).

I don’t understand what you mean.

I will explain later, since I am only second in queue [smile] and I feel Mark needs this urgently.

Any updates relating my question?

Fixed it. It now makes sure that the first glyph is always visible.

By default? Cannot see it happen in Version 2.2.2 (823). Also I need a way to have the Caret always visible (not necessarily the first glyph).

It is not in the shipping version yet.

Thank you, Georg!