JavaScript to Proofread Text in a Illustrator File
Hi Folks,
I'm working on a JavaScript to proofread my text in the Illustrator file. here's the scenario…
1) I have a Text file (.txt) that contains all the text.
2) I have Finished Illustrator file with layout, graphics, text (including and excluding the text from the Text file).
3) Currently, as a QC, my manual process is like this:
a) Copy the text (sentence by sentence or Paragraph by Paragraph) from the Text file and search it in the Illustrator file;
b) If a text is found, then move to the second Sentence or Paragraph and so on;
c) If a text is not found in the Illustrator file, then paste that Sentence or Paragraph in a new Text file, and repeat process 2, until all the text is processed.
I want to accomplish the same with a JavaScript, where the script will read the text from the Text file and find it in the ai file, if found, then move to the second sentence or para, if not found then copy the not-found text in a new Text file.
Here's the script I'm trying to accomplish but I'm not getting the right results. My sample ai and Text files are here.
Can you please help me finished my task and feel free to improvise it to make it better.
// Function to read a local text file selected by the user
function readTextFile(file) {
var text = "";
var rawFile = new File(file);
rawFile.open("r");
text = rawFile.read();
rawFile.close();
return text;
}
// Function to show the dialog for file selection
function showDialog() {
var dialog = new Window("dialog", "Proof Reading…");
dialog.alignChildren = "fill";
dialog.alignChildren = "left";
// Panel for Text file selection
var textFilePanel = dialog.add("panel", undefined, "");
textFilePanel.add("statictext", undefined, "Select the TEXT File:");
textFilePanel.orientation = "row";
var textFilePathInput = textFilePanel.add("edittext", undefined, "", {
readonly: true
});
textFilePathInput.size = [302, 20];
var textFileBrowseBtn = textFilePanel.add("button", undefined, "Browse");
textFileBrowseBtn.onClick = function() {
var file = File.openDialog("Select the text file", "*.txt", false);
if (file) {
textFilePathInput.text = file.fsName;
}
};
// Dialog buttons
var buttonGroup = dialog.add("group");
buttonGroup.alignment = "center";
buttonGroup.add("button", undefined, "OK", { name: "ok" });
buttonGroup.add("button", undefined, "Cancel", { name: "cancel" });
if (dialog.show() === 1) {
// User clicked OK
return {
textFilePath: textFilePathInput.text
};
} else {
return null; // User clicked Cancel or closed the dialog
}
}
// Main function to search text in Illustrator
function searchAndVerifyText() {
// Show the file selection dialog
var filePaths = showDialog();
if (filePaths === null) {
alert("No file selected. Exiting script.");
return;
}
// Read the selected text file
var text = readTextFile(filePaths.textFilePath);
var paragraphs = text.split(/\r?\n\s*\r?\n/); // Split paragraphs by blank lines
// Check if there is an active Illustrator document
if (app.documents.length == 0) {
alert(
"No open Illustrator document found. Please open a document and try again."
);
return;
}
var doc = app.activeDocument;
// Create a new file for missing paragraphs
var missingTextFilePath = filePaths.textFilePath.replace(
".txt",
"_missing.txt"
);
var missingTextFile = new File(missingTextFilePath);
missingTextFile.open("w");
// Loop through each paragraph and search in Illustrator
for (var i = 0; i < paragraphs.length; i++) {
var paragraph = paragraphs[i];
// Ensure the paragraph is a string and trim it
if (typeof paragraph !== "string") {
paragraph = String(paragraph);
}
// Debugging output
$.writeln("Processing paragraph " + i + ": " + paragraph);
try {
paragraph = paragraph.trim();
} catch (e) {
$.writeln("Error trimming paragraph " + i + ": " + e.message);
continue; // Skip this paragraph if there's an error
}
if (paragraph === "") {
$.writeln("Paragraph " + i + " is empty, skipping.");
continue; // Skip empty lines
}
var found = false;
for (var j = 0; j < doc.textFrames.length; j++) {
var textFrame = doc.textFrames[j];
var textContent = textFrame.contents;
// Debugging output
$.writeln("Checking text frame " + j + " content: " + textContent);
if (textContent.indexOf(paragraph) !== -1) {
found = true;
$.writeln("Paragraph found in text frame " + j + ": " + paragraph);
break;
}
}
if (!found) {
$.writeln("Paragraph not found: " + paragraph);
missingTextFile.writeln(paragraph); // Write the missing paragraph to the new file
}
}
missingTextFile.close();
// Display the result
alert("Missing paragraphs saved in: " + missingTextFilePath);
}
// Run the verification
searchAndVerifyText();
