2 Replies Latest reply on Sep 19, 2014 10:26 AM by Tete San

    Relative path and XML methods

    Tete San

      Hi, I am learning to integrate xml with JSX, I've read the document "JavaScript Tools Guide CS6" within the Help Extend the Tool Kit menu, and I am using an example from another thread here the community is already working ... But still, I'm two questions are:

       

      - Currently use an absolute path (line # 2 below) to find the file on the desktop. However, I need the relative path to find the folder that the script is (my xml file will be along with the script and not the desktop ...)

       

      - In line # 12 got:

      file.open ("r", "TEXT", "????");

      What are these three arguments, "r", "TEXT" and "????" the open() method? I did not find documentation about the open(), read() and close() methods, know where I can find?

       

      If anyone can help me I would be very grateful, thanks. Below is my code.

       

      //
      var file = new File("~/Desktop/bookstore.xml");
      readXMLFile(file);
      //
      function readXMLFile(file) {
              if (!file.exists) {
                      throw "Dont find: " + deodeURI(file.absoluteURI);
              }
              file.encoding = "UTF8";
              file.lineFeed = "unix";
              //
              file.open("r", "TEXT", "????");
              var bookXmlStr = file.read();
              file.close();
              return bookstoreXML = new XML(bookXmlStr);
      };
      //
      writeXMLFile(file,bookstoreXML)
      //
      function writeXMLFile(file, xml) {
              if (!(xml instanceof XML)) {
                      throw "Bad XML parameter";
              }
              file.encoding = "UTF8";
              file.open("w", "TEXT", "????");
              // unicode signature, this is UTF16 but will convert to UTF8 "EF BB BF"
              file.write("\uFEFF");
              file.lineFeed = "unix";
              file.write(xml.toXMLString());
              file.close();
      };
      //
      
        • 1. Re: Relative path and XML methods
          cchimi Level 2

          You can get the folder of the current script using either

           

          Folder(File($.fileName).parent).fullName

           

          or

           

          Folder (app.activeScript.parent).fullName

           

          I think the latter may only work when launching a script from the Scripts Panel; I always use the former because I don't run my scripts that way, and I have never had any issues.

           

          As for file.open, if you're using ExtendScript Toolkit you can read about it by opening the Object Model Viewer and selecting Core JavaScript Classes in the browser. Open takes three arguments. The first is the mode (read, write, edit, append), the second and third are Mac-only values that describe type and creator.

           

          If you're not using the toolkit, you can read about the core JavaScript stuff that ExtendScript implements here: Adobe InDesign CS6 (8.0) Object Model JS: Table of Contents, JavaScript Base Classes (there are other versions for other versions of the object model if you need something different).

          • 2. Re: Relative path and XML methods
            Tete San Level 1

            Many thanks cchimi !!!

             

            The relative path worked perfectly, I used "Folder(File($.FileName).parent).fullName" seemed actually safer.

             

            Did not know the "Object Model Viewer",very useful as well as the link jongware. Thanks.