Open every component of a book , copy its content and paste it into a single frame
Hello,
I have plenty of documents split as components in books. I have a specific need that requires to flatten all these components in a single file and help a publication/conversion process to HTML.
I currently have several chunks of code that succeed independantly the following actions:
1. crawl a book, check every component, and open them individually
2. create a temporary file that will receive the content copied in the components
3. copy content in component from the first paragraph to the last available
3. paste it in the temporary file
Now, I've tried to gather all of this and make it work, but without success. I don't have any error provided, and I suspect a conflict with the identification of the doc where content must be pasted.
I just start to get acquainted with Extendscript and I miss more tutorials or resources.
Thanks a lot for any pointer and tips!
counter++;
comp.ComponentIsSelected = true;
var compType = comp.ComponentType;
comp.ComponentIsSelected = false;
// goto the next component
comp = comp.NextBookComponentInDFSOrder;
}
var comp = book.FirstComponentInBook;
while(comp.ObjectValid())
{
if(comp.ComponentType == Constants.FV_BK_FILE)
{
if(doc.ObjectValid())
{
alert("Successfully opened: \n\n" + comp.Name);
// variables for the copy/paste process
var doc = OpenFile(comp.Name);
var mainflow = doc.MainFlowInDoc;
var tframe = mainflow.FirstTextFrameInFlow;
var pgf = tframe.FirstPgf;
var lastPgf = mainflow.LastTextFrameInFlow.LastPgf;
//copy the paragraphs from start to end
tRange.beg.obj = pgf;
tRange.beg.offset = 0;
tRange.end.obj = lastPgf;
tRange.end.offset = Constants.FV_OBJ_END_OFFSET;
doc.TextSelection = tRange;
doc.Copy(0);
// Paste to the other document called duplicateDoc
duplicateDoc.NewSeriesObject(Constants.FO_Pgf, pgf);
tRange.beg.obj = pgf.NextPgfInFlow;
//tRange.beg.offset =0;
//tRange.end = tRange.beg;
tRange.end = lastPgf;
duplicateDoc.TextSelection = tRange;
duplicateDoc.Paste(0);
}
else
{
alert("Could not open: \n\n"
+ comp.Name
+ "\n\nMaybe the file doesn't exist?\n\n"
+ "We are going to delete this component now "
+ "because we want to do a book update, but "
+ "it will fail with this bogus component.");
var compToDelete = comp;
comp = comp.PrevBookComponentInDFSOrder;
if(!comp.ObjectValid()) comp = comp.FirstComponentInBook;
compToDelete.Delete();
}
}
comp = comp.NextBookComponentInDFSOrder;
}
// required for the crawling of the components
var index = GetPropIndex(props, Constants.FS_ShowBookErrorLog);
props[index].propVal.ival = true;
var returnp = new PropVals();
}
else
alert("No book is open or active. Cannot run the script.");
// global function used to handle the warning messages
function OpenFile(path)
{
var index;
props = GetOpenDefaultParams();
index = GetPropIndex(props, Constants.FS_AlertUserAboutFailure);
if(index > -1)
props[index].propVal.ival = false;
index = GetPropIndex(props, Constants.FS_FileIsInUse);
if(index > -1)
props[index].propVal.ival = Constants.FV_ResetLockAndContinue;
index = GetPropIndex(props, Constants.FS_BookIsInUse);
if(index > -1)
props[index].propVal.ival = Constants.FV_ResetLockAndContinue;
index = GetPropIndex(props, Constants.FS_LockCantBeReset);
if(index > -1)
props[index].propVal.ival = Constants.FV_DoOK;
index = GetPropIndex(props, Constants.FS_DisallowMIF);
if(index > -1)
props[index].propVal.ival = false;
index = GetPropIndex(props, Constants.FS_DisallowXml);
if(index > -1)
props[index].propVal.ival = false;
index = GetPropIndex(props, Constants.FS_FileIsOldVersion);
if(index > -1)
props[index].propVal.ival = Constants.FV_DoOK;
index = GetPropIndex(props, Constants.FS_FontChangedMetric);
if(index > -1)
props[index].propVal.ival = Constants.FV_DoOK;
index = GetPropIndex(props, Constants.FS_FontNotFoundInCatalog);
if(index > -1)
props[index].propVal.ival = Constants.FV_DoOK;
index = GetPropIndex(props, Constants.FS_FontNotFoundInDoc);
if(index > -1)
props[index].propVal.ival = Constants.FV_DoOK;
index = GetPropIndex(props, Constants.FS_LanguageNotAvailable);
if(index > -1)
props[index].propVal.ival = Constants.FV_DoOK;
index = GetPropIndex(props, Constants.FS_OpenAsType);
if(index > -1)
props[index].propVal.ival = Constants.FV_AUTORECOGNIZE;
index = GetPropIndex(props, Constants.FS_UpdateTextReferences);
if(index > -1)
props[index].propVal.ival = Constants.FV_DoNo;
index = GetPropIndex(props, Constants.FS_UpdateXRefs);
if(index > -1)
props[index].propVal.ival = Constants.FV_DoNo;
index = GetPropIndex(props, Constants.FS_UseRecoverFile);
if(index > -1)
props[index].propVal.ival = Constants.FV_DoNo;
index = GetPropIndex(props, Constants.FS_UseAutoSaveFile);
if(index > -1)
props[index].propVal.ival = Constants.FV_DoNo;
index = GetPropIndex(props, Constants.FS_OpenFileNotWritable);
if(index > -1)
props[index].propVal.ival = Constants.FV_DoOK;
index = GetPropIndex(props, Constants.FS_RefFileNotFound);
if(index > -1)
props[index].propVal.ival = Constants.FV_DoOK;
returnp = new PropVals();
var file = Open(path, props, returnp);
return file;
}
