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

Accessing file system

New Here ,
Feb 21, 2014 Feb 21, 2014

Copy link to clipboard

Copied

My script needs a Framemaker template file in creating a new file. I've made it work by hard-coding the path to the template in the script:

sFilename = "C:\\Users\\gisteppen\\Desktop\\fm change bars\\dev\\changes-template.fm";

I want to make this easy to distribute. I want the script to look for this template file in whatever directory the script resides in, but I find it won't work if I just put the name of the template without the path like this:

sFilename = "changes-template.fm";

Is there a way to get the current directory where the script is from the file system? Then I could construct the absolute path that Fm seems to need.

Or maybe there is a better way to go about this?

Thanks,

Mark

TOPICS
Scripting

Views

699

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

correct answers 1 Correct answer

Community Expert , Feb 22, 2014 Feb 22, 2014

You can do something like the code below, which finds a settings file with the same name as the script, but with a .cfg extension.

    // Make a File object for the settings XML file.
    var settingsFile = new File($.fileName.replace (/\.jsx$/i , ".cfg"));
    if (settingsFile.exists === false) {
        $.writeln ("Settings file does not exist: " + settingsFile.fsName);
    }

The $.fileName property gives you the path to the currently running script. Here I am replacing the .jsx extension with .cfg.

...

Votes

Translate

Translate
Community Expert ,
Feb 22, 2014 Feb 22, 2014

Copy link to clipboard

Copied

You can do something like the code below, which finds a settings file with the same name as the script, but with a .cfg extension.

    // Make a File object for the settings XML file.
    var settingsFile = new File($.fileName.replace (/\.jsx$/i , ".cfg"));
    if (settingsFile.exists === false) {
        $.writeln ("Settings file does not exist: " + settingsFile.fsName);
    }

The $.fileName property gives you the path to the currently running script. Here I am replacing the .jsx extension with .cfg.

In your case, you could use something like this:

    // Make a File object for the template.
    var template = new File($.fileName.replace (/[^\/]+$/, "changes-template.fm"));
    if (template.exists === false) {
        $.writeln ("Template does not exist: " + template.fsName);
    }

One important thing: make sure you save the script before using this code. $.fileName returns (Script#) from an untitled script.

-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
New Here ,
Feb 23, 2014 Feb 23, 2014

Copy link to clipboard

Copied

Nice! Not only did you point me at $.fileName to get the path to the running script, which would have been plenty, you gave me a good code implementation with a nice regex to boot. So thanks!

I was looking at the File and Folder objects and not finding what I wanted. I'm going to look more closely at those $.* tools.

I have a question, should I somehow get rid of that File object I'm using? Is there a best practice? Should it be closed like if I open a FrameMaker file with Open()?

Thanks,

Mark

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 ,
Feb 23, 2014 Feb 23, 2014

Copy link to clipboard

Copied

LATEST

You don't have to worry about closing a File object unless you actually open it with the open method.

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