Copy link to clipboard
Copied
I need to export multiple files as WOSVG (web optimized SVG).
This script, when run after opening a new instance of illustrator, produces an SVG file of 0 kb size the first time when run. (Empty, invalid file.)
If I leave the instance of Illustrator open, and run the same script again, it works fine. The file looks fine.
I altered the script to export as standard SVG (changing the export type and options) and this script performs fine in all cases. (New instance of illustrator or not.)
Am I doing something wrong with my approach to export as WOSVG?
#target illustrator
app.userInteractionLevel = UserInteractionLevel.DONTDISPLAYALERTS;
var sampleDWG = "myPath\\myCadfile.dwg";
processDrawing (sampleDWG);
function processDrawing(mySampleDWG){
openFile(mySampleDWG);
exportAsSVGWO();
}
function openFile(myFile){
var autoCADOpenOptions = app.preferences.AutoCADFileOptions;
var fileRef =myFile;
app.open(new File( fileRef, autoCADOpenOptions ));
}
function exportAsSVGWO(){
try {
if (app.documents.length > 0 ) {
// Get the folder to save the files into
var destFolder = null;
destFolder = "myfinalLocation\\";
if (destFolder != null) {
var options, i, sourceDoc, targetFile;
// Get the SVG options to be used.
options = this.getOptions();
for ( i = 0; i < app.documents.length; i++ ) {
sourceDoc = app.documents; // returns the document object
// Get the file to save the document as svg into
targetFile = this.getTargetFile(sourceDoc.name, '.svg', destFolder);
// Save as SVG
sourceDoc.exportFile(targetFile, ExportType.WOSVG, options); //works ok as standard svg
}
}
}
else{
throw new Error('There are no documents open!');
}
}
catch(e) {
alert( e.message, "Script Alert", true);
}
}
/** Returns the options to be used for the generated files.
@return ExportOptionsSVG object
*/
function getOptions()
{
//Works ok as standard SVG
//var options = new ExportOptionsSVG();
var options = new ExportOptionsWebOptimizedSVG();
options.svgMinify= true;
options.coordinatePrecision=4;
options.rasterImageLocation=RasterImageLocation.EMBED;
options.svgId=SVGIdType.SVGIDMINIMAL;
options.artboardRange=1;
return options;
}
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;
}
Copy link to clipboard
Copied
Try by removing the lines dealing with creating a file with File.open("w").
Copy link to clipboard
Copied
Hi Silly V. Thanks for taking the time to look at this.
When I comment out that section, the files do not save at all. Everything seems to work, it just doesn't write the file to destination.
I was not expecting the file to not write AT ALL when removing that section. I thought that part only checked if a file was open.
Edit - On second run through without restarting AI, the script works.
Why will it not work the first time through?
Copy link to clipboard
Copied
change this line:
targetFile = this.getTargetFile(sourceDoc.name, '.svg', destFolder);
to this:
targetFile = File(destFolder + "/" + sourceDoc.name.replace(/\..+&/, '') + ".svg");
Not sure why it doesn't work on first run - rather I'm not sure why it ​does work on the second run! haha!
Copy link to clipboard
Copied
Unfortunately, still does not work. Same behavior.
It will work if I don't close illustrator and run the script a second time.
If I open an autocad file first, then run a script that only exports as WOSVG, it works.
It seems that scripting a file to open, then calling the export as SVG in same script will fail the first time through.
Copy link to clipboard
Copied
Oh, maybe it's cause the file opening line is messed up. Disregard my previous advice!
app.open(new File( fileRef, autoCADOpenOptions ));
This open command takes the file object, color space, and options.
So it should be app.open(File(fileRef), undefined, autoCADOpenOptions);
Try this now.
Copy link to clipboard
Copied
Silly V - Still same behavior with the changed open command. It fails on first try, then runs on the second try as long as I don't close illustrator.
Copy link to clipboard
Copied
I made a couple of changes such as file paths to my paths, the sample file to an .ai file and no using open options arguments, and I put the entire script into a wrapper function. The "this." were removed from the function call lines. It worked on the first try.
Also, I am on a Mac
Copy link to clipboard
Copied
Hi Silly,
Can you post your code you used?
Here is what I have just tried and it has the same behavior. It works only on the second attempt with same session of Illustrator. (I am on windows 10.)
#target illustrator
app.userInteractionLevel = UserInteractionLevel.DONTDISPLAYALERTS;
function wrapperFunc(){
var sampleDWG = "\\myPath\\arch_5_mod.ai";
processDrawing (sampleDWG);
function processDrawing(mySampleDWG){
openFile(mySampleDWG);
exportAsSVGWO();
app.activeDocument.close(SaveOptions.DONOTSAVECHANGES);
}
function openFile(myFile){
var autoCADOpenOptions = app.preferences.AutoCADFileOptions;
var fileRef =myFile;
app.open(new File( fileRef, autoCADOpenOptions ));
}
function exportAsSVGWO(){
try {
if (app.documents.length > 0 ) {
// Get the folder to save the files into
var destFolder = null;
destFolder = "myPath\\svgFinal\\";
if (destFolder != null) {
var options, i, sourceDoc, targetFile;
// Get the SVG options to be used.
options = getOptions();
for ( i = 0; i < app.documents.length; i++ ) {
sourceDoc = app.documents; // returns the document object
// Get the file to save the document as svg into
targetFile = getTargetFile(sourceDoc.name, '.svg', destFolder);
// Save as SVG
sourceDoc.exportFile(targetFile, ExportType.WOSVG); //works ok as standard svg
}
}
}
else{
throw new Error('There are no documents open!');
}
}
catch(e) {
alert( e.message, "Script Alert", true);
}
}
/** Returns the options to be used for the generated files.
@return ExportOptionsSVG object
*/
function getOptions()
{
//Works ok as standard SVG
//var options = new ExportOptionsSVG();
var options = new ExportOptionsWebOptimizedSVG();
options.svgMinify= true;
options.coordinatePrecision=4;
options.rasterImageLocation=RasterImageLocation.EMBED;
options.svgId=SVGIdType.SVGIDMINIMAL;
options.artboardRange=1;
return options;
}
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;
}
}
wrapperFunc();
Copy link to clipboard
Copied
you still have the "app.open(new File( fileRef, autoCADOpenOptions ));" . That 2nd argument does not go into a File constructor, and it goes in the 3rd spot if needed, inside the app.open arguments.
Copy link to clipboard
Copied
I changed it to: app.open(File( fileRef )); and still same issue.
Copy link to clipboard
Copied
Oh you know what else, I only used forward-slashes in my file paths. The way you have them, the line destFolder + '/' + newName would then translate to "\myPath\svgFinal/fileName.svg" , which may mess it up too.
Copy link to clipboard
Copied
Hmm, when I debug and display the values for myFile, it seems ok
This also showed that I was incorrectly using a for loop around line 35 of my original code at top.
I removed the for loop in favor of sourceDoc = app.activeDocument;.
Thanks again for taking your time to look at this. It's much appreciated.
Copy link to clipboard
Copied
So it works now? What did it say about the for-loop?
Copy link to clipboard
Copied
No, still does not work. Same behavior.
Sorry for confusion. I meant the values of the variable myFile is ok, in response to your question about the construction of that path.
I mentioned that I removed the for loop in my original code at the top so that sourceDoc = app.activeDocument.
Copy link to clipboard
Copied
Hi, it's better using simple snippet for debug.
#target illustrator
app.userInteractionLevel = UserInteractionLevel.DONTDISPLAYALERTS;
var sampleDWG = "myPath\\myCadfile.dwg";
processDrawing(sampleDWG);
function processDrawing(mySampleDWG) {
exportAsSVGWO(openFile(mySampleDWG));
}
function openFile(myFile) {
var autoCADOpenOptions = app.preferences.AutoCADFileOptions;
return app.open(new File(myFile));
}
function exportAsSVGWO(doc) {
// Get the folder to save the files into
var destFolder = "myfinalLocation\\";
if (destFolder != null) {
var options = getOptions(),
targetFile = destFolder + doc.name.replace(/(\w*)(\.\w*)?$/, '$1.svg');
doc.exportFile(new File(targetFile), ExportType.WOSVG, options);
doc.close(SaveOptions.DONOTSAVECHANGES);
}
}
/** Returns the options to be used for the generated files.
@return ExportOptionsSVG object
*/
function getOptions() {
var options = new ExportOptionsWebOptimizedSVG();
options.svgMinify = true;
options.coordinatePrecision = 4;
options.rasterImageLocation = RasterImageLocation.EMBED;
options.svgId = SVGIdType.SVGIDMINIMAL;
options.artboardRange = 1;
return options;
}
Copy link to clipboard
Copied
Thanks moluapple. Your snippet is definitely more efficient for debugging. I wish I could change my original post to this snippet.
I was able to run your snippet and it has the same behavior. It runs but does not produce output the first time (With a new session of illustrator) but second time I run it with an illustrator session, it works.
Again, after changing the export format to standard SVG, the code runs as expected without any issues.
Copy link to clipboard
Copied
My code which runs first time as expected on my Mac:
#target illustrator
function test(){
app.userInteractionLevel = UserInteractionLevel.DONTDISPLAYALERTS;
// var sampleDWG = "myPath\\myCadfile.dwg";
var sampleDWG = "/Users/Me/Desktop/Test Sample File.ai";
processDrawing(sampleDWG);
function processDrawing(mySampleDWG) {
openFile(mySampleDWG);
exportAsSVGWO();
}
function openFile(myFile) {
var autoCADOpenOptions = app.preferences.AutoCADFileOptions;
var fileRef = myFile;
app.open(new File(fileRef));
}
function exportAsSVGWO() {
try {
if (app.documents.length > 0) {
// Get the folder to save the files into
var destFolder = null;
destFolder = Folder("/Users/me/Desktop/");
if (destFolder != null) {
var options, i, sourceDoc, targetFile;
// Get the SVG options to be used.
options = getOptions();
for (i = 0; i < app.documents.length; i++) {
sourceDoc = app.documents; // returns the document object
// Get the file to save the document as svg into
targetFile = getTargetFile(sourceDoc.name, '.svg', destFolder);
// Save as SVG
sourceDoc.exportFile(targetFile, ExportType.WOSVG, options); //works ok as standard svg
}
}
} else {
throw new Error('There are no documents open!');
}
} catch (e) {
alert(e.message, "Script Alert", true);
}
}
/** Returns the options to be used for the generated files.
@return ExportOptionsSVG object
*/
function getOptions() {
//Works ok as standard SVG
//var options = new ExportOptionsSVG();
var options = new ExportOptionsWebOptimizedSVG();
options.svgMinify = true;
options.coordinatePrecision = 4;
options.rasterImageLocation = RasterImageLocation.EMBED;
options.svgId = SVGIdType.SVGIDMINIMAL;
options.artboardRange = 1;
return options;
}
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;
}
};
test();
Copy link to clipboard
Copied
Silly-
Thanks again for your time in looking at this.
Unfortunately this doesn't work for me on windows 10. It still produces a file of 0 kb the first time through on a new instance of Adobe Illustrator and then works correctly the second time. (I need it to work on the first try of a new instance of AI as this will be part of an automated/scheduled task.)
My only change of your code was to adjust my file paths.
Again, everything works fine for export as standard SVG.
Is it possible the export as Web Optimized SVG is bugged for Windows 10?
Copy link to clipboard
Copied
Please allow me to comment on the old agenda.
I have also observed the phenomenon that 0KB SVG is generated, but after leaving it for a few minutes, a normally completed file is generated.
This can simply take an enormous amount of time to generate the file.
Compared to manually exporting, the data capacity of SVG that was automatically output even with the same data was several tens of times larger.
Is it possible that the PC simply takes too long to generate the file?
Copy link to clipboard
Copied
The above was at a time when I was not aware of or didn't pay attention to the fact that SVG exports in Illustrator are asynchronous and the saves of these files act a little differently from normal saving. A common theme is how the scripts tend to continue despite the fact that the file in question is still being written, and this process is not blocked as it is for all the other exports. Strangely I cannot find anything on the subject via search, but the creation of a 'placeholder' file at 0KB and then following some time later the appearance of a full file leads me to believe there is such an issue happening. I am not sure if I remember what posts regarding this said, but there are topics on "svg export is too slow".