Skip to main content
Participating Frequently
April 2, 2023
Question

My script was working, but now it's broken!

  • April 2, 2023
  • 3 replies
  • 953 views

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!");

 

This topic has been closed for replies.

3 replies

rob day
Community Expert
Community Expert
April 3, 2023

You could wrap the place line in a try statement:

 

try {
    primaryTextFrame.place(xlsxFile, false);
}catch(e) {}  
Participating Frequently
April 3, 2023

Thanks!

When I do this, then it returns: 'No table found in the document'. I have attached two of the files that 'used to work' below if that helps. I know they worked, because I typeset these last week! But now the script isn't working on the same files.

INDT (Dropbox)

XLSX (Dropbox)

rob day
Community Expert
Community Expert
April 3, 2023

XLSX (Dropbox) is showing as deleted

brian_p_dts
Community Expert
Community Expert
April 2, 2023

Your getFiles function is probably returning a not real xslx. Maybe a hidden file, or another file that was saved with an xlsx extension or had .xlsx in its name. Or your Indd files file handling options changed so that you can no longer place xlsx files. Check your prefs, and debug in a clean folder.

Participating Frequently
April 3, 2023

Thanks, I've installed a new copy of Indesign CC, rerun the whole thing in a new folder, and tried it on a different computer. And the issue is now happening on that computer too 😞 I am certain I didn't change anything, but I guess things don't break themselves so I must have done something!!

James Gifford—NitroPress
Legend
April 2, 2023

Just an aside: using ChatGPT is fine. It might step on the toes of a few paid script consultants, but as Bobby D put it, the times they are a' changin'.

 

That said, my observation is that Chaggy writes awesome, mind-blowing, incredible code in a variety of fields and applications... that is either industrial-streamlined and thus a bit rough around the edges (as in writing a web page that meets the description... but an ugly, crude one visually)... or just has a broken bit or two requiring real, wetware expertise to make functional. Par'n me if I withhold my wild glee at the supposed demise of all coding.

 

Fortunately, there is some exceptional wetware intelligence here to help. (Not my area, but I'd bet on a fix by the end of today... and something Chaggy really should have caught, too. 🙂 )

 

Participating Frequently
April 3, 2023

I'm certainly glad for the generosity and ability of wetware intelligence!