Skip to main content
Participant
May 30, 2017
Answered

file.Save() is not a function

  • May 30, 2017
  • 1 reply
  • 1578 views

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);

    }

    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.propVal.ival = Constants.FV_SaveFmtText;

// Save the document as TXT.

  file.Save(saveName, params, returnParamsp);

  return true;

}

This topic has been closed for replies.
Correct answer frameexpert

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?


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 reply

frameexpert
Community Expert
Community Expert
May 30, 2017

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.propVal.ival = Constants.FV_SaveFmtText;

    i = GetPropIndex (params, Constants.FS_SaveTextTblSetting);

    params.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.

www.frameexpert.com
Participant
May 30, 2017

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);

        myFile.open();

       saveAsText(myFile);

        myFile.close();

    }

    return true;

}

inside of my listFiles function?