Example: File browser

A directory browser built on a Treeview — name / size / kind columns, double-click to enter folders or open files, an Up button, and a right-click context menu.

Run it:

scriptweaver internal/scriptweaver/testdata/filebrowser.js

It shows a Treeview driven by sw.fs and sw.sys.

Listing a directory

sw.fs.list + sw.fs.stat populate the tree, folders first:

const entries = sw.fs.list(dir).map((n) => {
  const st = sw.fs.stat(join(dir, n));
  return { name: n, isDir: st.isDir, size: st.size };
});
entries.sort((a, b) =>
  a.isDir !== b.isDir ? (a.isDir ? -1 : 1) : a.name.localeCompare(b.name),
);

for (const e of entries) {
  const id = tv.insert('', 'end', { text: e.name });
  tv.set(id, 'size', e.isDir ? '' : String(e.size));
  tv.set(id, 'kind', e.isDir ? 'folder' : 'file');
}

Double-click enters a folder, or opens a file in the OS default app:

tv.bind('<Double-1>', () => {
  const row = rows[tv.selection()[0]];
  if (row.isDir) load(row.path);
  else sw.sys.open(row.path);
});

A context menu

Right-click selects the row under the cursor, then posts a menu at the pointer:

tv.bind('<Button-3>', (e) => {
  const id = __native_tcl(tv._id, 'identify', 'item', e.x, e.y);
  if (id) tv.selectionSet(id);
  ctx.post(e.screenX, e.screenY);
});

Full source: internal/scriptweaver/testdata/filebrowser.js.

See also