Skip to main content
Participant
December 11, 2012
Answered

SVG-files into one layered AI-file

  • December 11, 2012
  • 2 replies
  • 12429 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
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
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 12, 2012

Hey guys – thank you very much for your great input!

This works. First I open one of the SVG files and delete its content (to get the right artboard dimensions). Then I pick a specific folder containing the SVGs, and your script will stack each file in its own layer. I got no error asking to Replace, Ignore or Cancel. Simply beautiful …

- Kenn

(using MacOSX 10.7.5)


If you know the sizes for all then… You could hard code the new document creation… The other option is to make any sized document and resize the artboard to suit at the end. You didn't mention any particular layer ordering so it just runs at loop that will reverse the stack… If the posts were helful or answered then mark then so… we all want a new adobe mug for christmas…