Skip to main content
dublove
Legend
December 26, 2024
Question

This script for adding rows seems to be a bit off, please help fix it!

  • December 26, 2024
  • 3 replies
  • 1498 views

This script is used to determine if there is already a table header and add a row if there isn't.

Since this script doesn't require the table to be selected in full, I like it.
I found the script to be correct if there is only one table.
However, if there are more than two tables, my cursor is already inside the second table, but mytable still points to the previous table table[0].

 

 

//first mytable in the active document
var mytable=app.activeDocument.textFrames[0].parentStory.tables[0]
var firstRow = mytable.rows[0];
//to header and add 1 row;
if (firstRow.rowType !== RowTypes.HEADER_ROW)
{
dupeTopRow(mytable);
mytable.rows.add(LocationOptions.BEFORE, mytable.rows[0]);
}
else{
alert("header exist")
exit ();
}

 

 

 

 

This topic has been closed for replies.

3 replies

Peter Kahrel
Community Expert
Community Expert
December 27, 2024

@dublove -- I've mentioned this before, and I'll mention it again, hoping that you'll add it to your list of New Year good intentions.

 

1. The most important thing for you is to get to grips with InDesign. Get yourself a good reference book. There are books aimed a users of various levels of experience. I would suggest Real-World InDesign. It covers InDesign up to some versions ago but nothing much has been added to InDesign in recent versions that would get people like you -- typesetters -- excited. It's an excellent reference with a good table of contents and an excellent index. You could also consider Linda tutorials, many of which are produced by seasoned InDesign pros.

 

2. When you can't find something in the book/tutorials, use a search engine to check whether your problem was dealt with in the past. You clearly don't do that. what you raised recently -- about user interaction -- is a sad example. User interaction has been dealt with many times in this forum but also in other forums.

 

3. Perhaps the best approach for you would be to hire a consultant. Make a deal with someone you can ask questions and ask to write scripts for you. You can learn from such a consultant, and you can also get some script solutions that are tailored to your problem. This is more efficient than trying to adapt other scripts to your needs.

 

P.

dublove
dubloveAuthor
Legend
December 27, 2024

The great Peter Kahrel.
You always give me some great ideas.


I'm just a typesetter and I'm pretty clueless about scripting.

I've actually searched through a lot of the postings and you'll see I've left comments under a lot of the old ones.

 

I'm making a lot of progress these days.
Thank you very much.

 

Just today I have completed a script worth millions.

I'll try not to ask big questions in the future.


It's all forced by life.

 

Congratulations on the fortune.
Thank you.

Community Expert
December 26, 2024

You have threaded frames so it would be a single parentStory for both the frames in the thread. Hence the following statement would point to the 1st table irrespective of where your cursor is placed.

 

var mytable=app.activeDocument.textFrames[0].parentStory.tables[0]

 

With respect to your second approach you are trying to find table in your selection. However, if you just have your cursor in the table then it would not return any table as the selection is just an insertion point. However if you get to the parent of the selection then you will get to cell and if you check its parent then you will get the table.

 

var cell = app.activeDocument.selection[0].parent
var myTable = cell.parent

 

-Manan

-Manan
dublove
dubloveAuthor
Legend
December 26, 2024

I didn't get the function.
Tired.

Robert at ID-Tasker
Legend
December 26, 2024

@dublove

 

You should really learn the internal structure(s) of the Document.

 

@Manan Joshi gave you the answer - so what's the point of your reply and why are you saying it's wrong? 

 

dublove
dubloveAuthor
Legend
December 26, 2024

When I changed it to this.

 

 

 

 

var myTable=app.activeDocument.selection[0].tables[0]
var firstRow = myTable.rows[0];

 

 

 

 


It then says var firstRow = myTable.rows[0];

is an invalid object

 

So you have to add it.
.parentStory.tables[0]

 

var myTable=app.activeDocument.selection[0].parentStory.tables[0]
var firstRow =myTable.rows[0];

 

But if there are more than two tables in a text box, the problem arises again, and the script only recognizes the first table in the text box.

How to target only the current table