When a document is exported to EPub, by default Adobe InDesign exports items in order of front page to back, left to right, top to bottom. If this is not what you want, you have several options:

Use the Articles Panel

  1. Drag page items in the order you want them to the Articles Panel
  2. Select page items and add to existing article or create a new article in the Articles Panel

In working with the Articles Panel:

  • If a number of page items are selected when the Create New Article icon at the bottom of the Articles panel is clicked, all selected page items will be added to the new article when it is created.
  • If an Article is selected when the Add Selection to Articles button is clicked, any selected items will be added to that article.
  • If no Article is selected when the Add Selection to Articles button is clicked, the user is presented a dialog in which to name the new article, and the selected items will be added to the article when it is created.

When selecting items for inclusion in the Articles Panel:

  • If you shift-click to select the items, the items will be added to the new article in the order that you selected them.
  • If you use command+Click to add all page items, the items will be added using InDesign’s default order (left to right, top to bottom).
  • If you use Select All (Edit > Select All) to select the items and then click either the Create New Article or Add Selection to Articles button, the items will be added in the order of their creation (item ID).

Use a Script

When you have story segments scattered throughout a sizable document, selection is out of the question. If related stories have an attribute that is unique, you can filter the stories by attribute and combine the segments into a single story thread.

Get a List of First Frames

First you need to get a list of the stories (or story containers) by attribute. (See our blog post for July 22, 2013).

Once you have the list, you can parse through the list and thread the stories together using a script. This story can then be added to the Articles Panel

Some story segments may have more than one text frame within its link. You will need to filter through the stories and get a list of just the first text frames for each text thread. This can be done using one of the following:

    1. Filter out all text frames where the previous text property is nothing (or null). This example gets all first text frames:

AppleScript

tell application "Adobe InDesign CC"
    tell document 1
       set firstFrames to {}
       set textFrameList to text frames
       repeat with i from 1 to length of textFrameList
          if class of parent of item i of textFrameList is not master spread then
             if previous text frame of item i of textFrameList is nothing then
                set end of firstFrames to item i of textFrameList
             end if
          end if
       end repeat
    end tell
 end tell

ExtendScript

var docRef = app.documents.item(0);
var firstFrames = [];
var textFrameArr = docRef.textFrames;
var thisItem;
for (var i = 0; i < textFrameArr.length; i++) {
    thisItem = textFrameArr[i];
    if (thisItem.parent.constructor.name != "MasterSpread") {
        if (thisItem.previousTextFrame == null) {
            firstFrames.push(thisItem);
        }
    }
}
    1. Get a list of all stories and then filter stories using reference to the first item of its text container list. This example adds a further test to filter the story containers.

AppleScript

set styleName to "Note" --get paragraph style name from user
tell application "Adobe InDesign CC"
    tell document 1
       set styleRef to paragraph style styleName
       set storyList to stories
       set firstFrames to {}
       --get filtered list
       repeat with i from 1 to length of storyList
          set thisStory to item i of storyList
          tell thisStory to set thisContainer to item 1 of text containers
          if class of parent of thisContainer is not master spread then
             if (previous text frame of thisContainer is nothing and applied paragraph style of paragraph 1 of thisStory = styleRef) then
                 set end of firstFrames to thisContainer
             end if
         end if
       end repeat
    end tell
 end tell

ExtendScript

var styleName = "Note"; //get name of style from user
main();
    function main(){
      var docRef = app.documents.item(0);
      var styleRef = docRef.paragraphStyles.itemByName(styleName);
      var firstFrames = [];
      var storyArr = docRef.stories;
      var thisStory, thisContainer;
      for (var i = 0; i < storyArr.length; i++) {
        thisStory = storyArr[i];
        thisContainer = thisStory.textContainers[0];
        if (thisContainer.parent.constructor.name != "MasterSpread") {
            if (thisContainer.previousTextFrame == null && thisStory.paragraphs.item(0).appliedParagraphStyle == styleRef) {
                firstFrames.push(thisContainer);
            }
        }
      }
   }

Thread Stories

Once you have a list of filtered first frames, you can parse through the list to combine the stories into a single thread:

AppleScript

set articleName to "MyArticle" --get article name from user
tell application "Adobe InDesign CC"
   set docRef to document 1
   tell docRef
      set masterStory to parent story of item 1 of firstFrames
      set lastFrame to missing value
      repeat with i from 1 to length of firstFrames
         set thisFrame to item i of firstFrames
         set testFrame to end text frame of thisFrame
         --if lastFrame is not missing value then
         --link the current frame to the lastFrame
         if lastFrame is not missing value then
             set previous text frame of thisFrame to lastFrame
         end if
         --set up the lastFrame variable for the next iteration
         if testFrame is not thisFrame then
            set lastFrame to end text frame of thisFrame
         else
            set lastFrame to thisFrame
         end if
         --make sure stories do not run together
         my testEndCharacter(docRef, masterStory)
      end repeat
      tell masterStory
         set masterFrame to item 1 of text containers
      end tell
      set articleRef to make article with properties {name:articleName}
      tell articleRef to make article member with properties {item ref:masterFrame}
   end tell
end tell
--HANDLER 
on testEndCharacter(docRef, storyRef)
       tell application "Adobe InDesign CC"
          tell docRef
             if contents of insertion point -1 of storyRef is not return then
                 set contents of insertion point -1 of storyRef to return
             end if
          end tell
       end tell
end testEndCharacter 

ExtendScript

var articleName = "MyArticle"; //get name of article from user
    function combineStories(docRef, firstFrames) {
       masterStory = firstFrames[0].parentStory;
       var lastFrame, thisFrame, testFrame;
       for (var i = 0; i < firstFrames.length; i++) {
           thisFrame = firstFrames[i];
           testFrame = thisFrame.endTextFrame;
           if (lastFrame != undefined) {
              thisFrame.previousTextFrame = lastFrame;
           }
           //set up the lastFrame variable for the next iteration
           if (testFrame.id != thisFrame.id) {
              lastFrame = thisFrame.endTextFrame;
           } else {
               lastFrame = thisFrame;
           }
           testEndCharacter (masterStory);
       }
      //Add story to article
      var masterFrame = masterStory.textContainers[0];
      var articleRef = articles.add({name:articleName});
      articleRef.articleMembers.add({itemRef:masterFrame});
      function testEndCharacter (storyRef){
          if (storyRef.insertionPoints.lastItem().contents != "\r"){
              storyRef.insertionPoints.lastItem().contents = "\r";
          }
     }
}

Finishing Up

Add finishing touches to the code above to make your own script. We will be adding a more complete script to an EPub script collection to be posted later.