Skip to main content
wckdtall
Inspiring
October 10, 2023
해결됨

Is there an InDesign DOM flowchart? Getting contents of text frames on a specific page.

  • October 10, 2023
  • 3 답변들
  • 855 조회

I reference the flowchart for Illustrator quite a bit to understand structure, but I'm just wondering if one is available for InDesign? Maybe it's too fluid, but in general, I'm scripting getting the contents of a text frame based on the page number. Stories seem to hold the most info, but I don't entirely understand where they are in the structure. So I think my best bet is:

pageItems = app.activeDocument.pages.item(1).allPageItems
for(var i = 0; i < pageItems.length; i++){
        var curPI = pageItems[i];
        var curType = curPI.constructor.name;
        if(curType == "TextFrame") alert(curPI.parentStory.contents)
}

I don't entirely understand how parentStory or parentPage works... is there a more straightforward way than I've found?

FWIW I'm determining which page I pull text frames from, based on the first findGrep result, but have not complicated this example with that piece.

이 주제는 답변이 닫혔습니다.
최고의 답변: Jens Trost

Hi,

 

this is the closest thing in terms of a flowchart I know of

http://www.indesignjs.de/auflage2/wp-content/uploads/2015/04/InDesign_Skripting_Kurzreferenz.pdf

3 답변

Jens Trost
Inspiring
October 10, 2023

Hi,

 

this is the closest thing in terms of a flowchart I know of

http://www.indesignjs.de/auflage2/wp-content/uploads/2015/04/InDesign_Skripting_Kurzreferenz.pdf

wckdtall
wckdtall작성자
Inspiring
October 11, 2023

Thanks! Definitely what seems to be an all inclusive a flowchart! If nothing else, it's a great quick referene to see what attributes can be addressed per object.

rob day
Community Expert
Community Expert
October 10, 2023

You probably want to use the found text’s parentTextFrames property. Something like this

 

 

//the search result array
var res = getTextSearch("Hello World")

for (var i = 0; i < res.length; i++){
    $.writeln("A search result starts on page " + res[i].parentTextFrames[0].parentPage.name)
    //returns
    //A search result starts on page 4
    //A search result starts on page 5
};   


/**
* Gets results of a text search as an array 
* @ param text to search for 
* @ return result array 
*/
function getTextSearch(fp){
    app.findTextPreferences = app.changeTextPreferences = app.findChangeTextOptions = null;
    app.findChangeTextOptions.properties = {includeHiddenLayers:true, includeLockedLayersForFind:true, includeLockedStoriesForFind:true, includeMasterPages:true} 
    app.findTextPreferences.findWhat = fp;
    return app.activeDocument.findText()
}

 

 

 

rob day
Community Expert
Community Expert
October 10, 2023

Hi @wckdtall , A textFrame has parentPage and parentStory properties

 

Here's the API

https://www.indesignjs.de/extendscriptAPI/indesign-latest/#TextFrame.html

wckdtall
wckdtall작성자
Inspiring
October 10, 2023

Thanks! I do use the API documentation, but I'm specifically looking for the flow chart so I know how these things interact a little bit clearer.

The example you provide is certainly an alternative option to grep, but would still require Parent Story in order to get the contents regardless of overset text.

What I've chosen to do is use allPageItems, because it ignores object structure, and just needs to be evaluated for the object you're looking for. I think this is the most straightforward way, but again I feel like a flowchart would help if it exists

var theDoc = app.activeDocument;
    app.findGrepPreferences.findWhat = "(?i).*TABLE OF CONTENTS.*";
//Check for a result or there will be an error setting variables
    if (theDoc.findGrep().length > 0) {
        //Set the Target Number based off Document Offset starts at 0
        var tgtPgNum = theDoc.findGrep()[0].parentTextFrames[0].parentPage.documentOffset;
        //Set Page to Target
        var tgtPage = theDoc.pages.item(tgtPgNum);
        var tgtPI = tgtPage.allPageItems;
        var tgtFind = [];
        for (var c = 0; c < tgtPI.length; c++) {
            var curPI = tgtPI[c];
            var curType = curPI.constructor.name;
            if (curType == "TextFrame") tgtFind.push(curPI.parentStory.contents);
        }
}
//Dump contents to an alert for testing.
alert(tgtFind);

 

rob day
Community Expert
Community Expert
October 10, 2023

This does the same without looping through the page items—parentTextFrames[0] is the result’s first text frame:

 

 

 


//the search result array
var res = getGrepSearch("(?i).*TABLE OF CONTENTS.*")

for (var i = 0; i < res.length; i++){
    alert("Result on page " + (res[i].parentTextFrames[0].parentPage.documentOffset+1) +"\rText Frame Contents\r\r" + res[i].parentTextFrames[0].contents )
};   


/**
* Gets results of a grep search as an array 
* @ param grep to search for 
* @ returns result array 
*/
function getGrepSearch(fp){
    app.findGrepPreferences = app.changeGrepPreferences = app.findChangeGrepOptions = null;
    app.findGrepPreferences.findWhat = fp;
    return app.activeDocument.findGrep()
}