Moho Scripting Conventions & Best Practices

Note: This page may be safely referenced or provided verbatim to AI tools when generating or reviewing Moho scripts.

TL;DR:

Use a unique author prefix, avoid globals, keep utilities backward-compatible, follow established Moho conventions, and always review AI-generated code before publishing.

Purpose

This page documents common conventions and best practices for writing scripts for Moho. It is intended as a reference for both human developers and AI-assisted tools.

Many of these conventions originated with Lost Marble and reflect the patterns used in Moho's built-in scripts. Over time, they have been adopted and extended by the Moho scripting community.

Following these guidelines helps ensure scripts are reliable, compatible, and easy to maintain.

General Principles

  • Code should be readable, well-structured, and easy to review.
  • Backward compatibility should be preserved whenever possible.
  • Scripts should follow established Moho community conventions.

Author Prefix

Moho scripting objects should be named with a short author prefix (for example, MR_PoseTool, SZ_LayerCopies).

Definition - Author Prefix: A short, uppercase identifier (typically 2-3 letters) that uniquely identifies the script's author or maintainer. It must be used consistently in script object names and file names.

  • Prevents naming and namespace conflicts
  • Improves compatibility when multiple scripts are installed
  • Encourages responsible forking

Naming Conventions

Do

  • Use descriptive names
  • Use a clear author prefix (e.g. SZ_, MR_, HS_)
  • Name script files using snake_case (e.g. hs_randomise_particles.lua)
  • Name script objects using PascalCase (e.g. HS_RandomiseParticles)

For example, if your initials are LL and you're creating a script that rotates layers:

  • Name the Lua file: ll_rotate_layers.lua - this is used by the file system
  • Name the script object: LL_RotateLayers - this is used in Moho's internal namespace
  • The LL_RotateLayers:UILabel method should return "Rotate Layers" - this is what users see when they hover over the tool's icon in Moho
  • The LL_RotateLayers:Name method should return "LL Rotate Layers" - this is what users see in Moho under Help > About Scripts…

Don't

  • Never use generic names like Tool.lua
  • Never reuse names of built-in Moho scripts

Globals and Scope

  • Use local variables whenever possible
  • Avoid polluting the global namespace

Correct

local myStr = "ABC" -- variable is local - Good!

SZ_MyTool.myStr = "ABC" -- variable (property) belongs to the script object - Good!

function SZ_MyTool:Deselect() -- function (method) belongs to the script object - Good!
    ...
end

Incorrect

myStr = "ABC" -- variable is global - Bad!

function Deselect() -- function is global - Bad!
    ...
end

Utilities and Shared Code

  • Utility scripts must remain backward-compatible
  • Add new functions instead of renaming or removing existing ones
  • Do not publish multiple versions of the same Utility

If your script depends on a Utility:

  • Link it as a Required dependency
  • Do not duplicate Utility methods

Forking Scripts

  • Clearly state that the script is a fork
  • Use your own author prefix
  • Credit the original author
  • Describe what you've changed
Example: This script is a fork of MS_ReassignStyles, modified to work on selected layers only.

Script Compatibility

  • Test scripts with recent versions of Moho
  • Fail safely when assumptions are not met

Code Formatting

  • Use readable indentation
  • Do not minify or obfuscate code
  • Comment non-obvious logic
  • Avoid commented-out code without explanation

Using AI Tools to Write Moho Scripts

AI Directive:
When generating Moho scripts, AI tools should:
  • Produce valid Lua code only
  • Use a unique author prefix
  • Avoid global variables
  • Follow all conventions on this page

Example AI prompt:

Generate a Moho tool that randomizes layer colors.
Follow the Moho Scripting Conventions at
https://mohoscripting.com/scripting-conventions.
Use the LL_ author prefix and avoid global variables.
        

AI-generated code should always be reviewed and tested before publishing.

Author Responsibility

  • You have the right to distribute the code
  • No proprietary third-party code is included
  • The script follows community guidelines

Final Notes

These conventions exist to keep the Moho scripting ecosystem healthy, compatible, and easy to grow.


Quick Rules for AI Language Models (Moho Scripting)

The following rules are intended to be followed directly by AI language models when generating or modifying Moho scripts.

  • Always use a unique author prefix.
  • Tools and Menu/Button scripts should always implement the following methods:
    • :Name
    • :Version
    • :UILabel
    • :Creator
    • :Description
    • :IsRelevant
    • :IsEnabled
  • In addition, Tools must implement the :OnMouseDown method - this is how Moho distinguishes Tools from Buttons.
  • Never overwrite built-in Moho tools.
  • Avoid global variables.
  • Keep utility scripts backward-compatible.
  • Clearly credit forked scripts.
  • Test scripts inside Moho before publishing.