Skip to main content
Legend
February 7, 2013
Question

Need ExtendedScript to change paragraph formats

  • February 7, 2013
  • 1 reply
  • 1302 views

Hi

We are migrating our old courses into a new FrameMaker template. As part of this process, we have to change the content with old paragraph formats to new paragraph formats as well. For example, content with paragraph tag "Bullet1" in the old course should be changed to "ListBullet1" paragraph format in the new course.

 

I am aware of two approaches for this task.

1.  Open each FM file and use Global Update feature to update the each format at file level.

2.  Use Paragraph Tools shareware plug-in from Silicon Prairie to update the each format at book level.

We have 70+ paragraph formats to be converted. So, is there a ExtendedScript to do this task?

This topic has been closed for replies.

1 reply

Legend
February 7, 2013

Hi Sreekanth,

Fun question. This is exactly the type of thing scripting is good for. Here is a script that does the task for a single format change for a single, active document. It could be expanded to work over a book, all active docs, a list of pgf formats,  etc. But, this is as much as I can scratch down at the moment.  To make this useful, you'll probably need to expand it some.

You can change the arguments of the function call to do whichever formats you want.

Russ

ChangePgfFmt("Bullet1", "ListBullet1");

function ChangePgfFmt(oldFmtName, newFmtName)

{

    var newPgfFmt, pgf;

    //get the active document

    var doc = app.ActiveDoc;

    if(!doc.ObjectValid())

    {

        alert("No active document.");

        return;

    }

    //get the new paragraph format

    newPgfFmt = doc.GetNamedObject (Constants.FO_PgfFmt, newFmtName);

    if(!newPgfFmt.ObjectValid())

    {

        alert("The new paragraph format name is bogus.");

        return;

    }

    //get the new paragraph format properties. This will include the name

    props = newPgfFmt.GetProps();

   

    //start iterating through all paragraphs in the doc

    pgf = doc.FirstPgfInDoc;

   

    while(pgf.ObjectValid())

    {

        //if the current paragraph has a format applied

        //with the old pgf format name, apply the new

        //format properties. This will apply the new name.

        if(pgf.Name == oldFmtName)

           pgf.SetProps(props);

       

        pgf = pgf.NextPgfInDoc;

    }

  

}

Legend
February 8, 2013

Thanks Russ for your quick reply.

I am still very new to ExtendedScript. From what I see here, I need to execute this script for every paragraph tag. Is it possible to use some kind of array (or anything else) and use this to change multiple tags in one go?

I am just thinking if I can have a table with old and new tags, which the script can use to find/ replace in the document.

Legend
February 8, 2013

Hi Sreekanth,

I didn't have time yesterday, but I did take a few minutes today to enhance the script to use arrays. It now takes two parallel lists of format names, old and new, which can be as long as you want. You'll have to edit the script manually to put in the formats you want. It would certainly be possible to read from a table, but this would be a bit more involved and is beyond what I can do for free.

In this new script, I removed the warning and abort if an input format name is bogus.

Russ

var oldFmtNames=new Array("OldFmt1","OldFmt2","OldFmt3");

var newFmtNames=new Array("ReplacementFmt1","ReplacementFmt2","ReplacementFmt3");

ChangePgfFmt(oldFmtNames, newFmtNames);

function ChangePgfFmt(oldFmtNames, newFmtNames)

{

    //Make sure our arrays are valid and balanced

    if(oldFmtNames.length == 0 ||

       newFmtNames.length == 0 ||

       oldFmtNames.length != newFmtNames.length)

    {

        alert("Something is wrong with the input " +

                "format arrays. Cannot continue.");

        return;

    }

   

    var newPgfFmt, pgf;

    //get the active document

    var doc = app.ActiveDoc;

    if(!doc.ObjectValid())

    {

        alert("No active document.");

        return;

    }

    for(i = 0; i < oldFmtNames.length; i++)

    {

        //get the new paragraph format

        newPgfFmt = doc.GetNamedObject (Constants.FO_PgfFmt, newFmtNames);

        if(!newPgfFmt.ObjectValid())

        {

            continue;

            //alert("The new paragraph format name is bogus.");

            //return;

        }

        //get the new paragraph format properties. This will include the name

        props = newPgfFmt.GetProps();

       

        //start iterating through all paragraphs in the doc

        pgf = doc.FirstPgfInDoc;

       

        while(pgf.ObjectValid())

        {

            //if the current paragraph has a format applied

            //with the old pgf format name, apply the new

            //format properties. This will apply the new name.

            if(pgf.Name == oldFmtNames)

               pgf.SetProps(props);

           

            pgf = pgf.NextPgfInDoc;

        }

    }

}