On Fri, Jun 3, 2016 at 8:45 PM, Conor White-Sullivan < [email protected]> wrote:
> > > I have a couple scripts I'd like to write -- I think they're pretty > simple, but I'm new to python and leo > > If anyone can help I'd appreciate it > First, all of this, except perhaps the bonus, should be in Leo's scripting chapter <http://leoeditor.com/tutorial-scripting.html>. It would be good to review this. > > Script 1 : Creating Children of current based on selected text > > Goal: I'd like to > > 1. get the value of the text that is currently selected > c.frame.body.wrapper is the interface to Leo's body pane. The following (tested) code prints the the selected text: w = c.frame.body.wrapper s = w.getSelectedText() print(s) 2. create a new node, which has that text as both its headline and body > It was unclear from the tutorial how to just create a new node with > any arbitrary content > c.p is the presently selected position. To create a new node as the last child of c.p: p = c.p.insertAsLastChild() To see the change, do: c.redraw() To set headline and body text of the new node: p.h = 'headline' p.b = 'body text' > Bonus: > > 1. Trigger a little popup that lets me put in the value of what I'd like > to name this new child > Look at the methods in leoPy.leo: Code-->Qt gui-->@file ../plugins/qt_gui.py-->class LeoQtGui(leoGui.LeoGui)-->LeoQtGui.Dialogs & panels Access them with g.app.gui, for instance, g.app.gui.runAskOkCancelStringDialog(...) 2. remove the selected text and replace it with the value << 'whatever I > named the new child node in the prompt' >> > I'll leave this as an exercise :-) Hints: 1. With w set as above, you can access all the methods defined here: Code-->Qt gui-->@file ../plugins/qt_text.py-->class QTextEditWrapper(QTextMixin) 2. You get the selection range with w.getSelectionRange(), get the original text with s = w.getAllText() and then replace the text with w.setAllText(). 3. You can compute the final text using python's string slices. This is a good beginning Python exercise. Ask for help here if you get lost. ----- Also, what is the best way to discover how to do these kind of things > without asking others? > After you have read Leo's scripting chapter, you can look at Leo's own source code in leoPy.leo (leoPyRef.leo). Edward -- You received this message because you are subscribed to the Google Groups "leo-editor" group. To unsubscribe from this group and stop receiving emails from it, send an email to [email protected]. To post to this group, send email to [email protected]. Visit this group at https://groups.google.com/group/leo-editor. For more options, visit https://groups.google.com/d/optout.
