Skip to main content
Roci
Participant
February 25, 2025
Question

Illustrator script to edit multiply files

  • February 25, 2025
  • 2 replies
  • 383 views

Hello everyone!

 

Thank you all in advance for your help and advice.

 

Our ssue:

  • We have exported 1200 .svg files from nodebox.
  • Each file has background, invisible stroke graphics and everything is in single layer
  • We need to remove the "background", colorize (#BBA3FF) invisible stroke graphics and make 0.2 px stroke weight
  • save and close all files

 

Is it poosible to do su much actions?

2 replies

Stefan Rakete
Inspiring
February 28, 2025

Hi Roci,
this is a perfect task for Illustrator scripting. In a similar project we decided to process the files in chuncks of 100 because the performace of Illustrator was regressing.

m1b
Community Expert
Community Expert
February 25, 2025

Hi @Roci It might be possible with a script. Could you share one or two of the svg files?

- Mark

Roci
RociAuthor
Participant
February 25, 2025

@m1b Sure, attaching. During that time I managed to disable background, so now it the matter of the stroke color and weight.

m1b
Community Expert
Community Expert
February 25, 2025

Great. I have written a script that will process your svg files. Instructions are in the script header. You can look through and get an idea of what it is doing. It just removes some unwanted attributes from each path and adds a class name "line" and adds a CSS style definition. You should edit the style definition to suit your needs. Let me know how it goes.

- Mark

 

Before:

 

After:

 

 

 

/**
 * @File Style SVG Files.js
 *
 * Set up:
 * Run this script once, to create "SVG IMPORT" and "SVG EXPORT"
 * folders in the same location as this script file.
 *
 * Usage:
 * 1. Put your svg files into "SVG IMPORT"
 * 2. Run script
 * 3. Collect the resulting svg files from "SVG EXPORT"
 *
 * @7111211 m1b
 * @version 2025-02-26
 * @discussion https://community.adobe.com/t5/illustrator-discussions/illustrator-script-to-edit-multiply-files/m-p/15177289
 */
(function () {

    // edit this to match your needs
    // must be valid CSS
    var style = [
        'fill: none;',
        'stroke: #1d1d1b;',
        'stroke-width: .2px;',
        'stroke-miterlimit: 10;',
    ];

    var svgImportFolder = Folder(File($.fileName).parent + '/SVG IMPORT/'),
        svgExportFolder = Folder(File($.fileName).parent + '/SVG EXPORT/');

    if (!svgImportFolder.exists)
        svgImportFolder.create();

    if (!svgExportFolder.exists)
        svgExportFolder.create();

    // collect the svg files
    var files = svgImportFolder.getFiles(),
        fileCounter = 0;

    // this is the style definition element
    var styleElement = new XML(
        '<defs><style>.line {##STYLE##}</style></defs>'
            .replace('##STYLE##', style.join(' '))
    );

    fileLoop:
    for (var i = 0; i < files.length; i++) {

        var f = files[i];
        f.open('r');
        var svg = f.read();

        if (!svg)
            continue fileLoop;

        try {

            var xml = new XML(svg),
                elements = xml.descendants(),
                len = elements.length(),
                elementCounter = 0;

        } catch (error) {
            // failed to parse XML
            continue fileLoop;
        }

        elementLoop:
        for (var j = 0; j < len; j++) {

            var el = elements[j];

            if ('path' !== el.localName())
                continue elementLoop;

            // remove unwanted attributes
            delete el['@stroke-width'];
            delete el['@stroke'];
            delete el['@fill'];

            // add our class
            el['@class'] = 'line';

            elementCounter++;

        }

        if (0 === elementCounter)
            continue fileLoop;

        // Append <defs> to the root <svg>
        xml.insertChildBefore(elements[0], styleElement);

        // write to file
        var exportFile = File(svgExportFolder + '/' + decodeURI(f.name));
        exportFile.open('w');
        var success = exportFile.write(xml.toString());

        if (success)
            fileCounter++

    }

    alert('Processed ' + fileCounter + ' svg files.');

})();

 

Edit 2025-02-26: minor typo.