Illustrator scripting
Hello everyone,
I'm actually trying to make a script for batch treating some pdfs in a folder.
It's for preparing files to be laser cut.
For having a usable file for the laser cutter I need to do several actions on my pdfs :
first removing all the clipping mask,
then using pathfinder "outline" for getting rid of double lines,
and finally set all the strokes to 0.01pt.
After that i need to export a first time in pdf,
and then I need to invert blue and green lines and export again.
This is my code for the moment :
var doc = app.activeDocument;
// Removing clipping mask
for (var i = 0; i < doc.pageItems.length; i++) {
var pageItem = doc.pageItems[i];
if (pageItem.clipping) {
pageItem.clipping = false;
pageItem.move(doc.layers[0], ElementPlacement.PLACEATBEGINNING);
}
}
// Removing double lines
for (var i = 0; i < doc.pathItems.length; i++) {
var pathItem = doc.pathItems[i];
if (pathItem.strokeWidth > 0) {
var paths = pathItem.pathItems;
var newPath = paths[0].duplicate();
for (var j = 1; j < paths.length; j++) {
newPath = newPath.join(paths[j]);
}
newPath.strokeWidth = pathItem.strokeWidth;
newPath.strokeColor = pathItem.strokeColor;
newPath.strokeCap = pathItem.strokeCap;
newPath.strokeJoin = pathItem.strokeJoin;
newPath.strokeDashes = pathItem.strokeDashes;
pathItem.remove();
}
}
// Changing strokes to 0.01
for (var i = 0; i < doc.pathItems.length; i++) {
var pathItem = doc.pathItems[i];
pathItem.strokeWidth = 0.01;
}
// First pdf export
var pdfExportOpts = new PDFSaveOptions();
pdfExportOpts.pDFPreset = "High Quality Print";
pdfExportOpts.artboardRange = "1";
doc.saveAs(new File("~/Desktop/my_Doc.pdf"), pdfExportOpts);
// Turning blue lines purple and green lines blue
for (var i = 0; i < doc.pathItems.length; i++) {
var pathItem = doc.pathItems[i];
if (pathItem.strokeColor && pathItem.strokeColor.typename == "RGBColor") {
var strokeColor = pathItem.strokeColor;
if (strokeColor.red == 0 && strokeColor.green == 0 && strokeColor.blue == 255) {
strokeColor.red = 238;
strokeColor.green = 130;
strokeColor.blue = 238;
} else if (strokeColor.red == 0 && strokeColor.green == 128 && strokeColor.blue == 0) {
strokeColor.red = 0;
strokeColor.green = 0;
strokeColor.blue = 255;
}
}
}
// Turning purple lines green
for (var i = 0; i < doc.pathItems.length; i++) {
var pathItem = doc.pathItems[i];
if (pathItem.strokeColor && pathItem.strokeColor.typename == "RGBColor") {
var strokeColor = pathItem.strokeColor;
if (strokeColor.red == 238 && strokeColor.green == 130 && strokeColor.blue == 238) {
strokeColor.red = 0;
strokeColor.green = 128;
strokeColor.blue = 0;
}
}
}
// Second pdf export
var pdfExportOpts2 = new PDFSaveOptions();
pdfExportOpts2.pDFPreset = "High Quality Print";
pdfExportOpts2.artboardRange = "1";
doc.saveAs(new File("~/Desktop/my_Doc2.pdf"), pdfExportOpts2);
I'm a real beginner in scripting, and actually I always have the same strange error when I try a script in illustrator else than the given sample (the pdfexport one work for exemple) :
It's juste show an alert sign in illustrator and doesn't launch anything.
I first tried using applescript instead of javascript but even this really basic as code, given in the "cs5 scripting guide documentation" doesn't work:
--Send the following commands to Illustrator
tell application "Adobe Illustrator"
--Create a new document
set docRef to make new document
--Create a new text frame with the string "Hello World"
set textRef to make new text frame in docRef ?
with properties {contents: "Hello World!", position:{200, 200}}
end tell
Could anyone please help me to fix my script or at least understand how to launch properly the most basic hello world in illustrator or something like that, because I spend several hours today trying to figure out how to make this work and I really don't understand ...
Thanks all,
Victor from France
