• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

Illustrator CS5 - Open All Files in Folder, Select All Objects in File and Change Color, and Save as SVG

Community Beginner ,
May 27, 2016 May 27, 2016

Copy link to clipboard

Copied

Could anyone provide some guidance or insight to what is not working in the script below?

I need to convert 300+ icon files to a new color, and save as SVG in Illustrator CS5.

Actions needed:

  1. Open all files in specific folder (optional)
  2. Select all objects in file from selected source folder above or in all currently opened files
  3. Change objects fill color to new color
  4. Export/Save as SVG file into destination folder

I've tried to combine 2 individual scripts - specify object fill color AND save as default save as SVG - into a single script with no success

Could anyone provide some guidance or insight to what is not working in the script below?

//===

// Start of Script

//===

//Script intention is to:

// - Open all files in specific folder (optional)

// - Select all objects in file from selected source folder above or in all currently opened files

// - Change objects fill color to new color

// - Export/Save as SVG file into destination folder

// Tried to combined default save as SVG file from Adobe Illustror CS5 and additional script to change fill color of object.

try {

  if (app.documents.length > 0 ) {

  // Get the folder to save the files into

  var destFolder = null;

  destFolder = Folder.selectDialog( 'Select folder for SVG files.', '~' );

  if (destFolder != null) {

  var options, i, sourceDoc, targetFile;

    // Get the SVG options to be used.

  //options = this.getOptions();

    // You can tune these by changing the code in the getOptions() function.

  for ( i = 0; i < app.documents.length; i++ ) {

  sourceDoc = app.documents; // returns the document object

                var docRef = app.activeDocument;

                var  col;

                // specify new rgb color

                var col = new RGBColor();

                col.red = 17;

                col.green = 101;

                col.blue = 229;

           

                // Create the new swatch using the above color

                var swatch = docRef.swatches.add();

                swatch.color = col;

                swatch.name = "col";

           

                // Apply the swatch to a new path item

                var pathRef = docRef.pathItems[0];

                pathRef.filled = true;

                pathRef.fillColor = swatch.color;

                pathRef.stroked = false; //set object stroke to false

  // Get the file to save the document as svg into

  targetFile = this.getTargetFile(sourceDoc.name, '.svg', destFolder);

  // Save as SVG

  sourceDoc.exportFile(targetFile, ExportType.SVG, options);

  // Note: the doc.exportFile function for SVG is actually a Save As

  // operation rather than an Export, that is, the document's name

  // in Illustrator will change to the result of this call.

  }

  alert( 'Documents saved as SVG' );

  }

  }

  else{

  throw new Error('There are no document open!');

  }

}

catch(e) {

  alert( e.message, "Script Alert", true);

}

/** Returns the options to be used for the generated files.

  @Return ExportOptionsSVG object

*/

function getOptions()

{

  // Create the required options object

  var options = new ExportOptionsSVG();

  // See ExportOptionsSVG in the JavaScript Reference for available options

  // Set the options you want below:

  // For example, uncomment to set the compatibility of the generated svg to SVG Tiny 1.1

  // options.DTD = SVGDTDVersion.SVGTINY1_1;

  // For example, uncomment to embed raster images

  // options.embedRasterImages = true;

  return options;

}

/** Returns the file to save or export the document into.

  @Param docName the name of the document

  @Param ext the extension the file extension to be applied

  @Param destFolder the output folder

  @Return File object

*/

function getTargetFile(docName, ext, destFolder) {

  var newName = "";

  // if name has no dot (and hence no extension),

  // just append the extension

  if (docName.indexOf('.') < 0) {

  newName = docName + ext;

  } else {

  var dot = docName.lastIndexOf('.');

  newName += docName.substring(0, dot);

  newName += ext;

  }

  // Create the file object to save to

  var myFile = new File( destFolder + '/' + newName );

  // Preflight access rights

  if (myFile.open("w")) {

  myFile.close();

  }

  else {

  throw new Error('Access is denied');

  }

  return myFile;

}

//===

// End of Script

//===

Message was edited by: Daniel Chaparro

TOPICS
Scripting

Views

655

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Adobe
Enthusiast ,
May 29, 2016 May 29, 2016

Copy link to clipboard

Copied

LATEST

Hi, you need get files, open files in a loop, and change object's color as you want. You title asked "all objects", but your code only  change one pathItem, I don't know what's your want. And you can change color directly, no need a swatch.

Anyway, let's give you a start.

//===

// Start of Script

//===

//Script intention is to:

// - Open all files in specific folder (optional)

// - Select all objects in file from selected source folder above or in all currently opened files

// - Change objects fill color to new color

// - Export/Save as SVG file into destination folder

// Tried to combined default save as SVG file from Adobe Illustror CS5 and additional script to change fill color of object.

try {

    // Get the folder to save the files into

    var destFolder = Folder.selectDialog('Select folder for SVG files.', '~');

    var files = destFolder.getFiles(); // .getFiles('*.ai')?

    if (destFolder != null) {

        var options, i, docRef, targetFile;

        // Get the SVG options to be used.

        options = this.getOptions();

        // You can tune these by changing the code in the getOptions() function.

        // specify new rgb color

        var col = new RGBColor();

        col.red = 17;

        col.green = 101;

        col.blue = 229;

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

            docRef = app.open(files); // returns the document object

            // Apply the swatch to a new path item

            var pathRef = docRef.pathItems[0];

            pathRef.filled = true;

            pathRef.fillColor = col;

            pathRef.stroked = false; //set object stroke to false

            // Get the file to save the document as svg into

            targetFile = this.getTargetFile(docRef.name, '.svg', destFolder);

            // Save as SVG

            docRef.exportFile(targetFile, ExportType.SVG, options);

            // Note: the doc.exportFile function for SVG is actually a Save As

            // operation rather than an Export, that is, the document's name

            // in Illustrator will change to the result of this call.

            docRef.close();

        }

        alert('Documents saved as SVG');

    }

} catch (e) {

    alert(e.message, "Script Alert", true);

}

/** Returns the options to be used for the generated files.

  @return ExportOptionsSVG object

*/

function getOptions() {

    // Create the required options object

    var options = new ExportOptionsSVG();

    // See ExportOptionsSVG in the JavaScript Reference for available options

    // Set the options you want below:

    // For example, uncomment to set the compatibility of the generated svg to SVG Tiny 1.1

    // options.DTD = SVGDTDVersion.SVGTINY1_1;

    // For example, uncomment to embed raster images

    // options.embedRasterImages = true;

    return options;

}

/** Returns the file to save or export the document into.

  @param docName the name of the document

  @param ext the extension the file extension to be applied

  @param destFolder the output folder

  @return File object

*/

function getTargetFile(docName, ext, destFolder) {

    var newName = "";

    // if name has no dot (and hence no extension),

    // just append the extension

    if (docName.indexOf('.') < 0) {

        newName = docName + ext;

    } else {

        var dot = docName.lastIndexOf('.');

        newName += docName.substring(0, dot);

        newName += ext;

    }

    // Create the file object to save to

    var myFile = new File(destFolder + '/' + newName);

    // Preflight access rights

    if (myFile.open("w")) {

        myFile.close();

    } else {

        throw new Error('Access is denied');

    }

    return myFile;

}

//===

// End of Script

//===

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines