Skip to main content
Known Participant
November 10, 2023
Question

Book file info custom variable

  • November 10, 2023
  • 2 replies
  • 475 views

Can I create a variable in the document footer that automatically shows information from the book file info? I have written the name of the document in the Title field (I right-clicked the book file and chose File Info...). I want the title to appear on all pages of the document.

    This topic has been closed for replies.

    2 replies

    Matt-Tech Comm Tools
    Community Expert
    Community Expert
    November 14, 2023

    If it's not easy to do right now, it seems like it would be easy to engineer and it's a great suggestion for the next update of the application!

    https://tracker.adobe.com

     

    -Matt

    -Matt

    -Matt Sullivan, FrameMaker Course Creator, Author, Trainer, Consultant
    Community Expert
    November 10, 2023

    Hi, @Peter32610444u0rc ,

    afaik it is not possible.

    Possible solutions:

    1) As you are using a book-file you are having more than one chapter-file I assume.

    Put the information (title) in one chapter with a paragraph-format of its own (set to not printing) and use cross-references.

    2) Use a script.

    Regards

    Stephan

    Known Participant
    November 10, 2023

    Hi,

    OK, maybe I should forget the idea altogether. Thank you for taking the time to answer! Regards Peter

    frameexpert
    Community Expert
    Community Expert
    November 10, 2023

    Here is a script that will set variables in each document of the book that will match the book's File Info fields. For example, if you set the Title and Author fields, you will get Title and Author variables in each of the book's components that match the values you set in the book's File Info dialog box.

     

    To use the script, copy and paste it into a plain text file and save it with a .jsx extension. Open you book and set the desired fields in the File Info dialog box. With the book active, choose File > Script > Run and select the script. Alternatively, you can use File > Script > Library and add the script to the Script Catalog and run it from there.

     

    #target framemaker
    
    main ();
    
    function main () {
    
        var book;
        
        book = app.ActiveBook;
        if (book.ObjectValid () === 1) {
            if (book.PDFDocInfo.length > 0) {
                processBook (book, book.PDFDocInfo);
            }
        }
    }
    
    function processBook (book, bookInfo) {
        
        var bookComp, doc;
            
        // Loop through all of the components in the book.
        bookComp = book.FirstComponentInBook;
        while (bookComp.ObjectValid ()) {
            // Only process document components (skip groups, folders, etc.).
            if (bookComp.ComponentType === 512) {
                // Get the document returned in a JavaScript object.
                doc = getDocument (bookComp.Name, undefined, false);
                if ((doc) && (doc.ObjectValid () === 1)) {
                    // Display the file name to the book status area.
                    book.StatusLine = "Processing " + new File (bookComp.Name).displayName;
                    // Call the function to set the document's variables.
                    processDoc (doc, bookInfo);
                    // If the document was opened by the script, save it and close it.
                    if (doc.openedByScript === true) {
                        //doc.SimpleSave (bookComp.Name, false);
                        doc.Close (true);
                    }
                }
            }
            bookComp = bookComp.NextBookComponentInDFSOrder;
        }
        // Reset the book status line.
        book.StatusLine = "";
    }
    
    function processDoc (doc, bookInfo) {
        
        var count, i, varFmt;
        
        count = bookInfo.length;
        for (i = 0; i < count; i += 2) {
            varFmt = setVarFmt (bookInfo[i], bookInfo[i + 1], doc);
        }
    }
    
    function setVarFmt (name, format, doc) {
        
        var varFmt;
        
        varFmt = doc.GetNamedVarFmt (name);
        if (varFmt.ObjectValid () === 0) {
            varFmt = doc.NewNamedVarFmt (name);
        }
    
        varFmt.Fmt = format;
    
        return varFmt;
    }
    
    function getDocument (filename, structApp, visible) {
        
        // filename = full path to the file to open
        // structapp = optional structured application name
        // visible = make the document visible (true or false)
        
        var doc;
        
        // See if the document is already open.
        doc = docIsOpen (filename);
        if (doc) {
            return doc; // Return the open document
        } 
        else {
            // The document is not already open, so call a function to open it.
            return openDocOrBook (filename, structApp, visible);
        }
    }
    
    function docIsOpen (filename) {
        
        // filename = Full path to the file
        
        var file, name, doc;
        
        // Make a File object from the file name.
        file = new File (filename);
        // Uppercase the filename for easy comparison.
        name = file.fullName.toUpperCase ();
    
        // Loop through the open documents in the session.
        doc = app.FirstOpenDoc;
        while (doc.ObjectValid () === 1) {
            // Compare the document’s name with the one we are looking for.
            if (new File (doc.Name).fullName.toUpperCase () === name) {
                // The document we are looking for is open.
                // Since the script didn't open it, add a property to indicate that.
                doc.openedByScript = false;
                return doc;
            }
            doc = doc.NextOpenDocInSession;
        }
    }
    
    function openDocOrBook (filename, structApp, visible) {
        
        // filename = full path to the file to open
        // structapp = optional structured application name
        // visible = make the document visible (true or false)
        
        var openProps, retParm, i, docOrBook;
    
        // Get default property list for opening documents.
        openProps = GetOpenDefaultParams ();
        // Get a property list to return any error messages.
        retParm = new PropVals ();
    
        // Set specific open property values to open the document.
        // Don't display interface error dialog boxes.
        i = GetPropIndex (openProps, Constants.FS_AlertUserAboutFailure);
        openProps[i].propVal.ival = false;
        // Determine whether or not to make the document or book visible.
        i = GetPropIndex (openProps, Constants.FS_MakeVisible);
        openProps[i].propVal.ival = visible;
        // Open it if it is an older version without prompting.
        i = GetPropIndex (openProps, Constants.FS_FileIsOldVersion);
        openProps[i].propVal.ival = Constants.FV_DoOK;
        // If the file is locked, reset it and open it.
        i = GetPropIndex (openProps, Constants.FS_FileIsInUse);
        openProps[i].propVal.ival = Constants.FV_ResetLockAndContinue;
        // Open the file even if it is read only.
        i = GetPropIndex (openProps, Constants.FS_OpenFileNotWritable);
        openProps[i].propVal.ival = Constants.FV_DoOK;
        // Ignore font errors.
        i = GetPropIndex (openProps, Constants.FS_FontChangedMetric);
        openProps[i].propVal.ival = Constants.FV_DoOK;
        i = GetPropIndex (openProps, Constants.FS_FontNotFoundInCatalog);
        openProps[i].propVal.ival = Constants.FV_DoOK;
        i = GetPropIndex (openProps, Constants.FS_FontNotFoundInDoc);
        openProps[i].propVal.ival = Constants.FV_DoOK;
        // Ignore unresolved cross reference and text insets.
        i = GetPropIndex (openProps, Constants.FS_RefFileNotFound);
        openProps[i].propVal.ival = Constants.FV_AllowAllRefFilesUnFindable;
        // If a structured application is supplied, set its value.
        if (structApp !== undefined) {
            i = GetPropIndex (openProps, Constants.FS_StructuredOpenApplication);
            openProps[i].propVal.sval = structApp;
        }
    
        // Attempt to open the document.
        docOrBook = Open (filename, openProps, retParm);
    
        if (docOrBook.ObjectValid () === 1) {
            // Add a property to the document or book, indicating that the script opened it.
            docOrBook.openedByScript = true;
        }
        else {
            // If the document can't be open, print the errors to the Console.
            PrintOpenStatus (retParm);
        }
    
        return docOrBook; // Return the document or book object.
    }