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

How to convert a function into a method that can be directly applied to each object.

Guide ,
Sep 05, 2025 Sep 05, 2025

For example:
I have a function that adjusts the width of selected table to the width of the textFrame.
Now I have selected a textFrame.
I used a loop.
I want each table to apply this once:

tbs[i].tabFitToTextFrame()

If you remove tbs[i] and use:

tabFitToTextFrame();

only the first tab will be modified.

 

I try Like this:

It doesn't seem to be working.

//var sel = app.documents[0].selection;
var doc = app.activeDocument;
var tbs = doc.selection[0].tables;
//var tbs = getTable(doc.selection);
alert(tbs);
//alert(sel.tables);
if (tbs.length > 0) {
    for (var i = 0; i < tbs.length; i++) {
        alert(tbs[i]);
        tabFitToTextFrame();
    }
}

function tabFitToTextFrame() {
    var doc = app.activeDocument,
        item = doc.selection[0];
    var myTable = getTable(doc.selection);
    parentWidth = myTable.parent.geometricBounds[3] - myTable.parent.geometricBounds[1];
    factor = parentWidth / myTable.width;
    for (col = 0; col < myTable.columns.length; col++)
        myTable.columns[col].width *= factor;
}

function getTable(obj) {
    if (obj == undefined)
        return;
    if ('Array' === obj.constructor.name) {
        for (var i = 0, table; i < obj.length; i++) {
            table = getTable(obj[i]);
            if (table && table.isValid)
                return table;
        }
        return;
    }
    if (obj.constructor.name == 'Cell')
        return obj.parent;
    if (obj.parent.constructor.name == 'Cell')
        return obj.parent.parent;
    if (
        obj.hasOwnProperty('cells')
        && obj.cells.length > 0
    )
        return obj.cells[0].parent;
    if (
        obj.hasOwnProperty('tables')
        && 0 !== obj.tables.length
    )
        return obj.tables[0];
};

0060.png

TOPICS
How to , Scripting
307
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 2 Correct answers

Community Expert , Sep 06, 2025 Sep 06, 2025

If I am understanding your requirements correct the following code would work

var doc = app.activeDocument;
var tbs = doc.selection[0].tables;
//var tbs = getTable(doc.selection);
alert(tbs);
//alert(sel.tables);
if (tbs.length > 0) {
    for (var i = 0; i < tbs.length; i++) {
        tabFitToTextFrame(tbs[i]);
    }
}

function tabFitToTextFrame(myTable) {
    parentWidth = myTable.parent.geometricBounds[3] - myTable.parent.geometricBounds[1];
    factor = parentWidth / myTable.width;
    for (
...
Translate
Guide , Sep 06, 2025 Sep 06, 2025

I think I've found the reason.
I have two myTable settings.
One is m1b's myTable = getTable(doc.selection);
This getTable seems to have an issue—it can't retrieve multiple tables. It's not a collection.

Translate
Guide ,
Sep 05, 2025 Sep 05, 2025

I updated the question.

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 ,
Sep 06, 2025 Sep 06, 2025

If I am understanding your requirements correct the following code would work

var doc = app.activeDocument;
var tbs = doc.selection[0].tables;
//var tbs = getTable(doc.selection);
alert(tbs);
//alert(sel.tables);
if (tbs.length > 0) {
    for (var i = 0; i < tbs.length; i++) {
        tabFitToTextFrame(tbs[i]);
    }
}

function tabFitToTextFrame(myTable) {
    parentWidth = myTable.parent.geometricBounds[3] - myTable.parent.geometricBounds[1];
    factor = parentWidth / myTable.width;
    for (col = 0; col < myTable.columns.length; col++)
        myTable.columns[col].width *= factor;
}

-Manan

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
Guide ,
Sep 06, 2025 Sep 06, 2025

Hi Manan Joshi.

That's it.
Thank you very much.


But I don't understand where the key difference lies.
I think I've tried your method before.

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
Guide ,
Sep 06, 2025 Sep 06, 2025

I think I've found the reason.
I have two myTable settings.
One is m1b's myTable = getTable(doc.selection);
This getTable seems to have an issue—it can't retrieve multiple tables. It's not a collection.

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 ,
Sep 06, 2025 Sep 06, 2025

You did not need the getTables method as you already had the table collection in tbs variable. The getTables method returns back a table based on different input sent to it. Now you were always sending selection as input to the function and the selection is never changing so the function will keep on returning the same table everytime. Same input == Same output

-Manan 

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
Guide ,
Sep 07, 2025 Sep 07, 2025

Strange, what did I change?

The width property setting is invalid.

0000.png

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 ,
Sep 07, 2025 Sep 07, 2025

Maybe you changes the selection or something that affects the input to the script

-Manan

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
Guide ,
Sep 08, 2025 Sep 08, 2025

It seems to be due to getTables again, sometimes returning null values.

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 ,
Sep 08, 2025 Sep 08, 2025
LATEST

But we concluded that your code would not need getTables. However, if you are using it then you need to handle the null check before the rest of the code is called up

-Manan

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