Skip to content

Test a script before it touches real files

What it does. Gives you a routine for trying an automation without harm. A script that moves, renames or emails can’t be undone by pressing Stop, so trial runs come first.

What you need.

  • The MaSH feature, and access to Settings > MaSH (the editor is the administrator tier).
  • A test share, or a test folder, with a few copies of real documents.
  1. Open the script in the editor. In Settings > MaSH > Scripts, choose Edit on the script, or Add new for a new one.
  2. Turn on virtual mode. The toolbar has a virtual mode switch. With it on, the editor uses mock files and mock I/O, so a trial run touches nothing real. The button’s tooltip tells you which mode you’re in. Turn it off to run against real files.
  3. Pick a test file. Use Pick file to choose a document. It becomes mash.event.file, as if the event had fired for it.
  4. Choose a test user. Use Select test user (the toolbar’s Set test user) to choose the person for mash.event.user.
  5. Fill in the config parameters. Set the test values in the Config parameters panel.
  6. Run it. Choose Run. The Output panel shows what the script printed, and an Unhandled errors count appears if any statement failed along the way.
  7. Read the unhandled errors. A run that ends normally can still have recorded failures. See The run log and unhandled errors.
  8. Run it for real on the test folder. Turn virtual mode off, pick a copy of a real document, and run again. Check the result in the file browser.
  9. Attach it. Only now add it to an event, schedule or webhook, with a narrow Path prefix or Extension filter. See Events.
  10. Watch the first live runs on Settings > MaSH > Logs.

The editor’s Run isn’t sandboxed to an owner: it runs the text on screen, with the rights of someone who can manage MaSH. A real run on a live file really changes it. Your test file must be a copy. See Who a script runs as.

Add a boolean config parameter called DRY_RUN. In the script, say what you would do instead of doing it:

set dryRun to mash.config.get("DRY_RUN") ?? true
set files to ["a.pdf", "b.pdf", "c.txt"]
set moved to 0
each files as name
if not mash.str(name).endsWith(".pdf")
next
end
if dryRun
printline "Would move {{name}}"
else
# mash.files.get(...).move(archive) goes here
printline "Moved {{name}}"
end
set moved to moved + 1
end
printline "{{moved}} files, dry run: {{dryRun}}"
Would move a.pdf
Would move b.pdf
2 files, dry run: true

The ?? true default matters. A script nobody has configured does nothing harmful. Turn DRY_RUN off on the attachment only when the output looks right.

  • Copy before you move. Use duplicate while developing. Switch to move or delete when you trust the script.
  • Filter the trigger. A filter on Path prefix keeps a test automation off every other folder.
  • Make it safe to run twice. Every real trigger fires more than once. Check for work already done.
  • Cap the batch. In a loop over many files, stop after a fixed number. See Tidy a folder every night.
  • Stop on the first error. stoponerror = true makes a failing loop stop at once instead of failing a thousand times.
  • Print as you go. printline costs nothing and ends up in the run log.
  • Stop a run that’s gone wrong. On Settings > MaSH > Logs, a running job has Stop this run.
  • Grace can rehearse. When you ask Grace to draft an automation, she tries its opening check on up to 20 real files first. See Grace and Captur.