FeaturesPricingBlogDownload

Advanced

Rhai Scripting

Use the embedded Rhai scripting engine for advanced logic and manipulation.

Rhai is a lightweight, incredibly fast, sandboxed scripting language embedded directly into Snipset's Rust backend. When standard template variables can't handle a complex use case, Rhai gives you full programmatic control—with native access to your clipboard, dates, and system info.

The Correct Syntax

Important: The correct syntax to call a Rhai script is `#{script:expression}`. Do NOT use `#{rhai:expression}`. Using the `rhai:` prefix will fail.
When Snipset evaluates the variable, it runs the entire expression as a Rhai script. The return value (the result of the last expression) is what gets inserted into your snippet.
  • Timeout: 10 seconds. If your script runs longer, it outputs [Script Timeout].
  • Errors: If there is a syntax error, it outputs [Script Error: <message>].

Simple Expressions

You can use Rhai for basic math or string manipulation.

`#{script:2 + 2}` → `4`

#{script:"hello".to_upper()}HELLO

#{script:get_clipboard().len().to_string() + " chars"}42 chars

Built-in Functions Reference

Snipset injects several custom functions into the Rhai environment specifically for text automation:

Function Returns Example
`get_clipboard()` String `#{script:get_clipboard()}`
`date_format(fmt)` String `#{script:date_format("yyyy-MM-dd")}`
`add_days(n)` String (ISO date) `#{script:add_days(7)}`
`is_weekend()` Boolean ` #{script:if is_weekend() {"Yes"} else {"No"}} `
`get_user_name()` String `#{script:get_user_name()}`

Conditional Logic

Rhai supports standard if / else blocks. This is useful for snippets that change based on context, like the day of the week.

Snippet:
    `
      #{script:if is_weekend() {"Enjoy your weekend! "} else {"Have a productive day! "}
      }
    `
  

Combining with Other Variables

Rhai is evaluated early in Snipset's pipeline. This means you can nest other Snipset variables inside a Rhai script. However, the most robust way to manipulate data is to use Rhai's native functions (like get_clipboard()) instead of nesting #{clipboard}.

Correct (Native): `#{script:get_clipboard().replace("old", "new")}`

Limitations and Security

  • Sandboxed: Rhai runs in a strictly sandboxed environment. It has no file system access and cannot make network calls. (Use Ollama for AI tasks).
  • No Imports: There is no import or require module support. All code must be inline.
  • Loop Limits: Loops have a maximum iteration count to prevent your computer from freezing due to an accidental infinite loop.
  • Plain Text Output: The output of a Rhai script is treated as a plain string. It cannot output another #{variable} and expect it to be evaluated.