Copy link to clipboard
Copied
Hi
I wrote a script that finds all table headers and collects the page numbers they are on. Then I check the page number of the next paragraph (table) to see if the table spans more than one page. And everything works fine until there is no footnote reference at the end of the paragraph with the table header - then the next paragraph is the first paragraph of the thread. Do you have any ideas on how to solve this problem?
var doc = app.activeDocument;
var toSearch = app.selection[0].parentStory;
var myTxt = app.selection[0].parentStory.paragraphs;
function main(){
var tableHeadersPages = findTableHeaders();
alert(tableHeadersPages);
}
function findTableHeaders(){
// reset szukania
app.findChangeGrepOptions = null;
app.findChangeTextOptions = null;
app.findTextPreferences = app.changeTextPreferences = null;
app.findGrepPreferences = app.changeGrepPreferences = null;
// nagłówki tabel – numery stron
var tableHeadersPages = [];
app.findTextPreferences.appliedParagraphStyle = app.activeDocument.paragraphStyleGroups.item("Rys i Tab").paragraphStyles.item("Tabela H");
var tableHeaders = toSearch.findText();
for (var i = 0; i < tableHeaders.length; i++){
var start = tableHeaders[i].parentTextFrames[0].parentPage.name;
var stop = myTxt.nextItem(myTxt.nextItem(tableHeaders[i])).parentTextFrames[0].parentPage.name;
if (start != stop){
tableHeadersPages.push(start.toString() + '-' + stop.toString());
}
}
return tableHeadersPages;
}
main();
I wrote a short test to select the next paragraph and this script is OK.
var myPara = app.selection[0].paragraphs[0];
var myTxt = app.selection[0].parentStory.paragraphs;
myTxt.nextItem(myPara).select();
Perhaps the problem is because I used the search tool?
> Perhaps the problem is because I used the search tool?
No. Eugene was on the right track: the problem is with this line:
myTxt.nextItem(myTxt.nextItem(tableHeaders[i]))
if a table header consists of one word, you'll have found a word. You could probably get it to work when you target the found item's parent paragraph:
myTxt.nextItem(myTxt.nextItem(tableHeaders[i].paragraphs[0]))
As to finding whether a table breaks across pages, there's an easier way to discover that If a table's par
...Copy link to clipboard
Copied
Could it be the findText()
Does 'myTxt.nextItem() with validation' work?
If you add some error handling it might pinpoint the issue 'try...catch'
Could you instead of relying on
findTextPreferences
Directly compare
paragraph.appliedParagraphStyle
Not really sure if I'm honest but might be an alternate approach.
Copy link to clipboard
Copied
--
Copy link to clipboard
Copied
> Perhaps the problem is because I used the search tool?
No. Eugene was on the right track: the problem is with this line:
myTxt.nextItem(myTxt.nextItem(tableHeaders[i]))
if a table header consists of one word, you'll have found a word. You could probably get it to work when you target the found item's parent paragraph:
myTxt.nextItem(myTxt.nextItem(tableHeaders[i].paragraphs[0]))
As to finding whether a table breaks across pages, there's an easier way to discover that If a table's parent paragraph consists of one line, the table doesn't break. If a table breaks across two pages, its parent paragraph has two lines. Etc. So:
for (var i = 0; i < tableHeaders.length; i++) {
// par is the table's parent paragraph
par = tableHeaders[i].parent.parent.storyOffset.paragraphs[0];
if (par.lines.length > 1) {
start = par.parentTextFrames[0].parentPage.name;
stop = Number(start) + par.lines.length;
tableHeadersPages.push(start + '-' + stop.toString());
}
}
Copy link to clipboard
Copied
Thank You!!! It works.
The loop to find tables doesn't work – Object does not support the property or method 'storyOffset'
I tried to understand storyOffset but I can't
Copy link to clipboard
Copied
storyOffset is an insertion point (though you wouldn't guess it from its name; it was probably a number in early days).
storyOffset is a property of tables. A table's storyOffset's parentParagraph is therefore the paragraph that the table sits in. Not sure why that doesn't work for you.
>Object does not support the property or method 'storyOffset'
When the error occurs, what kind of object is tableHeaders[i].parent.parent? Check it in your debugger, or write logs to a console or a file, depending on what you use to debug scripts.
Copy link to clipboard
Copied
What if TextFrames are not on consecutive pages? Or even on the same page - but small, so only one row is in each TF?
Then number of text lines will return misleading info?
Copy link to clipboard
Copied
> Then number of text lines will return misleading info?
Yep.