Copy link to clipboard
Copied
Greetings,
Can anyone provide any help with correcting the bugs in JavaScript I got from ChatGPT?
Thanks for any help on this 🙂
I asked chatGPT the following:
write me a script for adobe illustrator that will:
1. batch open dxf files at Original Size, press OK on the DXF/DWG Options UI
2. Change the Document Color Mode to CMYK
3. Create color swatch with the following values: Swatch Name: CutContour, Color Type: Spot Color, Color Mode: CMYK, C= 0% M=100% Y=0% K=0%
4. change the color of all objects on Layer 1 on Artboard 1 to the Swatch Name: CutContour
5. change the stroke thickness to 0.1pt to all objects on Layer 1
6. Select all objects on Layer 1, add a check to the box for Overprint Stroke located on the Attributes panel
7. fit the artboard to the artwork, rename Layer 1 to CutContour on Artboard 1
8. add a 0.0785 inch bleed to the Document Setup
9. save a .AI file version in the same folder of the open dxf file and use the same exact name of the dxf file, but add to the end the width and height dimensions measured when selecting Layer 1 which was renamed to CutCounter. The dimensions added to the end should have the following format: " - [width] x [height]", the artboard dimensions used in the file name should be in inches to the 4th decimal place.
10. save a .PDF version of the .AI file also. The .PDF version should also have layers. Uncheck the export option for "View PDF after Saving"
11. close the file then move on to the next file and repeat the steps until all selected dxf files are processed.
The script runs, but has the following bugs:
#target illustrator
// Function to set the document color mode to CMYK
function setDocumentToCMYK(doc) {
if (doc.documentColorMode !== DocumentColorSpace.CMYK) {
doc.documentColorMode = DocumentColorSpace.CMYK;
}
}
// Function to create the CutContour spot color swatch if it doesn’t exist
function createCutContourSwatch(doc) {
var swatchName = "CutContour";
var spotColor;
try {
spotColor = doc.spots.getByName(swatchName);
} catch (e) {
// If the swatch does not exist, create it
spotColor = doc.spots.add();
spotColor.name = swatchName;
var cmykColor = new CMYKColor();
cmykColor.cyan = 0;
cmykColor.magenta = 100;
cmykColor.yellow = 0;
cmykColor.black = 0;
spotColor.color = cmykColor;
spotColor.colorType = ColorModel.SPOT;
}
return new SpotColor().spot = spotColor;
}
// Function to process each file
function processFile(dxfFile) {
app.open(dxfFile);
var doc = app.activeDocument;
// Step 2: Set the document to CMYK color mode
setDocumentToCMYK(doc);
// Step 3: Create and apply CutContour spot color
var cutContourColor = createCutContourSwatch(doc);
// Step 4: Apply CutContour color to all objects on Layer 1
var layer = doc.layers[0];
for (var i = 0; i < layer.pageItems.length; i++) {
layer.pageItems[i].fillColor = cutContourColor;
layer.pageItems[i].strokeColor = cutContourColor;
}
// Step 5: Set stroke thickness to 0.1pt for all objects on Layer 1
for (var j = 0; j < layer.pageItems.length; j++) {
layer.pageItems[j].strokeWidth = 0.1;
}
// Step 6: Set Overprint Stroke for all objects on Layer 1
for (var k = 0; k < layer.pageItems.length; k++) {
layer.pageItems[k].strokeOverprint = true;
}
// Step 7: Fit the artboard to artwork bounds and rename Layer 1
doc.artboards[0].artboardRect = doc.visibleBounds;
layer.name = "CutContour";
// Step 8: Add a 0.0785 inch bleed to Document Setup (convert to points)
var bleedOffset = 0.0785 * 72;
doc.documentBleedOffsets = [bleedOffset, bleedOffset, bleedOffset, bleedOffset];
// Step 9: Save .AI file with updated name
var artboardWidth = (doc.artboards[0].artboardRect[2] - doc.artboards[0].artboardRect[0]) / 72;
var artboardHeight = (doc.artboards[0].artboardRect[1] - doc.artboards[0].artboardRect[3]) / 72;
var widthStr = artboardWidth.toFixed(4);
var heightStr = artboardHeight.toFixed(4);
var outputFileName = dxfFile.name.replace(/\.dxf$/i, "") + " - " + widthStr + " x " + heightStr;
var aiOutputFile = new File(dxfFile.parent.fsName + "/" + outputFileName + ".ai");
var aiSaveOptions = new IllustratorSaveOptions();
aiSaveOptions.compatibility = Compatibility.ILLUSTRATOR16;
// Save as .AI file
doc.saveAs(aiOutputFile, aiSaveOptions);
// Step 10: Save as PDF with layers, do not view PDF after saving
var pdfOutputFile = new File(dxfFile.parent.fsName + "/" + outputFileName + ".pdf");
var pdfSaveOptions = new PDFSaveOptions();
pdfSaveOptions.preserveEditability = true; // Preserve AI layers
pdfSaveOptions.compatibility = PDFCompatibility.ACROBAT5;
pdfSaveOptions.viewPDF = false; // Uncheck "View PDF after Saving"
// Save the PDF
doc.saveAs(pdfOutputFile, pdfSaveOptions);
// Step 11: Close the file
doc.close(SaveOptions.DONOTSAVECHANGES);
}
// Prompt user to select multiple DXF files
var selectedFiles = File.openDialog("Select DXF files to process", "*.dxf", true);
if (selectedFiles) {
for (var i = 0; i < selectedFiles.length; i++) {
processFile(selectedFiles[i]);
}
alert("Batch processing complete!");
} else {
alert("No files selected.");
}
Copy link to clipboard
Copied
There is no command in ExtendScript to set bleed in Document Setup. Task 8 in your list is problematic.
Copy link to clipboard
Copied
Thank you for the insight.
Copy link to clipboard
Copied
You can probably imagine that not all script developers in this forum are overly enthused about revamping something AI generated that – according to your error list – almost does not work at all.
You may perseveringly grapple with your AI system until it provides something useful. Or you may consider to hire a human script developer to see if your goals can be achieved. For example, hiring Sergey Osokin would certainly be a good choice.
Copy link to clipboard
Copied
ChatGPT's typos are easy to find in this code, so they are fairly easy to fix. I found all the typos yesterday. Most of the code ChatGPT wrote will work. However, Adobe does not provide access to the Document Setup. Writing VBScript (PC only) to change the document setup is no fun.