getting node name of script behind a button?

35 views
Skip to first unread message

Matt Wilkie

unread,
Apr 19, 2020, 12:01:25 PM4/19/20
to leo-editor
How do I get the name/position of a script bound to a button, regardless of what node is currently selected when the button is used?

The goal is make it clear in the Log pane where the status message is coming from (the script).

So far I'm using `c.p.h` to report the name like this:

g.es_print("====", c.p.h, "====")


This works fine when I fire the script with Ctrl-B. Howevre after binding the script to a button it reports the currently selected node, which can be anywhere, and not the script's name.

thanks!

-matt

Thomas Passin

unread,
Apr 19, 2020, 7:01:29 PM4/19/20
to leo-editor
I don't know what the create-button script does, but if you want something quick and dirty for occasional use, you could embed the script's id and print that.  The script's node identifier is p.gnx,so if you add lines to your script like

id = 'TomP.20200419185110.1'
g.es(id)

it would identify your script's node.  Or you could just put in the name of the node directly and use that.

Edward K. Ream

unread,
Apr 20, 2020, 11:44:51 AM4/20/20
to leo-editor
On Sun, Apr 19, 2020 at 11:01 AM Matt Wilkie <map...@gmail.com> wrote:
How do I get the name/position of a script bound to a button, regardless of what node is currently selected when the button is used?

You can right-click the button itself, then choose "Goto Script".

If you want to do that programmatically, you will have to study the horrible code in mod_scripting.py. Searching for 'Goto Script' yields qtFrame.QtIconBarClass.setCommandForButton, which includes a gnx that could then be used to find a vnode, and its headline.

The goal is make it clear in the Log pane where the status message is coming from (the script).

I agree with Thomas that having the script itself print the message looks simplest.

Edward

Thomas Passin

unread,
Apr 20, 2020, 11:58:36 AM4/20/20
to leo-editor
If you want to get a little more complicated, you could do this dance:

1. Write the script so that when it's run the first time, it rewrites the script node to define its name:

p.b = f'script_name = {p.h}\n' + p.b

2. Run the script using CTRL-B.

3. There has to be some code to check to see if script_name has been defined.  If it is, the script would output your log message using its name.  Otherwise, it outputs no nome.

4. When the script runs, it logs using the name if it was already defined.

This would let you use the same button for many different trees.  OTOH, it does add more complication, so probably it's just easiest to embed the name directly.

Matt Wilkie

unread,
Apr 20, 2020, 2:49:34 PM4/20/20
to leo-editor
Thanks for the extra details that extend my understanding and gives path trail heads for more. For now I will content myself with just writing the name!

-matt
Reply all
Reply to author
Forward
0 new messages