Developing ExtendScripts – Part 3

Let me repeat, if you are very new to ExtendScripts, see the first two entries in this series. Here we are going to see a bit more complex example. This is what we are going to accomplish: If a book file is opened, running the script opens all FrameMaker files (chapters) added to it. Based on a property that we set in the script, it shows or hides a conditional text and then creates a pdf file of the book.

Before trying out this example, ensure that you do the following:

  1. Create a FrameMaker book, myBook.book. And, save it in a directory, FM_script, in your C: drive. (Feel free to change the name and location of the file. In that case, update the bookFile property value in the script.)
  2. Add two chapters to the book.
  3. Open the first chapter.
  4. Add some texts and apply the conditional text, Comment. (Comment is a default conditional text added in all the FrameMaker files. Don’t ask me how to apply a conditional text. Google is your friend.)
  5. Do step 3 to 4 for the second chapter.

Let us prepare a logical order for what we are going to accomplish:

  1. Open the book file. (Let us do it by ourselves; we will not seek help of the script. :))
  2. Open the first chapter.
  3. Hide the conditional text, Comment.
  4. Open the second chapter and hide the conditional text, Comment.
  5. Save the book as a pdf in a specific name.

Let us write functions to code step 2 to 4. First we will write a function that opens all chapters in the book.

bookFile=”C:\\FM_script\\myBook.book”
openedBook = app.ActiveBook
openBookFiles(openedBook)
savePdf(openedBook,bookFile + “.pdf”)
function openBookFiles(openedBook)
{
var bookChapter=openedBook.FirstComponentInBook
var chapterId = bookChapter.id
while(chapterId)
{
var chapterName = bookChapter.Name
chapterId = openFile(chapterName)
ShowHide(chapterId, “Comment”, 1)
bookChapter = bookChapter.NextComponentInBook
chapterId = bookChapter.id;
}
}

The first line created an object of the active book. We decided to keep the book opened. We could have written a function to open the book by providing its path. This just a tutorial and I don’t want to complicate things!! OpenBookFiles() function is called by providing the book object. It detected the first chapter using the FirstComponentInBook property. It also detected the component ID. A while loop is added and it’s valid till we reach the last chapter in the book. We found the chapter name using the Name property and opened the chapter using the openFile() method. After each chapter is opened, it calls a function, showHide().

function openFile(chapterName)
{
openProp = GetOpenDefaultParams()
retParm = new PropVals()
fileOpen=Open(chapterName,openProp,retParm)
return fileOpen;
}

You can open a FrameMaker file using the open() method. It needs three arguments: the file name, PropVal objects, and return parameters. PropVal objects is an array of values for various FrameMaker setting. We created the default ProPValue objects using the GetOpenDefaultParams() method. Return parameters are created using the new PropVals() method. The function opens the file and then calls the showHide() method:

function ShowHide(chapterId, condTextName, showOrHide)
{
CondFmtId = chapterId.GetNamedCondFmt (condTextName)
if (showOrHide)
CondFmtId.CondFmtIsShown = 1
else
CondFmtId.CondFmtIsShown = 0
chapterId.ShowAll = 0
}

The showHide() function accepts the following parameters:

chapterId – The reference to the FM file that is opened.
condTextName – The name of the conditional text. That is Comment.
showOrHide – It can be 0 or 1. If it is 1, the conditional text is visible in the pdf. If it’s 0, conditional text is hidden.
Using the GetNamedCondFmt() function, it finds the conditional text ID of Comment, and based on showorHide value, it makes it visible or hidden by setting the CondFmtIsShown property to 1 or 0. After conditional texts are set, the script calls the savePdf() method to save the file as a pdf.

function savePdf(openedBook,pdfName)
{
var params = GetSaveDefaultParams()
var returnParamsp =new PropVals()
var i = GetPropIndex(params, FS_FileType)
params[i].propVal.ival =FV_SaveFmtPdf
openedBook.Save(pdfName, params, returnParamsp)
return
}

The function accepts reference to the book file and pdf name as the arguments. Using the GetSaveDefaultParams() method it gets all the default parameters of FrameMaker. It finds the File Type index, a unique value for FrameMaker using the GetPropIndex method. In the parameter’s array, it changes the file type to pdf using the params[i].propVal.ival =FV_SaveFmtPdf method. Then, it saves the file as a pdf using the Save() method.

You can also download the script here. Let me know if you find any issues.

Try to improve the code. For example, if you have a folder added to the book, you may notice that the files in the folder are not getting opened. As I mentioned earlier, my intention was to give you an overview!! It is yours now. All the best!!

Comments are closed.