• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

Importing formats from another file

Community Beginner ,
Mar 26, 2012 Mar 26, 2012

Copy link to clipboard

Copied

Hi everyone,

First, I should say thanks for all the posts. It has definitely helped a newbie like me.

I'm trying to import formats from another file. When set to import from the doc itself, it works great. I just can't figure out how to reference another file.

Here is what I have (that's not working) :-}

function ImportFormats(doc) {

    var FormatFlags = Constants.FF_UFF_PGF | Constants.FF_UFF_FONT | Constants.FF_UFF_PAGE |

          Constants.FF_UFF_TABLE | Constants.FF_UFF_COLOR | Constants.FF_UFF_REFPAGE | Constants.FF_UFF_REMOVE_EXCEPTIONS;

   

    var FactsFile =File("C:\pg000_facts_table.fm");

  doc.SimpleImportFormats (FactsFile, FormatFlags);

    }\

Thanks again!

TOPICS
Scripting

Views

1.4K

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
community guidelines
Advocate ,
Mar 29, 2012 Mar 29, 2012

Copy link to clipboard

Copied

Heather,

The doc that is requested as the first parameter to SimpleImportFormats is NOT simply the file name. You need to open the file using the global functions SimpleOpen or Open to get a document handle. That handle is the one you pass to SimpleImportFormats.

Using SimpleIOpen is easier to code but if you want to suppress the File Open dialog (by setting the interactive parameter to false), any kind of problem preventing Frame to simply open the file will not be notified. I would invest the couple of extra coding lines and use Open, as you can tell Frame to ignore all kinds of possible stuff (updating references, missing fonts etc.) and simply open the requested file.

Also, note that the backslash has to be escaped with a backslash in a string, i.e. your filename should be "C:\\pg000_facts_table.fm". The backslash is used to indicate special characters like a newline (\n) or a tab (\t) etc. One of those special characters is the backslash (\\).

Welcome to the wonderful world of ExtendScript !

Jang

Votes

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
community guidelines
Explorer ,
Jul 25, 2017 Jul 25, 2017

Copy link to clipboard

Copied

I'm writing a small script to import certain formats to a lot of files.

I want the user to choose the source, but File.openDialog() returns a [File] and SimpleImportFormats() demands a [Doc].

How can make a prompt-dialog that returns a [Doc]? Or is there a way to change a [File] to [Doc]?

I have a few documents already opened in the session.

I bet there is something faster than my method:

var sourcefile = File.openDialog ("Choose file", "*.fm", false)

var sourcename = sourcefile.displayName;

var doc = app.FirstOpenDoc;

while(doc.ObjectValid()) {

     if (sourcename = doc.Name.split("\\").slice(-1)) {

          var sourcefileasDoc = doc;

          break;}

     doc = doc.NextOpenDocInSession;

}

Regards

Votes

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
community guidelines
Community Expert ,
Jul 25, 2017 Jul 25, 2017

Copy link to clipboard

Copied

LATEST

You import formats from an open document (a Doc object), so you have to open the File object that you return from the dialog box. Here is a set of functions that you can plug into any script to get a Doc object from a file name. It first tests if the document is already open; if it is it returns it, if not, it calls another function to open it.

function getDocument (filename, structApp, visible) {

   

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

    i=GetPropIndex (openProps,Constants.FS_MakeVisible);

    openProps.propVal.ival=visible;

    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;

    if (structApp !== undefined) {

        i=GetPropIndex (openProps,Constants.FS_StructuredOpenApplication);

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

}

The getDocument function is the one you call. It has three arguments:

1) filename is the absolute path to the FrameMaker file. If you have a File object, you must use the fsname property of the File object to get the platform-specific full path to the file.

2) structApp is the structured application required to open the document if it is structured. For an unstructured file, just use undefined.

3) visible is true if you want the document to be opened visibly, false if you want it to open invisibly.

The returned Doc object will have a new property on it called openedByScript. This is set to true if the function opened the document and false if it was already opened. You can use this property to decide whether or not to close the document when you are done with it.

-Rick

Votes

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
community guidelines