3 Replies Latest reply on Feb 23, 2014 6:36 PM by frameexpert

    Accessing file system

    gisteppen Level 1

      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

        • 1. Re: Accessing file system
          frameexpert Level 4

          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

          • 2. Re: Accessing file system
            gisteppen Level 1

            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

            • 3. Re: Accessing file system
              frameexpert Level 4

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