Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
0

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

Community Beginner ,
Apr 02, 2023 Apr 02, 2023

Copy link to clipboard

Copied

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

 

TOPICS
Scripting

Views

766
Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Apr 02, 2023 Apr 02, 2023

Copy link to clipboard

Copied

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. 🙂 )

 

Votes

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Apr 03, 2023 Apr 03, 2023

Copy link to clipboard

Copied

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

Votes

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Apr 02, 2023 Apr 02, 2023

Copy link to clipboard

Copied

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.

Votes

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Apr 03, 2023 Apr 03, 2023

Copy link to clipboard

Copied

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!!

Votes

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Apr 03, 2023 Apr 03, 2023

Copy link to clipboard

Copied

You could wrap the place line in a try statement:

 

try {
    primaryTextFrame.place(xlsxFile, false);
}catch(e) {}  

Votes

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Apr 03, 2023 Apr 03, 2023

Copy link to clipboard

Copied

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)

Votes

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Apr 03, 2023 Apr 03, 2023

Copy link to clipboard

Copied

XLSX (Dropbox) is showing as deleted

Votes

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Apr 03, 2023 Apr 03, 2023

Copy link to clipboard

Copied

This might work?

Votes

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Apr 03, 2023 Apr 03, 2023

Copy link to clipboard

Copied

I can’t place your XLSX files manually or via a script. If i export them from Numbers as XLS files they will place.

Votes

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Apr 03, 2023 Apr 03, 2023

Copy link to clipboard

Copied

That's odd. Excel files are not typically as fussy as Word files from alternate sources or conversions.

 

Votes

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Apr 03, 2023 Apr 03, 2023

Copy link to clipboard

Copied

This seems to have been the issue all along, I was using openpxyl to create my xlsx files which was apparently sometimes formatting ok, and other times producing a broken xlsx. I changed to xlsxwriter and all good (so far).

Votes

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Apr 03, 2023 Apr 03, 2023

Copy link to clipboard

Copied

Despite my above comment, any time you are working with files generated by a secondary app (even the Apple and Google clones), it's always wise to (1) note that in any solution seeking you post and (2) always suspect that app's file export as a primary cause of hassles.

 

There have been many discussions here that exhausted the suggested fixes until the OP belatedly notes that the "Word" or "Excel" or "SVG" or "PDF" file came from some wonky export process. 🙂

 

As a general rule, opening such files in the genuine app and resaving them under a different name often cleans them up to more standard form.

 

Votes

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Apr 05, 2023 Apr 05, 2023

Copy link to clipboard

Copied

LATEST
I guess I could just turn everything to CSV instead. Hopefully that will sort it 🙈

Votes

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Apr 05, 2023 Apr 05, 2023

Copy link to clipboard

Copied

Ok, I wonder why. I'll maybe try and remake the script I use to make the xlsx files. There must be something wrong with them.

Thanks for your help!

Votes

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines