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.
- Open the script in the editor. In Settings > MaSH > Scripts, choose Edit on the script, or Add new for a new one.
- 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.
- Pick a test file. Use Pick file to choose a document. It becomes
mash.event.file, as if the event had fired for it. - Choose a test user. Use Select test user (the toolbar’s Set test user) to choose the person for
mash.event.user. - Fill in the config parameters. Set the test values in the Config parameters panel.
- 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.
- Read the unhandled errors. A run that ends normally can still have recorded failures. See The run log and unhandled errors.
- 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.
- Attach it. Only now add it to an event, schedule or webhook, with a narrow Path prefix or Extension filter. See Events.
- 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.
Build in a dry-run switch
Section titled “Build in a dry-run switch”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") ?? trueset 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 + 1end
printline "{{moved}} files, dry run: {{dryRun}}"Would move a.pdfWould move b.pdf2 files, dry run: truedryRun = mash.config.get("DRY_RUN") ?? truefiles = ["a.pdf", "b.pdf", "c.txt"]moved = 0
each (files) as name { if (not mash.str(name).endsWith(".pdf")) { next }
if (dryRun) { printline("Would move {{name}}") } else { # mash.files.get(...).move(archive) goes here printline("Moved {{name}}") } moved = moved + 1}
printline("{{moved}} files, dry run: {{dryRun}}")Would move a.pdfWould move b.pdf2 files, dry run: trueThe ?? true default matters. A script nobody has configured does nothing harmful. Turn DRY_RUN off on the attachment only when the output looks right.
Habits that save you
Section titled “Habits that save you”- Copy before you move. Use
duplicatewhile developing. Switch tomoveordeletewhen 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 = truemakes a failing loop stop at once instead of failing a thousand times. - Print as you go.
printlinecosts 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.