• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

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

New Here ,
Sep 24, 2018 Sep 24, 2018

Copy link to clipboard

Copied

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); 

TOPICS
Scripting

Views

1.0K

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Community Expert , Sep 24, 2018 Sep 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.";

Votes

Translate

Translate
Community Expert ,
Sep 24, 2018 Sep 24, 2018

Copy link to clipboard

Copied

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.";

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Sep 24, 2018 Sep 24, 2018

Copy link to clipboard

Copied

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);

}

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Sep 24, 2018 Sep 24, 2018

Copy link to clipboard

Copied

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

bookComp.NextComponentInBook = tocComp;

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Sep 25, 2018 Sep 25, 2018

Copy link to clipboard

Copied

LATEST

Thank you!  I got it working:

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

var path = "Absolute file path to output file";

var boilerPlate_1 = "Absolute file path to BP1"

var boilerPlate_2 = "Absolute file path to BP2"

saveToFm(sourceDoc, path);

var book = app.ActiveBook;

var tableOfContents = book.FirstComponentInBook

tableOfContents = tableOfContents.NextComponentInBook

addBoilerPlate(book, boilerPlate_1, tableOfContents);

addBoilerPlate(book, boilerPlate_2, tableOfContents);

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 file path to Dita Val File"

  

    var saveAsName = savePath;

    var returnParamsp = new PropVals();

    fileObject.Save(saveAsName, saveParams, returnParamsp);

}

function addBoilerPlate(book, componentPath, tableOfContents) {

var bookComp = book.NewSeriesBookComponent (0);  

// Move it to the end.  

if (bookComp.NextComponentInBook.ObjectValid()) {  

    bookComp.NextComponentInBook = tableOfContents;  

} // Set its path.  

bookComp.Name = componentPath;

}

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines