I have a script that reads a simple XML file, which has to be in the same folder as the script and with the same base name. For example, the script is called PublishOutput.jsx and the XML file is called PublishOutput.xml. The purpose of the script is to output Responsive HTML from a particular book. Here is the XML file that determines the script's parameters:
<?xml version="1.0" encoding="UTF-8"?><settings> <input>C:\data\scripts\SAS\20180212_InsertMarkers\BA2.book</input> <sts>C:\Program Files (x86)\Adobe\AdobeFrameMaker2015\fminit\Publisher\Default.sts</sts> <outputFolder>C:\DATA\Scripts\SAS\20180212_InsertMarkers\Output</outputFolder> <type>Responsive HTML5</type></settings>
The script works fine if it and the settings file don't reside in one of the startup folders. But, what I want to do is run this from the startup folder so it runs automatically when I launch FrameMaker. But here it doesn't work. When I launch FrameMaker, the script opens the book, but then when the Publish command is invoked, it launches another instance of FrameMaker and the whole process is short-circuited. Any ideas would be appreciated. If you want to test it, you will have to edit the values in the xml file to reflect paths on your computer. Here is the script:
#target framemaker
main ();
function main () {
// Get the settings from the xml file. var settings = getSettings (); if (settings.errorMsgs.length === 0) { // Check the settings for missing elements and invalid paths. settings = checkSettings (settings.xml); if (settings.errorMsgs.length === 0) { settings = publishOutput (settings); writeLog (settings.errorMsgs); } else { writeLog (settings.errorMsgs); } } else { writeLog (settings.errorMsgs); return; }
}
function checkSettings (settingsXml) {
var settings = {errorMsgs: []}, value; value = String (settingsXml.input); if (value !== "") { if (File (value).exists === true) { settings.input = value; } else { settings.errorMsgs.push ("Input file does not exist: " + value); } } else { settings.errorMsgs.push ("input element is missing or empty in the settings file."); } value = String (settingsXml.sts); if (value !== "") { if (File (value).exists === true) { settings.sts = value; } else { settings.errorMsgs.push ("Publish settings file does not exist: " + value); } } else { settings.errorMsgs.push ("sts element is missing or empty in the settings file."); } value = String (settingsXml.outputFolder); if (value !== "") { if (Folder (value).exists === true) { settings.folder = value; } else { settings.errorMsgs.push ("Output folder does not exist: " + value); } } else { settings.errorMsgs.push ("outputFolder element is missing or empty in the settings file."); } value = String (settingsXml.type); if (value !== "") { settings.type = value; } else { settings.errorMsgs.push ("type element is missing or empty in the settings file."); } return settings;
}
function publishOutput (settings) {
var book; book = getDocOrBook (settings.input, "Book"); if ((book) && (book.ObjectValid () === 1)) { // Send the parameters to the Publish client. setSts (settings.sts); setOutputFolder (settings.folder); callPublisher (settings.type); if (book.openedByScript === true) { book.Close (true); } } else { settings.errorMsgs.push ("The book could not be opened: " + settings.input); } return settings;
}
function setSts (sts) { var cmd = "SetMCPSetting " + sts; return CallClient ("FMPublisher", cmd);
}
function setOutputFolder (folder) { var cmd = "SetOutputLocation " + folder; return CallClient ("FMPublisher", cmd);
}
function callPublisher (type) { var cmd = "MCPPublish " + type; return CallClient ("FMPublisher", cmd);
}
function writeLog (msgs) {
var log; // Make a File object for the log file. log = new File ($.fileName).fsName.replace (/\.[^\.]+$/, ".log"); log = File (log); log.open ("w"); // Write the messages to the log. log.write (msgs.join ("\r")); // Close the log file. log.close ();
}
function getSettings () {
var settingsFile, e, settings = {errorMsgs: []}; // Make a File object for the settings XML file. settingsFile = new File ($.fileName).fsName.replace (/\.[^\.]+$/, ".xml"); settingsFile = File (settingsFile); if (settingsFile.exists === false) { settings.errorMsgs.push ("The settings file does not exist:"); settings.errorMsgs.push (settingsFile.fsName); return settings; } // Open and read the settings file. settingsFile.open ("r"); try { settings.xml = new XML (settingsFile.read ()); } catch (e) { settings.errorMsgs.push ("There was an error reading the settings file:"); settings.errorMsgs.push (settingsFile.fsName); settings.errorMsgs.push (e); } // Close the settings file. settingsFile.close (); // Return the settings object. return settings;
}
function getDocOrBook (filename, type) {
var docOrBook; // See if the document or book is already open. docOrBook = docOrBookIsOpen (filename, type); if (docOrBook) { return docOrBook; } else { // The document or book is not already open, so open it. return openDocOrBook (filename); }
}
function docOrBookIsOpen (filename, type) {
var file, docOrBook; // Make a File object from the file name. file = File (filename); // Uppercase the filename for easy comparison. filename = file.fullName.toUpperCase (); if (type === "Doc") { // Loop through the open documents in the session. docOrBook = app.FirstOpenDoc; while (docOrBook.ObjectValid ()) { // Compare the document’s name with the one we are looking for. if (File (docOrBook.Name).fullName.toUpperCase () === filename) { // The document we are looking for is open. docOrBook.openedByScript = false; return docOrBook; } docOrBook = docOrBook.NextOpenDocInSession; } } else { // type === "Book" // Loop through the open books in the session. docOrBook = app.FirstOpenBook; while (docOrBook.ObjectValid ()) { // Compare the book's name with the one we are looking for. if (File (docOrBook.Name).fullName.toUpperCase () === filename) { // The book we are looking for is open. docOrBook.openedByScript = false; return docOrBook; } docOrBook = docOrBook.NextOpenBookInSession; } }
}
function openDocOrBook (filename) {
var i = 0, docOrBook, openProps, retParm; // 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. i=GetPropIndex(openProps,Constants.FS_AlertUserAboutFailure); openProps[i].propVal.ival=false; i=GetPropIndex(openProps,Constants.FS_MakeVisible); openProps[i].propVal.ival=true; 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; // Attempt to open the document or book docOrBook = Open (filename,openProps,retParm); if (docOrBook.ObjectValid () === 1) { 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); }
}