9 Replies Latest reply: Oct 8, 2014 3:38 AM by Klaus Göbel RSS

    Can a script execute another script?

    Michael Müller-Hillebrand CommunityMVP

      Dear colleagues,

       

      I am not talking about calling a function from another file for which I used the #include directive. I want to execute a .jsx that I know only at runtime.

       

      Until now I have found this: If a script file contains

       

      #target framemaker

       

      it is possible to use File(lvFilename).execute() (without the target instruction the script would simply open in ESTK).

       

      But: The execute() method triggers a warning that I should execute only scripts from trustworthy sources (or so…).

       

      Is there any other method to execute other scripts? Or is there a way to make my scripts trustworthy?

       

      - Michael

        • 1. Re: Can a script execute another script?
          Ian Proudfoot CommunityMVP

          Hi Michael,

          You could use the eval() method to do what you need. Here's a simple example that pops up an alert from the Alert.jsx file.

           

          var scriptFile = File('/c/Projects/ExtendScript/Alert.jsx');
          var script;

           

          scriptFile.open('r');
          script = scriptFile.read();
          scriptFile.close();

           

          eval(script);

           

          Caution: some say that eval() is evil, use with care.

           

          regards

          Ian

          • 2. Re: Can a script execute another script?
            Wiedenmaier Community Member

            Hi Michael,

             

            this should also work like this:

             

            CallClient("ScriptingSupport", "[YourPath]\[YourFile].jsx");

             

            I haven't tried it with ES, but in FDK it works well, so I think, it also works with ES in that way.

             

            bye

            Markus

            • 3. Re: Can a script execute another script?
              Michael Müller-Hillebrand CommunityMVP

              Ian,

               

              Thanks a lot for the idea with eval(), but removing the script content from its original location disables features like #include or $.fileName, unless I do some ugly workarounds there.

               

              - Michael

              • 4. Re: Can a script execute another script?
                Michael Müller-Hillebrand CommunityMVP

                Am 14.08.2011 um 20:38 schrieb Wiedenmaier:

                 

                this should also work like this:

                 

                CallClient("ScriptingSupport", "[YourPath]\[YourFile].jsx");

                 

                I haven't tried it with ES, but in FDK it works well, so I think, it also works with ES in that way.

                 

                Markus, this looks far better than eval() but unfortunately it does not work.

                 

                Well, it would be a recursive call, ScriptingSupport calling itself, which is something different from any other client calling a script.

                 

                - Michael

                • 5. Re: Can a script execute another script?
                  Ian Proudfoot CommunityMVP

                  Hi Michael,

                   

                  #include will work with eval() if you use an absolute file path. The best way to do that is to prepend an #includepath directive to the string that is passed to eval();

                   

                  Ian

                  • 6. Re: Can a script execute another script?
                    Ian Proudfoot CommunityMVP

                    Michael, here's another eval() idea:

                     

                    var scriptFile = File('/c/Projects/ExtendScript/Alert.jsx');
                    var script = '#include' + scriptFile.fullName;

                     

                    eval(script);

                     

                    I've tried this and it handles nested #includes and $.fileName. I can't be sure that it would work in every situation however. I feel that there must be a more elegant way...

                     

                    Ian

                    • 7. Re: Can a script execute another script?
                      4everJang Community Member

                      Reviving an old thread, as the answers so far did not solve my current related problem.

                       

                      I have tried to use both methods ( the CallClient suggested by Markus and the 'eval' suggested by Ian ) but in this case they do not seem to work. I am wondering if there is any option at all:

                       

                      I have a binary script file (jsxbin) to execute from another script. CallClient does not seem to do anything at all and the 'eval' method clearly expects a jsx file, as it gives me an unspecified syntax error when trying to run a jsxbin file. I am using the jsxbin file as that takes care of all the nasty include file hassle and is a self-contained executable. The problem is that I need to run it from another executable script file and the script to be executed is only known at runtime (as in the problem Michael originally indicated).

                       

                      Any suggestions ? Anyone at Adobe who knows whether this would work at all ?

                       

                      Jang

                      • 8. Re: Can a script execute another script?
                        Wiedenmaier Community Member

                        Hi Jang,

                        CallClient doesn't work from ExtendScript to ExtendScript, as there is the same FDK Client used for executing different scripts. CallClient works only from one FDK Client to another.

                        So if you want to use CallClient, you need your own small FDK Client, which controls the external script and your script.

                        AFAIK it's not possible to use eval for jsxbin files.

                         

                        If the external jsxbin has a command created to execute that function, you can crawl for the command name and execute it with the fcode.

                         

                        Hope this helps

                        Markus

                        • 9. Re: Can a script execute another script?
                          Klaus Göbel Community Member

                          Hi Michael,

                           

                          for me, it works with:

                           

                          $.evalFile("pathToMyFile/myfile.jsxbin")