I try to scale a bunch of svg file using adobe illustrator script written in jsx. The Illustrator crashes and a lot space on my drive is used. (i don't know where)
The Illustrator opens the first file. It stopped at after the number of pageItems were noted as an alrt May I ask for a hint. The following script i used:
var ziel44 = 124.72; // in pt
var ziel88 = 249.45; // in pt
var speicherPfad = "C:/Users/daso/Source/"
var folderPrefix = "C:/Users/daso/Target/"
var inputFolder = Folder.selectDialog(speicherPfad);
var files = inputFolder.getFiles("*.svg");
for (var i = 0; i < files.length; i++) {
var file = new File(files[i]);
try {
var doc = app.open(file);
doc.selection = null;
doc.selection = doc.pageItems;
app.executeMenuCommand('group'); // Gruppiere die Auswahl
var group = doc.selection[0];
var artboard = doc.artboards[0];
var rect = artboard.artboardRect; // [y1, x1, y2, x2]
var width = Math.abs(rect[2] - rect[0]);
var height = Math.abs(rect[1] - rect[3]);
var scaleFactor = 1;
if (height > width) {
if (height < 40) {
if (group.height > 0) {
scaleFactor = (ziel44 / group.height) * 100;
}
} else {
if (group.height > 0) {
scaleFactor = (ziel88 / group.height) * 100;
}
}
} else {
if (width < 40) {
if (group.width > 0) {
scaleFactor = (ziel44 / group.width) * 100;
}
} else {
if (group.width > 0) {
scaleFactor = (ziel88 / group.width) * 100;
}
}
}
alert("Skalierungsfaktor: " + scaleFactor);
group.resize(scaleFactor, scaleFactor);
app.executeMenuCommand('ungroup');
app.executeMenuCommand('fitArtboardToSelectedArt');
var saveFile = new File(speicherPfad + "/" + file.name);
var saveOptions = new IllustratorSaveOptions();
saveOptions.compatibility = Compatibility.ILLUSTRATOR29;
saveOptions.flattenOutput = OutputFlattening.PRESERVEAPPEARANCE;
doc.exportFile(saveFile, ExportType.SVG, new ExportOptionsSVG());
doc.close(saveOptions.DONOTSAVECHANGES);
$.sleep(1000);
} catch (e) {
alert("Fehler bei Datei: " + file.name + "\n" + e.message);
if (doc && !doc.closed) {
doc.close(SaveOptions.DONOTSAVECHANGES);
}
}
}
Too many errors to comment every ones. Here is the fixed code (if I understand your goal right):
var ziel44 = 124.72; // in pt
var ziel88 = 249.45; // in pt
var speicherPfad = "C:/Users/daso/Source"; // <-- no '/' at the end
var folderPrefix = "C:/Users/daso/Target"; // <-- target folder? no '/' at the end
var inputFolder = Folder.selectDialog(); // <-- '(speicherPfad)' doesn't make sense, see (*)
var files = inputFolder.getFiles("*.svg");
for (var i = 0; i < files.length; i++) {
var file = files[i]; // <-- doesn't need 'new File(files[i])', files[i] already a File
try {
var doc = app.open(file);
doc.selection = null;
// doc.selection = doc.pageItems; // <-- it doesn't work
app.executeMenuCommand('selectall');
app.executeMenuCommand('group'); // Gruppiere die Auswahl
var group = doc.selection[0];
var artboard = doc.artboards[0];
var rect = artboard.artboardRect; // [y1, x1, y2, x2]
var width = Math.abs(rect[2] - rect[0]);
var height = Math.abs(rect[1] - rect[3]);
var scaleFactor = 1;
if (height > width) {
if (height < 40) {
if (group.height > 0) {
scaleFactor = (ziel44 / group.height) * 100;
}
} else {
if (group.height > 0) {
scaleFactor = (ziel88 / group.height) * 100;
}
}
} else {
if (width < 40) {
if (group.width > 0) {
scaleFactor = (ziel44 / group.width) * 100;
}
} else {
if (group.width > 0) {
scaleFactor = (ziel88 / group.width) * 100;
}
}
}
alert("Skalierungsfaktor: " + scaleFactor);
group.resize(scaleFactor, scaleFactor);
app.executeMenuCommand('ungroup');
// app.executeMenuCommand('fitArtboardToSelectedArt'); // <-- it doesn't work
doc.artboards[0].artboardRect = doc.visibleBounds;
var saveFile = new File(folderPrefix + "/" + file.name); // <-- ? target folder 'folderPrefix' insteat of 'speicherPfad'
// var saveOptions = new IllustratorSaveOptions(); // <-- not used variable
// saveOptions.compatibility = Compatibility.ILLUSTRATOR29; // <-- not used
// saveOptions.flattenOutput = OutputFlattening.PRESERVEAPPEARANCE; // <-- not used
doc.exportFile(saveFile, ExportType.SVG, new ExportOptionsSVG());
doc.close(SaveOptions.DONOTSAVECHANGES); // <-- saveOptions with captal 'S', not critical, but becomes critical since you redefined 'saveOptions' variable earlier
$.sleep(1000);
} catch (e) {
alert("Fehler bei Datei: " + file.name + "\n" + e.message);
if (doc && !doc.closed) {
doc.close(SaveOptions.DONOTSAVECHANGES);
}
}
}
(*) — If you want to open the dialog at some predefined folder you can use this trick:
var speicherPfad = Folder("C:/Users/daso/Source");
var inputFolder = speicherPfad.selectDlg();