Automation and scripting
MagnumOpus has no built-in scripting language. You write the script in whatever you already use - PowerShell, Python, a .bat file, AutoHotkey - and the explorer can:
- be controlled from outside -
--cmd <id>runs a command palette command in the running window, - take a script's result - a list of paths becomes a result tab,
- run your scripts well - user commands with options for each file, output, refresh, confirmation, progress and cancelling.
mo.exe for scripts
mo-explorer.exe is a window application: cmd and PowerShell do not wait for it and a script never gets its exit code. That is why there is a small console launcher mo.exe next to it. The installer can add it to PATH for your user ("Add to PATH"); user commands get its full path in the MO_CLI variable.
mo.exe takes the same arguments as mo-explorer.exe, plus:
| Switch | Meaning |
|---|---|
--wait | wait until all --cmd commands are done and return their exit code |
--timeout <s> | how long --wait waits (default 60 s, 0 = no limit) |
--no-start | do not start the app when it is not running (exit code 3) |
When the app is not running, mo.exe starts it and waits for its window. Calling mo-explorer.exe --cmd ... directly works too - the command runs, only without waiting and without an exit code.
Subcommands
Besides the switches, mo.exe has subcommands that run without the explorer window and return an exit code:
| Subcommand | What it does |
|---|---|
mo diskmap <folder> | what takes up space in a folder or on a disk (section Disk usage below) |
mo cleanup [<drive>] | what can be freed on a drive, by category (section Disk cleanup below) |
mo link upload <link> | uploads the changes of a linked folder to its server |
mo diff --sync <profile> | runs a saved synchronization profile |
Each takes --help. The exit codes of mo link upload and mo diff --sync are on their own pages.
Running commands with --cmd
mo [path ...] --cmd <id> [--cmd-arg <value>] [--cmd <id> ...] [--window <id>] [--wait]
--cmd <id>runs a command. It can be repeated; commands run in the given order and each starts only after the previous one is done.- An argument goes either after
=(--cmd select.mask=*.jpg) or in--cmd-argright after the command. A command that needs an argument and gets none opens its dialog, the same as from the keyboard. - Paths and other switches (
--split,--view...) apply before the first--cmd. - User commands run as
user:<name>, saved workspaces asworkspace:<name>(the name ignores letter case).
When is a command done
Each ID has a kind of completion, shown in the Done column of Commands and IDs:
| Done | Meaning | Examples |
|---|---|---|
| instant | the action was performed | select.*, view.mode, tag.* |
| folder loaded | the active tab has loaded (a search has finished) | nav.go, tab.new, filter.set |
| dialog closed | OK = success, Cancel = exit code 8 | file.rename, select.mask without a mask |
| job finished | the copy, archive or user command has ended | file.paste, file.copy-to-other, user:<name> |
When the window has a dialog, menu or rename open (or the active tab is still loading), the commands wait in a queue and run after it closes. workspace:<name> replaces the window with the workspace windows, so commands after it do not run in this window.
Finding IDs and windows
mo --list-commandsprintsid<TAB>argument<TAB>description(description in the interface language), including your user commands and workspaces. The app does not have to be running.--jsongives an array of{id, title, arg, required, done}.mo --list-windowsprintsid<TAB>*<TAB>window title, where*marks the active window - the one that gets commands without--window.--jsongives{id, active, title}.--window <id>sends the commands to a specific window. Every user command gets the ID of its window inMO_WINDOW.
The full list is in Commands and IDs. IDs are a stable interface: they are never renamed or removed between versions.
The tools have IDs as well. Without an argument they open their dialog for the current selection, so a script can prepare the selection and hand over to you:
mo --cmd "tools.find-duplicates=D:\Photos;E:\Backup"
mo --cmd "tools.disk-cleanup=C:\"
mo D:\Photos --cmd select.mask=*.jpg --cmd file.convert-images
More IDs a script can use:
| ID | What it does |
|---|---|
tools.find-duplicates, tools.disk-cleanup | the duplicates and disk cleanup tools |
shelf.add=<paths>, shelf.show, shelf.copy-here, shelf.move-here, shelf.pack, shelf.clear | the shelf: put paths on it, open it, copy / move / compress what is on it |
link.upload, link.upload-selected, link.compare, link.undo, link:<name> | linked folder: upload, compare, undo, upload by link name |
sync:<profile> | opens a folder comparison by a synchronization profile in MagnumOpus Diff |
window.new, pane.split-right, pane.sync-select, diff.compare-folders | new window, split, select differences, compare the two panes |
A shelf from a script, copied into a folder and waited for:
mo --cmd "shelf.add=D:\Photos\a.jpg;D:\Photos\b.jpg"
mo D:\Backups --cmd shelf.copy-here --wait
Exit codes
| Code | Meaning |
|---|---|
| 0 | done (without --wait: handed over) |
| 1 | a command failed (the error text goes to stderr, e.g. copying ended with an error) |
| 2 | invalid switch or argument (also a path in an argument that does not exist) |
| 3 | the app is not running and --no-start was given (or it failed to start) |
| 4 | unknown command ID - nothing was run, IDs are checked first |
| 5 | the command cannot run here (user command or workspace does not exist, remote folder) |
| 6 | the --window window does not exist |
| 7 | --timeout expired (the commands still finish in the app) |
| 8 | cancelled by the user (Cancel in a dialog, the cross in the jobs panel, Stop) |
| 9 | control from the command line is turned off in Settings |
Codes 1, 3, 5, 7 and 8 come only from mo.exe --wait. After an error the remaining commands are skipped.
Result tab
A script can hand a list of files back to the explorer:
mo --results <file> [--title <name>],mo --results - [--title <name>]reads the list from stdin (... | mo --results -),--cmd results.show=<file>inside a longer command sequence,- a user command with Output: List of files (see below).
The tab "Result:
Format
- One path per line,
\nor\r\n; empty lines are skipped, a path in quotes is unquoted. - UTF-8 or UTF-16 (a BOM decides; PowerShell 5
Out-Fileis recognized without one), otherwise the OEM code page of older console tools. - Relative paths are resolved against the folder the command was started from.
sftp://andmtp://paths work too. path<TAB>noteadds a Note column - for example the line number and text of a ripgrep match.- Duplicates are merged, paths that do not exist are skipped and counted in a notification.
Disk usage: mo diskmap
mo diskmap <folder> prints what takes up space - for a script, or for an AI agent deciding what to clean up. It runs without a window, the app does not have to be running and nothing is deleted.
mo diskmap <folder> [--depth <n>] [--top <n>] [--min-size <size>] [--flat] [--json] [--scan]
| Switch | Meaning |
|---|---|
--depth <n> | tree levels (default 2) |
--top <n> | largest items per folder (default 20, with --flat 50; at most 5000) |
--min-size <size> | hide smaller items: 100MB, 1.5G, 500k or 1% of the folder |
--flat | the largest files anywhere under the folder instead of the tree |
--json | JSON for scripts |
--scan | always scan the folder, do not use the MFT service |
With the MFT folder size service (Settings > Search and sizes) only the listed folders are read, so even a whole disk takes a moment; without it, and always with --flat, the folder is scanned. Hidden items are summed up in a "+ N more" line (count in JSON counts a hidden folder as one item and a group of small files by its files). The text output is for people - a script should use --json. Text dates are in local time and, when redirected, the text comes in the console's code page (as PowerShell and cmd expect; UTF-8 in Git Bash). Characters outside that code page become ?, so act on paths from JSON only. JSON dates are UTC. Labels say what can usually go: build output (target next to Cargo.toml, obj and bin of a .NET project), dependencies (node_modules, Python virtual environments), temporary files, caches, the Recycle Bin, crash dumps and a previous Windows. System files such as pagefile.sys are marked not to be deleted by hand.
mo diskmap C:\Users\me --depth 3 --min-size 1%
mo diskmap D:\ --flat --top 30 --json > big-files.json
The JSON is {path, size, files, source, mode, ms, errors, truncated, items, more}: source is service or scan, mode is tree or flat. Each item is {name, path, type, size, share, modified, hint}, a folder also has files, items and more - type is dir or file, share is the percentage of the whole, modified is UTC in ISO 8601 and hint is one of build, deps, temp, cache, trash, dump, old-system, system or null. more is {count, size, share} of the hidden rest. A folder without items was not listed - below --depth, too small to show, unreadable or empty; truncated: true means listing stopped at 20 000 folders. Characters outside ASCII come as \uXXXX, so Windows PowerShell 5.1 reads paths with diacritics correctly with ConvertFrom-Json.
Exit code 0 = done (also when some subfolders cannot be read - errors counts them), 1 = the folder cannot be read or an internal error (details in crash.log), 2 = invalid switch or the folder does not exist, 3 = mo-explorer.exe is missing next to mo.exe. Ctrl+C stops the listing.
Disk cleanup: mo cleanup
mo cleanup [<drive>] prints what can be freed on a drive by category - the same categories and sizes as the Clean up disk... tab. It runs without a window and without --run nothing is deleted.
mo cleanup [<drive>] [--list <id>] [--run <id,id...>] [--yes] [--json]
| Switch | Meaning |
|---|---|
<drive> | C:, D:\ or a path on the drive (default: the drive of the current folder) |
--list <id> | items of a category with paths and sizes; build searches the folders with projects now |
--run <id,...> | clean up categories permanently, the same way as the tab (open files are skipped) |
--yes | do not ask before --run |
--json | JSON for scripts |
--run takes only temporary data that needs no administrator rights: temp, thumbs, mo, browser, shader, dumps. The Recycle Bin, old downloads and large files, developer caches, project build output and system items (Windows Update, Delivery Optimization, Windows temporary files, previous Windows) are cleaned only in the tab - item by item, personal files into the Recycle Bin, system items with one UAC prompt.
mo cleanup
mo cleanup D: --json > cleanup.json
mo cleanup C: --list dev
mo cleanup C: --run temp,browser,shader --yes
The overview JSON is {drive, free, total, categories}, each category {id, name, bytes, admin, denied, how, runnable} - bytes is null when only an administrator can measure it (Windows Update Cleanup) or the category is searched on demand (large, build). --list gives {id, items} with {section, name, path, bytes, skipped}, --run gives {drive, freed, results} with {id, ok, freed, skipped, running, denied, rejected}. Characters outside ASCII come as \uXXXX.
Exit code 0 = done (also when open files were skipped - skipped, running, denied and rejected in JSON say so), 1 = a category failed or the question was declined, 2 = invalid switch, drive or category (also a category --run does not take), 3 = mo-explorer.exe is missing next to mo.exe.
Linked folders and synchronization
mo link upload <link> uploads the changes of a linked folder without the window and returns the upload's code (0 = uploaded or nothing to upload, 10 = it needs confirmation - run it again with --yes, 12 = some items could not be uploaded). --plan only prints the plan. All switches and codes are in Linking a folder to a server.
mo link upload shop --plan
mo link upload shop --yes
In the running app mo --cmd link:shop --wait does the same with the window and notifications. A saved synchronization profile runs with mo diff --sync <profile> --plan or --run --yes - see Comparing and synchronizing folders.
User commands
Settings > User commands holds your own programs and scripts that run over the selection - from the command palette, the context menu (only when the selection matches the mask) and the keys User command 1–9, which you assign in Settings > Shortcuts.
| Field | Meaning |
|---|---|
| Name, Program, Arguments | what runs; .ps1 runs through PowerShell, .bat / .cmd through cmd |
| Working folder | empty = the current folder |
| Only for files | mask such as *.jpg;*.png (empty = all) |
| Hide the console window | runs without a console |
| Run | Once for all files / For each file separately, with At most at once |
| Output | Nothing / Notification / Output window / List of files |
| Refresh the folder when done | reloads the folder after the command ends |
| Ask before running | "Run X for 12 files?" |
Tokens in the arguments:
| Token | Value |
|---|---|
{file} | the first selected item (with For each file: the current file) |
{files} | all selected items |
{name} | the name of the first item |
{dir} | the current folder |
{dest} | the folder of the other pane |
{filelist} | a temporary file with all selected paths, one per line - for hundreds of files that do not fit on a command line |
{input} | a text you type before running (Cancel or empty = do not run) |
The script also gets the environment variables MO_DIR, MO_DEST, MO_WINDOW and MO_CLI, so it can control the window it was started from.
Everything runs in the background - the window never waits for a command. A command running longer than a second appears in the jobs panel with its time (and "37 / 120" for each file); the cross stops it including its child processes. The Output window shows live output with errors in red, Copy and Stop. A non-zero exit code always produces an error notification.
Workspaces
A workspace saves the current windows with their tabs, panels and split under a name. Workspaces... in the command palette saves, overwrites and deletes them; open one from the dialog, the palette ("Open workspace: ...") or the Workspaces section of Ctrl+P. From a script: --cmd workspace:<name>.
The command palette (Ctrl+Shift+P) lists every command, plus user commands and workspaces. It finds a command even by scattered letters.
Examples
Project layout
The project on the left, backups on the right, only changed files, newest first:
# project.ps1
$mo = if ($env:MO_CLI) { $env:MO_CLI } else { 'mo' }
& $mo 'C:\Projects\aurora' --new-tab `
--cmd pane.split-right --cmd-arg 'D:\Backups\aurora' `
--cmd view.mode --cmd-arg detail `
--cmd view.sort --cmd-arg date:desc `
--cmd filter.set --cmd-arg '[git = changed]' `
--wait
if ($LASTEXITCODE -ne 0) { Write-Error "MagnumOpus: exit code $LASTEXITCODE" }
Search file contents with ripgrep
A user command:
| Field | Value |
|---|---|
| Name | Search contents (ripgrep) |
| Program | rg |
| Arguments | --files-with-matches --ignore-case -- {input} . |
| Output | List of files |
| Hide the console window | yes |
{input} asks for the search text first. Without ripgrep, Windows can do the same: program findstr.exe, arguments /s /m /i /c:{input} *.
From a console, with the matching line as a note:
rg --files-with-matches FIXME C:\Projects | mo --results - --title 'FIXME in projects'
rg --line-number --no-heading FIXME C:\Projects |
ForEach-Object { if ($_ -match '^(.*?):(\d+):(.*)$') { "$($Matches[1])`t$($Matches[2]): $($Matches[3].Trim())" } } |
mo --results - --title 'FIXME with lines'
Sort photos by year
A user command with Program SortPhotos.ps1, Arguments {filelist} {dest}, Run once for all files, Output Notification, Refresh the folder when done, Ask before running and the mask *.jpg;*.jpeg;*.heic;*.png. Select photos in one pane, the target folder in the other:
# SortPhotos.ps1 - moves photos to <target>\<year>\ by the date taken (EXIF),
# without EXIF by the date modified.
param([string]$List, [string]$Dest)
$shell = New-Object -ComObject Shell.Application
$moved = 0
foreach ($path in Get-Content -LiteralPath $List -Encoding UTF8) {
$f = Get-Item -LiteralPath $path
$folder = $shell.Namespace($f.DirectoryName)
$taken = $folder.GetDetailsOf($folder.ParseName($f.Name), 12) # Date taken
$year = if ($taken -match '(\d{4})') { $Matches[1] } else { $f.LastWriteTime.Year }
$target = Join-Path $Dest $year
New-Item -ItemType Directory -Force -Path $target | Out-Null
Move-Item -LiteralPath $f.FullName -Destination $target
$moved++
}
"Moved $moved photos" # the last line becomes the notification text
Security and turning it off
Requests go only through Windows messages to the window and a named pipe for the reply - no TCP or HTTP. Only programs of the same user in the same session can control the app, and a process with lower integrity cannot send commands to one with higher. Commands take the same path as from the user, so delete confirmations and elevation are not skipped.
Settings > Command line > Control from the command line (mo --cmd) turns it off; then every --cmd returns exit code 9.
See also the Command line under the panels and the command-line options of mo-explorer.exe.