Skip to main content
Inspiring
February 22, 2023
Question

Extendscript: importing formats from one book component to all others

  • February 22, 2023
  • 0 replies
  • 160 views

Dear fellows,

 

Following my post regarding importing a specific book component's formats into the rest of the book components, here is my updated code. For a reason that is unclear to me, the code fails on the "

FS_FileIsOldVersion" open parameter with the "undefined is not an object" error. Will appreciate your inputs what could cause it.
#target framemaker

findCarrier();
importCarrier();

function findCarrier() {

    var book, matchedComps, CarrierFileName;

    book = app.ActiveBook;
    if (book.ObjectValid() === 1) {
        matchedComps = getMatchingComps(book, /Carrier/i, /Internal/i);
        return matchedComps[0].Name;
    }
}

function importCarrier() {

    var book, bookComp, targetDoc, CarrierFileName, openCarrierFM;
    book = app.ActiveBook;
    bookComp = book.FirstComponentInBook;
    if (book.ObjectValid() === 1) {
        CarrierFileName = findCarrier();

        while (bookComp.ObjectValid() === 1) {
            openCarrierFM = open(CarrierFileName);
            targetDoc = open(bookComp.Name);

            targetDoc.SimpleImportFormats(openCarrierFM, Constants.FF_UFF_COND | Constants.FF_UFF_VAR);
            targetDoc.SimpleSave(targetDoc, false);
            targetDoc.Close(1);

            bookComp = bookComp.NextBookComponentInDFSOrder;
        }

        openCarrierFM.Close(1);
    }
}

function getMatchingComps(book, findRegex, excludeRegex) {

    var matchedComps, bookComp, file;

    // An array of matched book components.
    matchedComps = [];

    // Loop through all of the components in the book.
    book = app.ActiveBook;
    bookComp = book.FirstComponentInBook;
    while (bookComp.ObjectValid() === 1) {
        // Make a file object for the component.
        file = new File(bookComp.Name);
        // See if the find regular expression matches the base 
        // file name and the exclude regex doesn't match.
        if ((findRegex.test(file.name) === true) &&
            (excludeRegex.test(file.name) === false)) {
            // Add it to the array.
            matchedComps.push(bookComp);
        }
        bookComp = bookComp.NextBookComponentInDFSOrder;
    }

    return matchedComps;
}

function open(filename) {

    var openProp = GetOpenDefaultParams();

    i = GetPropIndex(openProp, Constants.FS_FileIsOldVersion);

    openProp.propVal.ival = Constants.FV_DoOK;

    i = GetPropIndex(openProp, Constants.FS_FontNotFoundInCatalog);

    openProp.propVal.ival = Constants.FV_DoOK;

    i = GetPropIndex(openProp, Constants.FS_FontNotFoundInDoc);

    openProp.propVal.ival = Constants.FV_DoOK;

    i = GetPropIndex(openProp, Constants.FS_FileIsInUse);

    openProp.propVal.ival = Constants.FV_DoCancel;

    i = GetPropIndex(openProp, Constants.FS_AlertUserAboutFailure);

    openProp.propVal.ival = Constants.FV_DoCancel;

    retParm = new PropVals();

    OpenDoc = Open(filename, openProp, retParm);

    return OpenDoc;

}
    This topic has been closed for replies.