7 Replies Latest reply on May 31, 2017 6:21 AM by frameexpert

    file.Save() is not a function

    davidd87907370

      I'm trying to create a script to save a folder full of FM files to TEXT.

       

      My script is running fine until I get to file.Save() and I get a 'file.Save is not a function' error. Can someone help understand where this has gone wrong? Thank you.

       

      var selectedFolder = Folder.selectDialog ("Select Folder with Source Files");
      
      if(selectedFolder != null){
          myFolder = new Folder(selectedFolder);
          var myFiles = myFolder.getFiles("*.fm");
      }else{
          alert('Could not access that folder');
      }
      
      if(myFiles.length > 0){
          listFiles(myFiles);
      }else{
          alert('No FrameMaker files found in this folder');
      }
      function listFiles(selectedFiles){
          for(var i = 0; i< selectedFiles.length; i++){
           saveAsText(selectedFiles[i]);
          }
          return true;
      }
      
      //~ https://forums.adobe.com/thread/1819414
      function saveAsText (file) {
         // Replace the hash and file extension
        var newName = file.absoluteURI.replace (/\.[\S\.^\.\\]+$/,".txt");   
      
        // Get required parameters for the save function.
        var params = GetSaveDefaultParams();
       var returnParamsp = new PropVals();
       var saveName = file.rename(newName);
        if(file.error ){
          alert( "ERROR:" + file.error + " FILE:" + newName);
        }
        var i = GetPropIndex(params, Constants.FS_FileType);
        params[i].propVal.ival = Constants.FV_SaveFmtText;
       // Save the document as TXT.
        file.Save(saveName, params, returnParamsp);
        return true;
      }
      
        • 1. Re: file.Save() is not a function
          frameexpert Level 4

          Here is a function that I have that works:

           

          function saveToText (doc) {    
              
              var params, returnParams, saveName, i;
              
              saveName = doc.Name.replace(/\.fm$/i, ".txt");
              returnParams = new PropVals();
              params = GetSaveDefaultParams();
              i = GetPropIndex (params, Constants.FS_FileType);
              params[i].propVal.ival = Constants.FV_SaveFmtText;
              i = GetPropIndex (params, Constants.FS_SaveTextTblSetting);
              params[i].propVal.ival = Constants.FV_SaveTblUserPref;
          
              doc.Save (saveName, params, returnParams);
              PrintSaveStatus (returnParams);
              
          }
          

           

          As I look at your code, the problem is that you need to open each document in FrameMaker in order to save it. The Save method is valid on a Doc object and the only way to get a Doc object is to open the file with FrameMaker.

          • 2. Re: file.Save() is not a function
            davidd87907370 Level 1

            As I look at your code, the problem is that you need to open each document in FrameMaker in order to save it. The Save method is valid on a Doc object and the only way to get a Doc object is to open the file with FrameMaker.

            Do you mean physically open the file in Frame and then run the script?

             

            Or do I use something like

             

            function listFiles(selectedFiles){
                for(var i = 0; i< selectedFiles.length; i++){
                    var myFile = new File(selectedFiles[i]);
                    myFile.open();
                   saveAsText(myFile);
                    myFile.close();
                }
                return true;
            }
            

             

            inside of my listFiles function?

            • 3. Re: file.Save() is not a function
              frameexpert Level 4

              Just to clarify: you need to open the files in FrameMaker, but of course you can do it programmatically, not manually in the interface. Here are a set of functions to open a FrameMaker document. You call the getDocument function, passing it the platform-specific full path to the file.

               

              function getDocument (filename) {
                  
                  // 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, undefined, 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, structuredApplication, 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 (structuredApplication !== 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.
              }
              
              • 4. Re: file.Save() is not a function
                davidd87907370 Level 1

                Thanks so much for all of the help.

                 

                Now however, in openDocOrBook() Open() is returning an invalidobject and PrintOpenStatus is telling me FV_BadFileName (specified filename was invalid) in the FM console. Do you know what could be causing this?

                • 5. Re: file.Save() is not a function
                  frameexpert Level 4

                  What are you passing as the filename argument to the getDocument function? If you have a file object, you should pass the fsName property, which is the platform-specific, absolute path to the file.

                  1 person found this helpful
                  • 6. Re: file.Save() is not a function
                    davidd87907370 Level 1

                    Ding ding! We have a winner. Thank you so much!

                    • 7. Re: file.Save() is not a function
                      frameexpert Level 4

                      My pleasure!