My script was working, but now it's broken!
I had some code written in ChatGPT (sorry if you don't like this, but I can't afford a custom made script at the moment) to take all xlsx files in a folder and place then in an indt. It then runs a few other functions on which I need done to every file. The script was working nicely and saving me LOTS of time, but this evening I suddenly got an error and it won't work even when I roll back InDesign to a previous edition. Any help would be appreciated 🙂
The error:
JavaScript Error!
Error Number: 29441 Error String: Cannot place this file. No filter found for requested operation.
Engine: main File: /Users/.../Library/Preferences/Adobe InDesign/ Version 18.0/en_GB/Scripts/Scripts Panel/6x9.jsx Line: 78 Source: primaryTextFrame.place(xlsxFile, false);
My code:
// Load the necessary script libraries
app.scriptPreferences.userInteractionLevel = UserInteractionLevels.INTERACT_WITH_ALL;
// Select folder containing the xlsx files
var sourceFolder = Folder.selectDialog("Select the folder containing the xlsx files:");
if (sourceFolder === null) {
alert("No folder selected. Exiting...");
exit();
}
// Select folder to save the indd files
var destinationFolder = Folder.selectDialog("Select the folder to save the indd files:");
if (destinationFolder === null) {
alert("No folder selected. Exiting...");
exit();
}
// Select the indt file
var templateFile = File.openDialog("Select the indt template file:", "*.indt");
if (templateFile === null) {
alert("No template file selected. Exiting...");
exit();
}
// Function to find a table in a story
function findTableInStory(story) {
for (var i = 0; i < story.tables.length; i++) {
var table = story.tables[i];
if (table.isValid) {
return table;
}
}
return null;
}
// Function to apply GREP styles
function applyGrepStyle(grepExpression, paragraphStyleName, story) {
app.findGrepPreferences = NothingEnum.nothing;
app.changeGrepPreferences = NothingEnum.nothing;
app.findGrepPreferences.findWhat = grepExpression;
app.changeGrepPreferences.appliedParagraphStyle = app.activeDocument.paragraphStyles.item(paragraphStyleName);
var results = story.changeGrep();
app.findGrepPreferences = NothingEnum.nothing;
app.changeGrepPreferences = NothingEnum.nothing;
return results;
}
// Function to clear all overrides in the table
function clearTableStyleOverrides(table) {
for (var i = 0; i < table.cells.length; i++) {
var cell = table.cells[i];
cell.clearCellStyleOverrides();
}
}
// Function to set row height of the table
function setTableRowHeight(table) {
for (var i = 0; i < table.rows.length; i++) {
var row = table.rows[i];
row.height = "1.058mm";
row.minimumHeight = "1.058mm";
}
}
// Iterate through the xlsx files in the source folder
var xlsxFiles = sourceFolder.getFiles("*.xlsx");
for (var i = 0; i < xlsxFiles.length; i++) {
var xlsxFile = xlsxFiles[i];
// Open the template file
var templateDoc = app.open(templateFile, true);
// Place the xlsx file in the primary text frame
if (templateDoc.textFrames.length > 0) {
var primaryTextFrame = templateDoc.textFrames[0];
primaryTextFrame.place(xlsxFile, false);
primaryTextFrame.textFramePreferences.verticalJustification = VerticalJustification.TOP_ALIGN;
} else {
alert("No text frame found in the template document.");
templateDoc.close(SaveOptions.NO);
continue;
}
// Apply table style and set column widths
var table = null;
for (var j = 0; j < templateDoc.stories.length; j++) {
var story = templateDoc.stories[j];
table = findTableInStory(story);
if (table !== null) {
break;
}
}
if (table !== null) {
clearTableStyleOverrides(table);
var tableStyle = templateDoc.tableStyles.item("Table Style");
if (tableStyle.isValid) {
table.appliedTableStyle = tableStyle;
// Set column widths
if (table.columns.length >= 3) {
table.columns[0].width = "58.2mm";
table.columns[1].width = "6mm";
table.columns[2].width = "58.2mm";
} else {
alert("Table has fewer than 3 columns.");
}
setTableRowHeight(table);
} else {
alert("Table style 'Table Style' not found in the template document.");
}
} else {
alert("No table found in the document.");
}
// Apply GREP styles
for (var j = 0; j < templateDoc.stories.length; j++) {
var story = templateDoc.stories[j];
applyGrepStyle('Primary .+? \\b([2-9]|[1-9][0-9]|1[0-4][0-9]|150)\\b', 'Book-Title-First-Language', story);
applyGrepStyle('Secondary .+? \\b([2-9]|[1-9][0-9]|1[0-4][0-9]|150)\\b', 'Book-Title-Second-Language', story);
applyGrepStyle('Primary .+? 1\\b', 'Book-Title-First-Language-Chapter-1', story);
applyGrepStyle('Secondary .+? 1\\b', 'Book-Title-Second-Language-Chapter-1', story);
}
// Save the new indd file with the same name as the xlsx file
var inddFileName = xlsxFile.name.replace(".xlsx", ".indd");
var inddFilePath = destinationFolder + "/" + inddFileName;
var inddFile = new File(inddFilePath);
templateDoc.save(inddFile);
}
alert("Process completed!");
