Skip to main content
Participant
March 1, 2023
Question

Illustrator scripting

  • March 1, 2023
  • 2 replies
  • 733 views

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

This topic has been closed for replies.

2 replies

pixxxelschubser
Community Expert
Community Expert
March 7, 2023

@Victor28646481rho2 

only one note:

a forward loop is never a good choice if you want to remove items.

 

Removing items breaks the loop. That's why better loop backwards (i--) in such cases.

m1b
Community Expert
Community Expert
March 1, 2023

Hi @Victor28646481rho2, can you post a sample file? Save as pdf with Illustrator editing capabilities and attach to a post.

- Mark

Participant
March 1, 2023

Hello, this is a typical file we could get before treatment : different lines strokes, probably some superposed lines etc

Thank you for your interest about my issue

Victor

m1b
Community Expert
Community Expert
March 7, 2023

Hi @Victor28646481rho2, I had a quick look at your sample file and, here are the first things I noticed:

 

1. If I followed your requirements manually to see what happens, the pathfinder "Outline" removes all strokes and colors, so there's no colors left to invert later on.

 

2. If I run your code the section called "Removing double lines" is problematic. Can you tell me exactly what you are trying to do here? You might need to be very specific. I might be good to post a simple sample file set up just for that section of the code.

 

- Mark