Understanding the structure of menu scripts

Moho allows users to write new tools and plugins. Discuss scripting ideas and problems here.

Moderators: Víctor Paredes, Belgarath, slowtiger

Post Reply
nsnap
Posts: 2
Joined: Thu Mar 15, 2018 3:52 pm

Understanding the structure of menu scripts

Post by nsnap »

Hi all, I'm new to LUA and MOHO, so I'm afraid I'm struggling to figure out the structure / format of scripts. I was hoping someone could help me out.

I'm not new to programming in general, though I'm not an expert either. So the general concepts of variables, functions, loops, and so on make sense to me. But what I'm not comprehending is how menu scripts are actually invoked. I found a few sample menu scripts online, but I couldn't parse out which function is the base function - in other words, when I open the menu in MOHO and click on the script, which function does it call first? I can see there are functions that are called on mouse action and key presses, but I couldn't find one for menu selection.

I thought at first that it would just play from the top of the script when you selected it from the menu, but that doesn't seem to be the case. It looks like the script starts running as soon as you launch MOHO, and then constantly checks to see if it's enabled and relevant. But those functions just return booleans, and don't seem to call the functions that do the heavy lifting.

Hopefully I explained my confusion clearly. Could anyone give me pointers here? I'm just not sure how to make my script actually launch when I select it from the menu.

Thanks so much!

N
User avatar
synthsin75
Posts: 9934
Joined: Mon Jan 14, 2008 11:20 pm
Location: Oklahoma
Contact:

Re: Understanding the structure of menu scripts

Post by synthsin75 »

Menu and button scripts, like the insert text tool, are the same thing. Both run the code in their "Run" function when activated.

All other functions are either subroutines called from the "Run" function or overhead that all scripts use, like telling it when it's greyed-out, and can't be used, etc..

You can also look in the scripts folder of the installation directory to find all the stock scripts to study.
nsnap
Posts: 2
Joined: Thu Mar 15, 2018 3:52 pm

Re: Understanding the structure of menu scripts

Post by nsnap »

I just gave that a whirl and it worked like a charm! Thanks for the help, Wes!

Out of curiosity, while I have you here, two related questions:

1. Is there a hotkey to refresh the scripts, or do I have to restart MOHO every time I want to test a change?

2. I was looking at a script I found online (AST_Switch_Layer_Plus v1.1 by Vernon Zehr), and it doesn't seem to have a Run function at all. Do you know why that might be? Are there other ways to invoke a script that don't involve a function specifically called "Run"?

Thanks again!
N
User avatar
synthsin75
Posts: 9934
Joined: Mon Jan 14, 2008 11:20 pm
Location: Oklahoma
Contact:

Re: Understanding the structure of menu scripts

Post by synthsin75 »

Yes, Ctrl+Shift+Alt+L reloads all scripts.

If it doesn't have a "Run" function, it's likely a tool script (would likely have either "OnMouseDown" and/or "OnKeyDown" functions, which activate in response to clicks and keyboard).
User avatar
hayasidist
Posts: 3492
Joined: Wed Feb 16, 2011 8:12 pm
Location: Kent, England

Re: Understanding the structure of menu scripts

Post by hayasidist »

Post Reply