This repository has been archived by the owner on Apr 29, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 17
/
SaveDocsAsSVG.jsx
130 lines (107 loc) · 3.7 KB
/
SaveDocsAsSVG.jsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
/**********************************************************
ADOBE SYSTEMS INCORPORATED
Copyright 2005-2006 Adobe Systems Incorporated
All Rights Reserved
NOTICE: Adobe permits you to use, modify, and
distribute this file in accordance with the terms
of the Adobe license agreement accompanying it.
If you have received this file from a source
other than Adobe, then your use, modification,
or distribution of it requires the prior
written permission of Adobe.
*********************************************************/
/** Saves every document open in Illustrator
as an SVG file in a user specified folder.
*/
// Main Code [Execution of script begins here]
// uncomment to suppress Illustrator warning dialogs
// app.userInteractionLevel = UserInteractionLevel.DONTDISPLAYALERTS;
try {
if (app.documents.length > 0 ) {
// Get the folder to save the files into
var destFolder = null;
destFolder = Folder.selectDialog( 'Select folder for SVG files.', '~' );
if (destFolder != null) {
var options, targetFile;
// Get the SVG options to be used.
options = this.getOptions();
// You can tune these by changing the code in the getOptions() function.
var doc_count = 0;
while (app.documents.length) {
var aiDocument = app.activeDocument;
// Get the file to save the document as SVG into
targetFile = this.getTargetFile(aiDocument.name, '.svg', destFolder);
// Save as SVG
aiDocument.exportFile(targetFile, ExportType.SVG, options);
// Note: the doc.exportFile function for SVG is actually a Save As
// operation rather than an Export, that is, the document's name
// in Illustrator will change to the result of this call.
doc_count ++;
// Close current document
aiDocument.close(SaveOptions.DONOTSAVECHANGES);
}
alert( doc_count + ' documents saved as SVG to\r' + destFolder );
}
}
else{
throw new Error('There are no document open!');
}
}
catch(e) {
alert( e.message, "Script Alert", true);
}
/** Returns the options to be used for the generated files.
@return ExportOptionsSVG object
*/
function getOptions() {
// Create the required options object
var options = new ExportOptionsSVG();
// See ExportOptionsSVG in the JavaScript Reference for available options
// Set the options you want below:
options.compressed = false;
options.coordinatePrecision = 2;
options.cssProperties = SVGCSSPropertyLocation.PRESENTATIONATTRIBUTES;
options.documentEncoding = SVGDocumentEncoding.UTF8;
options.DTD = SVGDTDVersion.SVG1_1;
options.embedRasterImages = false;
options.fontSubsetting = SVGFontSubsetting.None;
options.fontType = SVGFontType.OUTLINEFONT;
options.includeFileInfo = false;
options.includeUnusedStyles = false;
options.includeVariablesAndDatasets = false;
options.optimizeForSVGViewer = false;
options.preserveEditability = false;
options.saveMultipleArtboards = false;
options.slices = false;
options.sVGAutoKerning = false;
options.sVGTextOnPath = false;
return options;
}
/** Returns the file to save or export the document into.
@param docName the name of the document
@param ext the extension the file extension to be applied
@param destFolder the output folder
@return File object
*/
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;
}