// Recolour Selection — Adobe Illustrator compatible
// Utility: applies a single fill colour to every selected path. Demonstrates iterating the
// selection (a very common real-world script pattern).
//
// svgfeedback SVGE-1403 — this script used to read
//     (doc.selection && doc.selection.length) ? doc.selection : doc.pathItems
// i.e. it fell back to the WHOLE DOCUMENT when nothing was selected. A Run with an empty
// selection therefore repainted every path in the file amber (493 of them in the report),
// with no warning before and nothing to tell it apart from a legitimate result after. A
// script named "…Selection" must operate on the selection and on nothing else: with an
// empty selection it now refuses and leaves the document untouched, exactly like its
// siblings 08-random-delete and 09-random-distribute. (`doc.selection` itself was already
// honest — an EMPTY array when nothing is selected, on both the WKWebView engine and the
// Skia/JSC route; the document-wide fallback was the whole bug.)
#target illustrator

var doc = app.activeDocument;

var fill = new RGBColor();
fill.red = 245; fill.green = 166; fill.blue = 35;   // amber

// Snapshot the selection ONCE — doc.selection is a live getter.
var sel = doc.selection;
var items = [];
for (var i = 0; sel && i < sel.length; i++) if (sel[i]) items.push(sel[i]);

if (!items.length)
    throw new Error('Nothing selected — select one or more objects first, then run ' +
                    'Recolour Selection again.');

var n = 0;
for (var j = 0; j < items.length; j++) {
    var it = items[j];
    if (it.typename === "PathItem") {
        it.fillColor = fill;
        it.stroked = false;
        n++;
    }
}

app.redraw();
// Honest about a selection that holds no path either, instead of a bare "Recoloured 0 path(s).".
$.writeln(n ? ('Recoloured ' + n + ' path(s).')
            : ('Recoloured 0 path(s) — the ' + items.length +
               ' selected object(s) contain no path.'));
