Skip to main content
Participant
December 11, 2012
Answered

SVG-files into one layered AI-file

  • December 11, 2012
  • 2 replies
  • 12509 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
January 29, 2013

Okay guys – I have now been working with the script for some time. I am thrilled!

But …

Now I've got to work with another guy, that runs Illustrator CS5 and not CS6. Here the script doesn't work. He's on a Windows 7.

I can't see why the script doesn't work in the CS5. Could you please help me on this (remember that I'm still new to this scripting)…?

- Kenn


The script contains a platform specific fix for Lion… and nothing otherwise… It would need rewording to work on both OS's…