Script to change line weight and drawing order in PDFs
Hello everyone. I post here in the hope that someone can help me with a script.
I have a number of PDFs to process and would like to take advantage of illustrator's scripting capabilities to automate certain operations that I currently have to perform manually for each file, but I'm a novice and the script I've tried doesn't work!
The operations I do are not particularly complex and I believe it could be effectively automated, currently I have to:
-open the pdf contained in a certain folder
-select a line with a certain thickness (0.24pt)
-select same stroke weight
-switch all selected lines to 0.14pt.
-select a line with stroke colour rgb 0,0,0
-arrangement>bring to front.
-select a line with stroke rgb 191,0,0
-arrangement>bring to front
-save and go to the next pdf.
Here is the script I tried to implement.
When charged, it correctly prompts the user for folder selection. Then, even if the folder contains only one file, Illustrator gets stuck "thinking" for several minutes.
// Choose a folder which contains PDF documents
var folder = Folder.selectDialog("Select folder");
if (folder != null) {
var pdfFiles = folder.getFiles("*.pdf");
for (var i = 0; i < pdfFiles.length; i++) {
var pdfFile = pdfFiles[i];
var doc = app.open(pdfFile);
// Select all lines and apply modifications if conditions are met
var lines = doc.pathItems;
for (var j = 0; j < lines.length; j++) {
var line = lines[j];
if (line.stroked && line.strokeWidth == 0.24) {
line.strokeWidth = 0.14;
}
var rgbColor = line.strokeColor;
if (rgbColor.red == 191 && rgbColor.green == 0 && rgbColor.blue == 0) {
line.zOrder(ZOrderMethod.BRINGTOFRONT);
} else if (rgbColor.red == 0 && rgbColor.green == 0 && rgbColor.blue == 0) {
line.zOrder(ZOrderMethod.BRINGTOFRONT);
}
}
// Save modified PDF
var saveOptions = new PDFSaveOptions();
var saveFile = new File(pdfFile.path + "/" + pdfFile.name.replace(".pdf", "_modified.pdf"));
doc.saveAs(saveFile, saveOptions);
doc.close(SaveOptions.DONOTSAVECHANGES);
}
alert("Operation completed.");
} else {
alert("No folder selected. The script will be interrupted");
}
Does anyone have any idea what the problem might be?
Thanks in advance for your support
