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

In a script, I'm trying to assign a table style, but getting it by name doesn't work.

Engaged ,
May 30, 2025 May 30, 2025

The style exists, and if I examine it with .name ==, it matches.

 

But getByName does not. In the code below, it's getByName that's throwing an exception, but the part that raises the "style found" alert works:

 

function styleTable(theTable)
{
    var tableStyleName = "Section heading";
    theTable.cells.everyItem().clearCellStyleOverrides(true);
    for(i = 0; i < app.activeDocument.tableStyles.length; i++)
    {
        if(app.activeDocument.tableStyles[i].name == tableStyleName)
        {
            alert("Style found");
        }
    }

    try
    {
        var tableStyle = app.activeDocument.tableStyles.getByName(tableStyleName);
        theTable.appliedTableStyle = tableStyle;
    }
    catch(err)
    {
        alert("Table style " + tableStyleName + " not found.");
        return;
    }
}
tableStyleNoMatch.png
TOPICS
Scripting
416
Translate
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

correct answers 1 Correct answer

Mentor , May 30, 2025 May 30, 2025

Add the error string to your alert to learn more about the actual error.

Maybe itemByName() works better than getByName() ?

 

Translate
Mentor ,
May 30, 2025 May 30, 2025

Add the error string to your alert to learn more about the actual error.

Maybe itemByName() works better than getByName() ?

 

Translate
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
Engaged ,
May 30, 2025 May 30, 2025

Thanks. The error says "Table style app.activeDocument.tableStyles.getByName is not a function"

Translate
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 ,
May 30, 2025 May 30, 2025

@Thomas_Calvin my guess is that you've been scripting Illustrator, which uses getByName. As Dirk mentioned Indesign uses itemByName.

 

Note that itemByName only searches in the current style group. So if you are searching in doc.tableStyles, you will not find it if the style is in a sub- tableStyleGroup.

 

On a positive note, you don't need the try/catch in Indesign because finding a non-existent style will not throw an error. The usual thing is to check validity immediately after getting the reference.

If (!myStyle.isValid)
    // handle missing style

This pattern is very helpful and should be used every time you get a DOM reference. Also it's something to check when debugging because a reference can *become* invalid when other things in the DOM change. 
- Mark

Translate
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
Engaged ,
May 30, 2025 May 30, 2025

Ha, good guess! Also, getByName turns up in search abstracts related to InDesign, probably because of your post here being ingested by "AI:" Solved: Script find - paragraph style (under the folder) - Adobe Community - 12833881

 

Translate
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 ,
May 30, 2025 May 30, 2025
LATEST

Haha, AI will often confabulate, giving you perfect code that relies on a little method call that does *exactly what you're looking for* but... doesn't exist. To me it is wishful thinking. 🙂

 

But seriously I actually find myself—in Illustrator *and* Indesign—using this function, getThings, in about 50% of my scripts:

/**
 * Returns a thing with matching property.
 * If `key` is undefined, evaluate the object itself.
 * @author m1b
 * @version 2024-04-21
 * @Param {Array|Collection} things - the things to look through.
 * @Param {String} [key] - the property name (default: undefined).
 * @Param {*} value - the value to match.
 * @Returns {*?} - the thing, if found.
 */
function getThing(things, key, value) {

    for (var i = 0, obj; i < things.length; i++)
        if ((undefined == key ? things[i] : things[i][key]) == value)
            return things[i];

};

 

It's is extra useful because in Indesign you can ask for Document.allTableStyles and you will get an array-like object containing every table style regardless of hierarchy. So I do

var myTableStyle = getThing(doc.allTableStyles, 'name', 'Financial Table');

and in this case I don't have to check if it's valid I just need to check if I have it, because if it doesn't exist you'll just get nothing (undefined) back:

if (!myTableStyle)
    return alert('There is no table style called "Financial Table".);

 

If you interested, look at this script for example usage, and in that same script I also made a more-elaborate version getThingByPath that you probably won't ever need but it illustrates the topic of getting styles in the hierarchy of style groups.

- Mark

Translate
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