Skip to main content
New Participant
December 11, 2012
Answered

SVG-files into one layered AI-file

  • December 11, 2012
  • 2 replies
  • 12425 views

Hello good guys!

Although I'm new to scripting Illustrator, I'm looking for a script that can help me – and merge 10 SVG-files into one layered AI-file.

I get a lot of projects which contents 10 individual SVG-files that are exported from a GIS program (Map data). Each SVG-file contents specific stuff like water, roads, houses etc., so when 'merging' them together, the result will be one final map. I'll need to copy each SVG-file into a new layer so the end result will be a layered AI-file.

I was thinking that the script should do something like this:

- Find a folder using a dialog box and open its SVG-files (each project has 10 SVG-files (same size))

- Copy the content from each SVG-file into a new document (same size as the SVG-file) on a separate layer

  (The new document will then have 10 layers)

  (Naming each layer the same as the SVG-file name)

- Close all the SVG-files, leaving the new AI-file on the screen

I'll would be very glad if someone could point me in the right direction for this project … Thanks

- Kenn

This topic has been closed for replies.
Correct answer Muppet Mark

I got an error tryign to place svg files too.


Well having had a quick try the same happened here too… but all is not lost… If you knock off the dialog supression it says svg is wrong type to be placed… Well it works just fine in the UI. I was thinking along the lines of this anyhows and this route worked here in basic tests… ( how about you's? )

#target illustrator

var svgFile = File.openDialog("Select File to place...");

var doc = app.activeDocument;

doc. groupItems.createFromFile( svgFile );

A very quick edoit of an existing file and this works herrr…

#target illustrator

main();

function main() {

          if ( app.documents.length == 0 ) { return; }

          app.userInteractionLevel = UserInteractionLevel.DONTDISPLAYALERTS;

          var f = Folder.selectDialog( 'Please choose your Image Folder?' );

          if ( f != null ) {

                    var list = f.getFiles( /\.svg$/i );

 

                    if ( isMac() && osxVersion() == 'Lion' ) {

 

                              list = lionFileFixer( list );

 

                    };

 

                    proccessDoc( list );

          };

};

//

function proccessDoc( fileList, options ) {

 

          var i, doc, lay;

 

          var doc = app.activeDocument;

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

 

                    lay = doc.layers.add();

 

                    lay.name = decodeURI( fileList.name ).match( /(.*)\.[^\.]+$/ )[1];

 

                    lay.groupItems.createFromFile( fileList );

          };

 

          doc.layers.getByName( 'Layer 1' ).remove();

 

          app.redraw();

 

};

//

function lionFileFixer( files ) {

 

          var fixed = Array();

 

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

 

                    fixed.push( File( files.fsName.replace( 'file://', '' ) ) );

 

          };

 

          return fixed;

 

};

//

function osxVersion() {

 

          switch( Number( $.os.substring( 16, 17 ) ) ) {

                    case 4 : return 'Tiger';

                    case 5 : return 'Leopard';

                    case 6 : return 'Snow Leopard';

                    case 7 : return 'Lion';

                    case 8 : return 'Mountain Lion';

 

          };

};

//

function isMac() { return /Macintosh/i.test( $.os ); }

2 replies

dirtcreative
New Participant
December 6, 2016

Anyone know how to make this work within macOS Sierra and Illustrator CC 2017?

Inspiring
December 11, 2012

I would place each in a layer…

kennrbAuthor
New Participant
December 11, 2012

exactly – but automated!- thus it's a lot of projects (about 100 projects = 100 x 10 SVG-files that need to be 'merged'). And more to follow next year, so …

… any script-ideas is welcome J

- Kenn

Inspiring
December 11, 2012

Just tried and a placed AI file is placed using the PDF portion and can only be edited using Edit>Edit Original but placing a SVG file from the GUI yields editable path items. Your idea might work.

Using this I can place an AI file and have it be usable. It does not place a SVG file.

#target illustrator

app.userInteractionLevel = UserInteractionLevel.DONTDISPLAYALERTS

var PFile = File.openDialog("Select File to place...");  // select jpg or any other valid file

var idoc = app.documents.add(DocumentColorSpace.CMYK, 612.0, 792.0);  // make new document

var ilayer = idoc.layers.add();   // make template layer

ilayer.name = "Roads";  // name layer

ilayer.zOrder(ZOrderMethod.SENDTOBACK);  // put at bottom of layer stack

  

var iplaced = ilayer.placedItems.add();  // add image place holder

iplaced.file = PFile;  // place file

iplaced.embed();  // embeds the file

app.userInteractionLevel = UserInteractionLevel.DISPLAYALERTS

It may have to be a two part One resaving the SVGs as AI files and then Placing the new AI files on layers in a New Dcoument.


I don't see any problem with script being able to do this process… Like I said I would look to script handling the whole job… ( 1k files ) Putting a script together that just adds 10 placed items to layers would not be overly complex… to do the whole lot as a single process would require more work and more info… but that too can be done…