Loop through all threadded text frames in JavaScript InDesign script?

Arktype picture Arktype · May 8, 2012 · Viewed 7.3k times · Source

I'm trying to create an InDesign script that functions exactly like SplitStory.jsx, only that it won't require first selecting a threaded text frame but instead loops through everything that is threaded/linked within the whole document.

Here is what I have so far, but it doesn't seem to be doing anything. I'm pretty new to scripting.

main();
function main(){
    //Make certain that user interaction (display of dialogs, etc.) is turned on.
    app.scriptPreferences.userInteractionLevel = UserInteractionLevels.interactWithAll;
    if(app.documents.length != 0){
        var myDoc = app.activeDocument;
        var myStories = 0;
        for (var i = 0; myDoc.stories.length > i; i++)
            myStories = myDoc.stories[i];
            mySplitStory(myStories);
            myRemoveFrames(myStories);
    }
    else{
        alert("Please open a document and try again.");
    }
}
function mySplitStory(myStory){
    var myTextFrame;
    //Duplicate each text frame in the story.
    for(var myCounter = myStory.textContainers.length-1; myCounter >= 0; myCounter --){
        myTextFrame = myStory.textContainers[myCounter];
        myTextFrame.duplicate();
    }
}
function myRemoveFrames(myStory){
    //Remove each text frame in the story. Iterate backwards to avoid invalid references.
    for(var myCounter = myStory.textContainers.length-1; myCounter >= 0; myCounter --){
        myStory.textContainers[myCounter].remove();
    }
}

I feel like I need to add something to mySplitStory(myStories); like mySplitStory(myStories.currentStory);, but I can't find anything about this specifically online (or in Adobe's scripting documentation - but maybe I'm not looking in the right places).

Any help would be greatly appreciated!

Answer

lhan picture lhan · May 11, 2012

Here is a very simple script that will allow you to loop through all textFrames within a document and then do whatever you need with the threaded text frames. Basically if any given textFrame has a textFrame object for nextTextFrame or previousTextFrame you know it's threaded.

test();
function test(){

    var myDoc = app.open('c:/123.indd');

    //get all textframes:
    var allTextFrames = myDoc.textFrames;

    for(var i=0;i<allTextFrames.length;i++){
        var tf = allTextFrames[i];

        if(tf.nextTextFrame != null || tf.previousTextFrame != null){

           //text frame is threaded, do work:
        }
    }
}

Hope this helps!!