Skip to main content
Known Participant
September 24, 2018
Answered

How to add .fm files to existing book using ExtendScript ToolKit

  • September 24, 2018
  • 1 reply
  • 1291 views

I am writing an ExtendScript Toolkit script to open a ditamap, save it as a book, and then add some boilerplate .fm files in a specific order between the title page and TOC.

So far I've got everything except adding the boiler plate .fm files.  Anyone know how to do that?

var sourceDoc = openXmlFile("Ditamap file path goes here");

var path = "Output file path goes here";

saveToFm(sourceDoc, path);

function openXmlFile(filePath) { 

    var file = File(filePath);

    var fileName = file.fsName;

    var openParams = GetOpenDefaultParams(); 

    var retParams = new PropVals();

    var doc = Open(fileName, openParams, retParams);

    return doc; 

function saveToFm(fileObject, savePath) { 

    var saveParams = GetSaveDefaultParams(); 

   

    var i = GetPropIndex(saveParams, Constants.FS_FileType); 

    saveParams.propVal.ival = Constants.FV_SaveFmtBookWithFm;

   

    var i = GetPropIndex(saveParams, Constants.FS_DitavalFile); 

    saveParams.propVal.sval = "Dit Val file path goes here"

   

    var saveAsName = savePath; 

    var returnParamsp = new PropVals(); 

    fileObject.Save(saveAsName, saveParams, returnParamsp); 

This topic has been closed for replies.
Correct answer frameexpert

Assuming the book is your book object, you can do something like this:

bookComp = book.NewSeriesBookComponent (0);

// Move it to the end.

if (bookComp.NextComponentInBook.ObjectValid()) {

    bookComp.NextComponentInBook = 0;

} // Set its path.

bookComp.Name = "Absolute path to the component.";

1 reply

frameexpert
Community Expert
frameexpertCommunity ExpertCorrect answer
Community Expert
September 24, 2018

Assuming the book is your book object, you can do something like this:

bookComp = book.NewSeriesBookComponent (0);

// Move it to the end.

if (bookComp.NextComponentInBook.ObjectValid()) {

    bookComp.NextComponentInBook = 0;

} // Set its path.

bookComp.Name = "Absolute path to the component.";

www.frameexpert.com
gabeS1234Author
Known Participant
September 24, 2018

I've updated my code with a

var book = app.ActiveBook;

And your function to add book components

function addBoilerPlate(book, componentPath) {

   

bookComp = book.NewSeriesBookComponent (0);  

// Move it to the end.  

if (bookComp.NextComponentInBook.ObjectValid()) {  

    bookComp.NextComponentInBook = 0;  

} // Set its path.  

bookComp.Name = componentPath;

bookComp.MoveComponent(1);

}

By itself your function adds the boilerPlate.fm to the end of the book.  However, I want it between the title and TOC.  Once it is added I can move it up with bookComp.MoveComponent(1); but the final version of this script will deal with books with different numbers of chapters so that wont work.  Is there a way for me to add boilerPlate.fm between 2 specific pages or after or before a specific page?  I tried modifying the 0s in your function, but no matter what I did it always added boilerPlate.fm to the end of the book.

Full code:

var sourceDoc = openXmlFile("Absolute path to ditamap");

var path = "Absolute path to output";

var componentPath = "Absolute path to boilerPlate.fm"

saveToFm(sourceDoc, path);

var book = app.ActiveBook;

addBoilerPlate(book, componentPath);

function openXmlFile(filePath) { 

    var file = File(filePath);

    var fileName = file.fsName;

    var openParams = GetOpenDefaultParams(); 

    var retParams = new PropVals();

    var doc = Open(fileName, openParams, retParams);

    return doc; 

function saveToFm(fileObject, savePath) { 

    var saveParams = GetSaveDefaultParams(); 

   

    var i = GetPropIndex(saveParams, Constants.FS_FileType); 

    saveParams.propVal.ival = Constants.FV_SaveFmtBookWithFm;

   

    var i = GetPropIndex(saveParams, Constants.FS_DitavalFile); 

    saveParams.propVal.sval = "Absolute path to Dita Val File"

   

    var saveAsName = savePath; 

    var returnParamsp = new PropVals(); 

    fileObject.Save(saveAsName, saveParams, returnParamsp); 

function addBoilerPlate(book, componentPath) {

   

bookComp = book.NewSeriesBookComponent (0);  

// Move it to the end.  

if (bookComp.NextComponentInBook.ObjectValid()) {  

    bookComp.NextComponentInBook = 0;  

} // Set its path.  

bookComp.Name = componentPath;

bookComp.MoveComponent(1);

}

frameexpert
Community Expert
Community Expert
September 24, 2018

Find the toc component; let's assume that the variable is tocComp. Then you can do this.

bookComp.NextComponentInBook = tocComp;

www.frameexpert.com