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

A script to adjust the column widths of just the selected table

Explorer ,
Jan 15, 2025 Jan 15, 2025

Hi

 

I've used the following script a lot over the years. It's brilliant, but it changes every table in the document, and now I've come across a situation where I need a script that does the same thing, but just to the table that is selected. I thought I'd seen this discussed somewhere on the forums in the past but I can't find it.

 

Is it a simple change to make it target just the selected table?

 

I don't recall where I originally found this script, so apologies to the author.

 

var myDoc = app.activeDocument;
var myWidths = [60, 10, 10, 10];
for(var T=0; T < myDoc.textFrames.length; T++){
for(var i=0; i < myDoc.textFrames[T].tables.length; i++){
for(var j=0; j < myWidths.length; j++){
myDoc.textFrames[T].tables[i].columns[j].width = myWidths[j];
}
}
}
alert("Table width updated successfully...");

 

Thanks in advance to anyone who may be able to assist.

TOPICS
Scripting
562
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

LEGEND , Jan 15, 2025 Jan 15, 2025

Try this - there is ZERO error checking - so before you run this script - select any Cell - not just text cursor in the Cell - you need to select any Cell. 

 

var myDoc = app.activeDocument;
var myWidths = [60, 10, 10, 10];
var myTable = app.selection[0].parentRow.parent;
for(var j=0; j < myWidths.length; j++){
myTable.columns[j].width = myWidths[j];
}
alert("Table width updated successfully...");

 

Translate
LEGEND ,
Jan 15, 2025 Jan 15, 2025

Try this - there is ZERO error checking - so before you run this script - select any Cell - not just text cursor in the Cell - you need to select any Cell. 

 

var myDoc = app.activeDocument;
var myWidths = [60, 10, 10, 10];
var myTable = app.selection[0].parentRow.parent;
for(var j=0; j < myWidths.length; j++){
myTable.columns[j].width = myWidths[j];
}
alert("Table width updated successfully...");

 

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
Explorer ,
Jan 16, 2025 Jan 16, 2025

That's perfect – thanks Robert.

I really appreciate your help.

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
LEGEND ,
Jan 16, 2025 Jan 16, 2025
quote

That's perfect – thanks Robert.

I really appreciate your help.


By @stevej61370215

 

So it works - great to hear. I've made it in the middle of the night, on my phone 😉 Always happy to help.

 

You could do a lot more - if you work on a PC and don't mind paid solutions 😉

 

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
Explorer ,
Jan 16, 2025 Jan 16, 2025

I'm on a Mac, but regardless I love this clever timesaving stuff.

 

I don't get large table-filled reports to do these days as our clients tend to do their reports inhouse, otherwise I'd definitely be looking for a bigger/paid solution.

 

For the small one-off I'm working on at the moment I've set up multiple copies of your script with different table widths that are common throughout – then I've assigned each version of the script a different keyboard short cut. So your script is definitely making the job more efficient.

 

Thanks again.

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
LEGEND ,
Jan 16, 2025 Jan 16, 2025
quote

I'm on a Mac, but regardless I love this clever timesaving stuff.

 

Unfortunately, my tool is PC only - for a direct use(*).

 

quote

For the small one-off I'm working on at the moment I've set up multiple copies of your script with different table widths that are common throughout – then I've assigned each version of the script a different keyboard short cut.


By @stevej61370215

 

And that's pretty much perfect definition of my tool - save manual clicks / operations as Task(s) - and execute on demand - WITHOUT the need to be a coder / scripter:

RobertatIDTasker_0-1737055412181.png

(in this case, "Row" / "Column" columns show number of rows / columns in each table, otherwise, they show "address" of the cell)

 

You can load basic info about all tables in the document - or from selected Story / Selected Text / etc. - then either double-click on the element on the list to select specific table and then execute Task to format it - or checkmark in the first column, tables you want to process - after sorting / filtering by any property - for example number of columns / rows, or applied Table / Cell Style, etc. - and then process them in BatchMode - using the same Task from the top part.

 

Or you can be in InDesign then use a keyboard shortcut - IDT is monitoring keyboard globally - and execute Task without switching to IDT.

 

Or you can browse tables in IDT - they can be automatically selected in InDesign for preview - and execute Task using just Keyboard - no mouse needed.

 

 

And that's just a tip of an enormous iceberg 😉 But - PC only...

 

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
LEGEND ,
Jan 16, 2025 Jan 16, 2025
LATEST

Here, I've loaded info about only first 5x selected tables, then used filters to show only Cells from 1st row and 3rd column:

RobertatIDTasker_0-1737056353209.png

 

Because, everything user can click in IDT - can also be saved as a command in the Task - user doesn't have to do "pre-processing" on each INDD file - a dedicated Task can prepare "environment" as well.

 

Or one big Task can select objects to be processed - texts, tables, objects, etc. - and then do processing.

 

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