3 Replies Latest reply on Jul 25, 2017 5:22 AM by frameexpert

    Importing formats from another file

    heatherb555 Level 1

      Hi everyone,

       

      First, I should say thanks for all the posts. It has definitely helped a newbie like me.

       

      I'm trying to import formats from another file. When set to import from the doc itself, it works great. I just can't figure out how to reference another file.

       

      Here is what I have (that's not working) :-}

       

      function ImportFormats(doc) {

          var FormatFlags = Constants.FF_UFF_PGF | Constants.FF_UFF_FONT | Constants.FF_UFF_PAGE |

                Constants.FF_UFF_TABLE | Constants.FF_UFF_COLOR | Constants.FF_UFF_REFPAGE | Constants.FF_UFF_REMOVE_EXCEPTIONS;

         

          var FactsFile =File("C:\pg000_facts_table.fm");

       

        doc.SimpleImportFormats (FactsFile, FormatFlags);

       

          }\

       

      Thanks again!

        • 1. Re: Importing formats from another file
          4everJang Level 3

          Heather,

           

          The doc that is requested as the first parameter to SimpleImportFormats is NOT simply the file name. You need to open the file using the global functions SimpleOpen or Open to get a document handle. That handle is the one you pass to SimpleImportFormats.

           

          Using SimpleIOpen is easier to code but if you want to suppress the File Open dialog (by setting the interactive parameter to false), any kind of problem preventing Frame to simply open the file will not be notified. I would invest the couple of extra coding lines and use Open, as you can tell Frame to ignore all kinds of possible stuff (updating references, missing fonts etc.) and simply open the requested file.

           

          Also, note that the backslash has to be escaped with a backslash in a string, i.e. your filename should be "C:\\pg000_facts_table.fm". The backslash is used to indicate special characters like a newline (\n) or a tab (\t) etc. One of those special characters is the backslash (\\).

           

          Welcome to the wonderful world of ExtendScript !

           

          Jang

          • 2. Re: Importing formats from another file
            FabianK007 Level 1

            I'm writing a small script to import certain formats to a lot of files.

            I want the user to choose the source, but File.openDialog() returns a [File] and SimpleImportFormats() demands a [Doc].

             

            How can make a prompt-dialog that returns a [Doc]? Or is there a way to change a [File] to [Doc]?

             

            I have a few documents already opened in the session.

            I bet there is something faster than my method:

             

            var sourcefile = File.openDialog ("Choose file", "*.fm", false)
            var sourcename = sourcefile.displayName;
            
            var doc = app.FirstOpenDoc;
            
            while(doc.ObjectValid()) {
                 if (sourcename = doc.Name.split("\\").slice(-1)) {
                      var sourcefileasDoc = doc;
                      break;}
                 doc = doc.NextOpenDocInSession;
            }
            

             

            Regards

            • 3. Re: Importing formats from another file
              frameexpert Level 4

              You import formats from an open document (a Doc object), so you have to open the File object that you return from the dialog box. Here is a set of functions that you can plug into any script to get a Doc object from a file name. It first tests if the document is already open; if it is it returns it, if not, it calls another function to open it.

               

              function getDocument (filename, structApp, visible) {
                  
                  // See if the document is already open.
                  var doc = docIsOpen (filename);
                  if (doc) {
                      return doc;
                  } else {
                      // The document is not already open, so open it.
                      return openDocOrBook (filename, structApp, false);
                  }
              }
              
              function docIsOpen (filename) {
                  
                  // Make a File object from the file name.
                  var file = File (filename);
                  // Uppercase the filename for easy comparison.
                  var name = file.fullName.toUpperCase ();
              
                  // Loop through the open documents in the session.
                  var doc = app.FirstOpenDoc;
                  while (doc.ObjectValid () === 1) {
                      // Compare the document’s name with the one we are looking for.
                      if (File (doc.Name).fullName.toUpperCase () === name) {
                          // The document we are looking for is open.
                          doc.openedByScript = false;
                          return doc;
                      }
                      doc = doc.NextOpenDocInSession;
                  }
              }
              
              function openDocOrBook (filename, structApp, visible) {
                  
                  var i = 0, docOrBook = 0;
              
                  // Get default property list for opening documents.
                  var openProps = GetOpenDefaultParams ();
                  // Get a property list to return any error messages.
                  var retParm = new PropVals ();
              
                  // Set specific open property values to open the document.
                  i=GetPropIndex (openProps,Constants.FS_AlertUserAboutFailure);
                  openProps[i].propVal.ival=false;
                  i=GetPropIndex (openProps,Constants.FS_MakeVisible);
                  openProps[i].propVal.ival=visible;
                  i=GetPropIndex (openProps,Constants.FS_FileIsOldVersion);
                  openProps[i].propVal.ival=Constants.FV_DoOK;
                  i=GetPropIndex (openProps,Constants.FS_FileIsInUse);
                  openProps[i].propVal.ival=Constants.FV_ResetLockAndContinue;
                  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;
                  i=GetPropIndex (openProps,Constants.FS_RefFileNotFound);
                  openProps[i].propVal.ival=Constants.FV_AllowAllRefFilesUnFindable;
                  if (structApp !== undefined) {
                      i=GetPropIndex (openProps,Constants.FS_StructuredOpenApplication);
                      openProps[i].propVal.sval=structuredApplication;
                  }
              
                  // 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.
              }
              

               

              The getDocument function is the one you call. It has three arguments:

               

              1) filename is the absolute path to the FrameMaker file. If you have a File object, you must use the fsname property of the File object to get the platform-specific full path to the file.

               

              2) structApp is the structured application required to open the document if it is structured. For an unstructured file, just use undefined.

               

              3) visible is true if you want the document to be opened visibly, false if you want it to open invisibly.

               

              The returned Doc object will have a new property on it called openedByScript. This is set to true if the function opened the document and false if it was already opened. You can use this property to decide whether or not to close the document when you are done with it.

               

              -Rick

              1 person found this helpful