Hello everyone,
I created the script that import multiple artwork based on CSV data. Currently it's trying to import 7 art files and file size range from 45kb - 500kb. My plan is to import 20-30 art files to create base template. When I run script my illustrator froze, and goes into not responding mode until script is completed (which is not a big issue), main issue is it's missing every second artwork, but when run in debug mode going all the steps one by one then all the artwork is imported. I tried to put $.sleep(), but it's not fixing the problem. And sometimes it misses two artworks in a row.
My debug console shows that script is running every step, but not importing actual artwork. If someone can review and let me know how to fix this issue that would be really helpful, here's my script:
var basePath = "C:/Users/hbansal/Downloads/Scripting/Adobe Scripting/Base Templates/"; // file location for all base Bus templates artworks
var basePathComponents = "C:/Users/hbansal/Downloads/Scripting/Adobe Scripting/Bus Compenents/"; // file location for all the Bus Components artworks
// Function to read values from CSV and store them in an array
var csvFilePath = "C:/Users/hbansal/Downloads/Configuration Values.CSV";
var csvFile = new File(csvFilePath);
if (!csvFile.exists) {
alert("CSV file not found!");
exit();
}
csvFile.open('r'); // Open the CSV file for reading
var csvContent = csvFile.read(); // Read the entire file content
csvFile.close();
var csvLines = csvContent.split("\n"); // Split content by line (each line is a row)
var dataArray = [];
// Loop through each row starting from row 2 (row 1 contains headers)
for (var i = 1; i < csvLines.length; i++) {
var row = csvLines[i];
if (row.length === 0) continue; // Skip empty rows
var rowData = row.split(","); // Split the row into columns
dataArray.push(rowData); // Store the row data in the array
}
// Function to perform the artworkFileName() with each value update
function processArtworkWithCSVValues() {
// Loop through the array and perform actions for each row
for (var i = 0; i < dataArray.length; i++) {
var rowData = dataArray[i];
// Log for debugging purposes
$.writeln("Processing Row " + (i + 2) + ": " + rowData);
var componentFolder = rowData[0]; // Column A
var artworkLayer = rowData[1]; // Column B
var artworkConfigurationPrefix = rowData[3]; // Column D
var missingArtworkValues = rowData[4]; // Column E
artworkFileName(artworkConfigurationPrefix, componentFolder, artworkLayer, missingArtworkValues);
}
}
function findMatchingArtworkFile(folder, fileNameRegex) {
// Get all files in the folder
var files = folder.getFiles();
// Iterate through files
for (var i = 0; i < files.length; i++) {
var file = files[i];
// Check if the current file is a File and matches the regex
if (file instanceof File && fileNameRegex.test(file.name)) {
return file.fsName; // Return the file path and exit the function
}
}
// Return null if no matching file is found
return null;
}
function artworkFileName(artworkConfigurationPrefix, componentFolder, artworkLayer, missingArtworkValues) {
var filePrefix = artworkConfigurationPrefix; // Construct the prefix of the filename based on the values
var fileNameRegex = new RegExp("^" + filePrefix); // Regular expression to match the file name prefix
var folder = new Folder(basePathComponents + componentFolder); // Folder containing the artwork files
var filePath = findMatchingArtworkFile(folder, fileNameRegex); // Call the findMatchingArtworkFile function to get the file path
if (filePath) {
// Import and paste artwork logic
var importFile = new File(filePath);
if (importFile.exists) {
var externalDoc = app.open(importFile); // Open the file
var externalLayer = externalDoc.activeLayer; // Get the active layer of the external document
externalLayer.hasSelectedArtwork = true; // Select all content in the external document
app.copy(); // Copy the selected content
externalDoc.close(SaveOptions.DONOTSAVECHANGES);// Close the external document without saving changes
var startTime = new Date().getTime();
var layer = app.activeDocument.layers[artworkLayer]; // Switch to the appropriate layer in the active document
app.activeDocument.activeLayer = layer;
}
// Paste the artwork
app.executeMenuCommand("pasteInPlace");
app.redraw();
} else {
storeMissingArtworkDetails(artworkLayer, null, missingArtworkValues);
}
}
processArtworkWithCSVValues();
alert("IMPORT COMPLETED");