4 Replies Latest reply on Nov 20, 2009 6:50 AM by Bob Stucky

    JS - Conditional include

    madhenkie

      Is it possible to conditionaly include a file

       

      for example:

       

      if (something == true){

           #include "first.jsx"

      } else {

           #include "second.jsx"

      }

        • 1. Re: JS - Conditional include
          Dirk Becker Level 4

          You could use eval() the same way as the jsxbin loader of the XHTML Export.

           

          Dirk

          • 2. Re: JS - Conditional include
            madhenkie Level 1

            Do you mean like this:

             

            if (condition) {

                 eval("#include \"somefile.jsx\"");

            } else {

                 eval("#include \"anotherfile.jsx\"");

            }

            • 3. Re: JS - Conditional include
              Dirk Becker Level 4

              No. Of course I should have looked twice, the relevant source has been changed since CS3.

               

              With namespaces removed, the code looked like this:

               

              getScriptsFolderPath = function() {

              try {

              var script = app.activeScript;

              } catch(e) {

              // we are running from the ESTK

              var script = File(e.fileName);

              }

              return script.path + '/../';

              }

               

               

              loadScript = function(filename) {

              var file = File(getScriptsFolderPath() + filename );

              var script = undefined;

              if (file.exists) {

              file.open();

              script = file.read();

              file.close();

              }

              // we return the script rather than calling eval() right here

              // because the results of eval() are only valid within the

              // scope of the function that calls eval()

              return script;

              }

               

              // load the script

              script = loadScript('XHTMLExportMenuItem.jsxbin');

              if( script != undefined) {

              // execute it

              eval(XHTMLExportMenuItemLoader.script);

              }

              • 4. Re: JS - Conditional include
                Bob Stucky Adobe Employee

                The best way to load a script file is $.evalFile()

                 

                It doesn't have the scoping problem of eval()

                 

                Regards

                 

                Bob