Copy link to clipboard
Copied
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
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 work
...Copy link to clipboard
Copied
I would place each in a layer…
Copy link to clipboard
Copied
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
Copy link to clipboard
Copied
What I meant was to script the placement over opening SVG and duping… Does placing work when using the GUI?
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
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…
Copy link to clipboard
Copied
Right but when I tried to place a SVG file with the script above it errored on the iplaced.file = PFile line. It questioned the file path. When I used an AI file it placed it no problem. Can you get it to do otherwise?
Copy link to clipboard
Copied
I don't use SVG myself… I don't have any issues when saving one and placing using the UI so I was guessing the same would be possible from script… I will have to run a quick test…
Copy link to clipboard
Copied
I got an error tryign to place svg files too.
Copy link to clipboard
Copied
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 ); }
Copy link to clipboard
Copied
That works. What I found doing it the other way is really weird. It would highlight the iplaced.file and say something about the file path. However when I clicked back at the AI document that was created it would show an error saying that some links had been modified and did I want to update them. Then an error came up asking to Replace, Ignore or Cancel. I chose replace and reselected the file. I was then able to embed the file from the link. Here's where its wierd. The file was placed with its center at the upper left of the workarea not the artboard and the file was flipped 180 on the horizontal.
Copy link to clipboard
Copied
Larry, thats not just you I got the same thing here about missing/updated links… This has happed to me once before there could be a bug in the place command… I don't like to call bug not qualified to do so but your the first to get the same result… not just me then.
Copy link to clipboard
Copied
OK, We got the same thing.
When I was messing around it would place an anchor point out there at the corner of the work area because if I selected the layer produced by the script, it would highlight as though there were something on the layer and that's what was referenced when replacing the linked file.
Copy link to clipboard
Copied
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)
Copy link to clipboard
Copied
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… ![]()
Copy link to clipboard
Copied
Thanks Mark, I just did 😉
- Kenn
Copy link to clipboard
Copied
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
Copy link to clipboard
Copied
The script contains a platform specific fix for Lion… and nothing otherwise… It would need rewording to work on both OS's…
Copy link to clipboard
Copied
Thank you thank you Muppet Mark!
Copy link to clipboard
Copied
I'm thinking that you would do something like doc.layer.move to the a new document. Something like the MoveItem.jsx in the Sample Scripts.
Copy link to clipboard
Copied
Anyone know how to make this work within macOS Sierra and Illustrator CC 2017?
Get ready! An upgraded Adobe Community experience is coming in January.
Learn more