Highlighted

Running a Publish script at startup

Adobe Community Professional ,
Mar 11, 2018

Copy link to clipboard

Copied

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

    i=GetPropIndex(openProps,Constants.FS_MakeVisible);

    openProps.propVal.ival=true;

    i=GetPropIndex(openProps,Constants.FS_FileIsOldVersion);

    openProps.propVal.ival=Constants.FV_DoOK;

    i=GetPropIndex(openProps,Constants.FS_FileIsInUse);

    openProps.propVal.ival=Constants.FV_ResetLockAndContinue;

    i=GetPropIndex(openProps,Constants.FS_FontChangedMetric);

    openProps.propVal.ival=Constants.FV_DoOK;

    i=GetPropIndex(openProps,Constants.FS_FontNotFoundInCatalog);

    openProps.propVal.ival=Constants.FV_DoOK;

    i=GetPropIndex(openProps,Constants.FS_FontNotFoundInDoc);

    openProps.propVal.ival=Constants.FV_DoOK;

    i=GetPropIndex(openProps,Constants.FS_RefFileNotFound);

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

    }

}

TOPICS
Scripting

Views

1.2K

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more

Running a Publish script at startup

Adobe Community Professional ,
Mar 11, 2018

Copy link to clipboard

Copied

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

    i=GetPropIndex(openProps,Constants.FS_MakeVisible);

    openProps.propVal.ival=true;

    i=GetPropIndex(openProps,Constants.FS_FileIsOldVersion);

    openProps.propVal.ival=Constants.FV_DoOK;

    i=GetPropIndex(openProps,Constants.FS_FileIsInUse);

    openProps.propVal.ival=Constants.FV_ResetLockAndContinue;

    i=GetPropIndex(openProps,Constants.FS_FontChangedMetric);

    openProps.propVal.ival=Constants.FV_DoOK;

    i=GetPropIndex(openProps,Constants.FS_FontNotFoundInCatalog);

    openProps.propVal.ival=Constants.FV_DoOK;

    i=GetPropIndex(openProps,Constants.FS_FontNotFoundInDoc);

    openProps.propVal.ival=Constants.FV_DoOK;

    i=GetPropIndex(openProps,Constants.FS_RefFileNotFound);

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

    }

}

TOPICS
Scripting

Views

1.2K

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Mar 11, 2018 0
Enthusiast ,
Mar 11, 2018

Copy link to clipboard

Copied

Hi Rick,

I have not a real solution, but maybe a workaround:

What about storing the original script elsewhere, but calling it via a script stored in startupfolder

var OriginalFile = new File(pathToOriginalScript);

$.evalFile(OriginalFile);

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Mar 11, 2018 1
Adobe Community Professional ,
Mar 11, 2018

Copy link to clipboard

Copied

Hi Klaus, That you for the reply and suggestion. The same problem occurs when I move my script out of the startup folder and replace it with your suggested script. However, I did some more research and realize that I can call my script outside of the startup folder with a command line call:

C:\Program Files (x86)\Adobe\Adobe ExtendScript Toolkit CC>"ExtendScript Toolkit

.exe" -run "C:\Users\Rick\Documents\Adobe Scripts\PublishOutput.jsx"

The script has to be in the Adobe Scripts folder because it is a "trusted location" and prevents the warning about running the script. Thanks again!

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Mar 11, 2018 0
Advocate ,
Mar 11, 2018

Copy link to clipboard

Copied

Hello Rick,

Any jsx as well as jsxbin in the startup folder is executed after Fm launches, as you know. At that time you do not have a book yet that might have to be published, which is probably why it does not work when placing your stuff in the startup folder. In general, I would not put any jsx into the startup folder - as I do not want to give a way all the sources in all cases and I do not want the hassle of also copying the include files into the startup folder. Keeping the startup folder clean and easy to manage by only allowing jsxbin packages there.

Just replace the Main( ) function call with a meaningful top-level function call in each particular module and you will be fine. If you need to have function calls added to one of the menus, add a module that sets up the menus and commands plus a generic command handler that calls the functions in the various jsxbin modules. I have created one such generic command handling module as a jsxbin, which takes an xml file to create menus and commands and call the right functions. That xml file is placed in a folder in the AppData space where I may also place other assets my scripts require. That AppData space is always available and in a place that you can easily get to via the Fm properties (UserSettingsDir).

If you need more info on how I manage this, we can have an off-forum discussion about it. But not this week, as I am working at the Adobe Office in Noida right now. Making stuff for the next Fm release. 🙂

Ciao for now

Jang

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Mar 11, 2018 0
New Here ,
Sep 07, 2018

Copy link to clipboard

Copied

Hi,

I'm using the function from your code to open fm files but it does not help me.

function openDocOrBook (filename)

Can you please help me ?

Thanks

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Sep 07, 2018 0