Skip to main content
Known Participant
November 29, 2018
Answered

Copy Paragraphs from Source files and Paste to Destination File

  • November 29, 2018
  • 1 reply
  • 927 views

Hi,

I have a Book with multiple Source files (Source_1, Source_2, ...) and a Destination File. I need to copy all paragraphs from Source files to a global array and finally paste to the Destination file without altering its Paragraph Format.

I tried with below code, it is copying to the array but failing to paste at Destination file. Please suggest.

var book = app.ActiveBook;

var Data = [] ;

if(!book.ObjectValid())

{

    book = app.FirstOpenBook;

}

if(book.ObjectValid())

{

    app.ActiveBook = book;

    var pgf, pgf1, doc, SrcDoc, DstDoc, index, lastPgf, i ;

    var comp = book.FirstComponentInBook;

    while(comp.ObjectValid()) // Book loop to push all paragraphs from Source files

    {

         doc = OpenFile(comp.Name); // Have function to Open File

         if (doc.Name.match ("Source"))

         {

            SrcDoc = doc;

            pgf = SrcDoc.MainFlowInDoc.FirstTextFrameInFlow.FirstPgf;

            while (pgf.ObjectValid())

            {

                Data.push(pgf);    

                pgf = pgf.NextPgfInFlow;

            }

        }

        comp = comp.NextBookComponentInDFSOrder;

    }

    comp = book.FirstComponentInBook;

    while(comp.ObjectValid()) // Book loop to find Destimnation file

    {

        doc = OpenFile(comp.Name);

        if (doc.Name.match ("Destination"))

        {

            DstDoc = doc;

            lastPgf = DstDoc.MainFlowInDoc.FirstTextFrameInFlow.LastPgf;

            pgf = DstDoc.NewSeriesPgf (lastPgf);

            tRange = new TextRange; 

            index = Data.length; 

            for (i = 0;  i < index; i++) // Loop to add all pushed paragraphs to Destimnation file

            {

                pgf1 = Data

                tRange.beg.obj = pgf1; 

                tRange.beg.offset = 0; 

                tRange.end.obj = pgf1; 

                tRange.end.offset = Constants.FV_OBJ_END_OFFSET; 

                DstDoc.TextSelection = tRange;  

                PushClipboard ();

                DstDoc.Copy (0);

                textLoc = new TextLoc (pgf, 0);

                textRange = new TextRange (textLoc, textLoc);

                DstDoc.TextSelection = textRange;

                DstDoc.Paste ();

                PopClipboard ();

            }

            break;

        }

        comp = comp.NextBookComponentInDFSOrder;

    }

}

This topic has been closed for replies.
Correct answer 4everJang

You are trying to do something that is not possible in this way. The objexcts that are pushed into the array are paragraph objects, whicih is not the same as the paragraph content in FrameMaker. In fact, the Pgf object in the array might be a pointer instead of the real thing, but you can never be sure whether the script creates an actual object or just a pointer to an object., In any case, the Pgf only has a meaning (something correspondig to it) in the scope of the document. In your case, this would be the source document. When you try to paste into the target document, there is nothing referenced, as the scope has changed to another document.

To copy the actual paragraph from one doc to another, you need to do the following:

1. Make sure you have the source document open. Let's call this oSourceDoc.

2. Make sure you have the target document open. Let's call this oTargetDoc.

3. Select a paragraph in the source doc.

4. Create a text range that contains the entire paragraph.

     create new TextLoc (oTLoc1) to point to the beginning of the paragraph

     create new TextLoc (oTLoc2) to point to the end of the paragrap

     create new TextRange (oTRangeSource) with oTLoc1 and oTLoc2

5. Set the current text selection of the source doc to match the text range

     oSourceDoc.TextSelection = oTRangeSource

5. Use oSourcDoc.Copy( ) to copy the paragraph to the Windows clipboard

6. Create a text range in the target document

     create new TextLoc (oTLoc) at the insertion point

     create new TextRange (oTRangeTarget) from oTLoc and oTLoc (same TLoc used twice)

7. Set the current text selection of the target doc to match the new text range

8 Use oTargetDoc.Paste( ) to paste the Windows clipboard into the target document

Repeat the action for all paragraphs you want to copy. You can of course make a text selection in the source doc that contains more than one paragraph, and paste all of them into the target in one action.

Having said all this, there might be essier ways to achieve what you want to achieve. I cannot figure out the use case for your scenario. Why would you want to copy all the content from one document (or a range of them) into another? If you need all content of a series of documents combined into one other document, you can use text insets.

Kind regards from Amsterdam

4everJang

1 reply

4everJang
4everJangCorrect answer
Legend
November 29, 2018

You are trying to do something that is not possible in this way. The objexcts that are pushed into the array are paragraph objects, whicih is not the same as the paragraph content in FrameMaker. In fact, the Pgf object in the array might be a pointer instead of the real thing, but you can never be sure whether the script creates an actual object or just a pointer to an object., In any case, the Pgf only has a meaning (something correspondig to it) in the scope of the document. In your case, this would be the source document. When you try to paste into the target document, there is nothing referenced, as the scope has changed to another document.

To copy the actual paragraph from one doc to another, you need to do the following:

1. Make sure you have the source document open. Let's call this oSourceDoc.

2. Make sure you have the target document open. Let's call this oTargetDoc.

3. Select a paragraph in the source doc.

4. Create a text range that contains the entire paragraph.

     create new TextLoc (oTLoc1) to point to the beginning of the paragraph

     create new TextLoc (oTLoc2) to point to the end of the paragrap

     create new TextRange (oTRangeSource) with oTLoc1 and oTLoc2

5. Set the current text selection of the source doc to match the text range

     oSourceDoc.TextSelection = oTRangeSource

5. Use oSourcDoc.Copy( ) to copy the paragraph to the Windows clipboard

6. Create a text range in the target document

     create new TextLoc (oTLoc) at the insertion point

     create new TextRange (oTRangeTarget) from oTLoc and oTLoc (same TLoc used twice)

7. Set the current text selection of the target doc to match the new text range

8 Use oTargetDoc.Paste( ) to paste the Windows clipboard into the target document

Repeat the action for all paragraphs you want to copy. You can of course make a text selection in the source doc that contains more than one paragraph, and paste all of them into the target in one action.

Having said all this, there might be essier ways to achieve what you want to achieve. I cannot figure out the use case for your scenario. Why would you want to copy all the content from one document (or a range of them) into another? If you need all content of a series of documents combined into one other document, you can use text insets.

Kind regards from Amsterdam

4everJang

Known Participant
November 29, 2018

Hi,

Thank you for your response.

The intention behind this: I am creating a maintenance manual which contains multiple maintenance sections. I just want to copy all Warnings and Cautions from the maintenance sections and put the consolidated list into a Safety sheet, then I need to delete the duplicated warnings and cautions from the safety sheet.

4everJang
Legend
November 29, 2018

If you move to structured content - using DITA is the best choice - this type of thing is ridiculously easy.