Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save tristar500/92576037153a475cb989c2cfd07b77e5 to your computer and use it in GitHub Desktop.
Save tristar500/92576037153a475cb989c2cfd07b77e5 to your computer and use it in GitHub Desktop.
FrameMaker - Save to TEXT and PDF
//~ https://forums.adobe.com/message/9544659
//~ https://forums.adobe.com/message/9544924
$.writeln("START **************************************************************");
// This switch just keeps the developer from having to work the select dialog for each test
var production = true;
if(production == true){
var selectedFolder = Folder.selectDialog ("Select Folder with Source Files");
}else{
var selectedFolder = "C:\\Path\\To\\Documents";
}
if(selectedFolder !== null){
myFolder = new Folder(selectedFolder);
var myFiles = myFolder.getFiles("*.fm");
if(myFiles.length > 0){
$.writeln("Found " + myFiles.length + " source files to process.");
listFiles(myFiles);
}else{
alert("No FrameMaker files found in this folder");
}
}else{
$.writeln("!! JOB CANCELLED !!");
}
$.writeln("END **************************************************************");
function listFiles(selectedFiles){
for(var i = 0; i< selectedFiles.length; i++){
$.writeln("Working file number " + (i+1) + ": " + (selectedFiles[i].fsName) );
var doc = getDocument(selectedFiles[i].fsName);
saveToText(doc);
saveToPdf(doc);
}
return true;
}
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;
return docOrBook; // Return the document or book object.
}
else {
// If the document can't be open, print the errors to the Console.
PrintOpenStatus (retParm);
return null;
}
}
function saveToText (doc) {
var params, returnParams, saveName, i;
saveName = doc.Name.replace(/\.fm$/i, ".txt");
$.writeln("SaveToText: " + saveName);
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);
}
function saveToPdf (doc) {
var params, returnParams, saveName, i;
saveName = doc.Name.replace(/\.fm$/i, ".pdf");
$.writeln("SaveToPdf: " + saveName);
returnParams = new PropVals();
params = GetSaveDefaultParams();
i = GetPropIndex (params, Constants.FS_FileType);
params[i].propVal.ival = Constants.FV_SaveFmtPdf;
i = GetPropIndex (params, Constants.FS_SaveTextTblSetting);
params[i].propVal.ival = Constants.FV_SaveTblUserPref;
doc.Save (saveName, params, returnParams);
PrintSaveStatus (returnParams);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment