4 Replies Latest reply on Mar 3, 2010 1:48 AM by Muppet Mark

    How to call Perl script from Javascript with arguments

    gopaljay78 Level 1

      Hi,

       

      Could anyone help me on how to call Perl script from Javascript and also how to pass arguments to perl script and return values from perl script.

       

      Thanks,

      Gopal

        • 1. Re: How to call Perl script from Javascript with arguments
          John Hawkinson Level 5

          This is OS-dependent, because JavaScript can't execute command lines. So you have to call VBscript or Applescript to do it.

          I guess someone should write an OS-independent function for it.

          This thread addressed it for OSX: Calling shell/perl from javascript
          This thread addressed it for Windows:copy folder with files in structured format

           

          Under OSX (untested):

          function shell(cmd) {
              var call ='do shell script "'+
                  cmd.replace(/\\/g,"\\\\").replace(/"/g,'\\"')+
                  '"';
              return app.doScript(call,
              ScriptLanguage.APPLESCRIPT_LANGUAGE);
          }
          
          var n1=10;
          var n2=20;
          alert("Perl returned: "+
                shell("perl -e 'print "+n1+"*"+n2+";'"));
          

           

          Which sets javascrip variables of n1 and n2 to 10 and 20, and then calls perl to multiply them, and displays the output in an alert dialog box. Quoting can get a bit tricky.

           

          Can you be more specific about what you want to do if the above is not sufficient?

          • 2. Re: How to call Perl script from Javascript with arguments
            Muppet Mark Level 5

            John, just in case this you've overlooked this bridge can issue system commands. app.system();

            I've been able to send some ditto, rsync and alike via bridgetalk message.

            Might be worth a play if you like this stuff.

            • 3. Re: How to call Perl script from Javascript with arguments
              John Hawkinson Level 5

              Wow, thanks Muppet Mark, I didn't know that! It does seem to have a major downside, which is that it does not return arguments. I guess a script can write to a file which you could then open in Javascript, but that feels pretty awkard.

               

              (In my case, I have a bunch of machines that have InCopy that don't have Bridge installed, but that's probably not the case for most people. And I suppose I could install Bridge on them...)

               

              I wonder why nobody mentioned that in any of the above two threads...

              • 4. Re: How to call Perl script from Javascript with arguments
                Muppet Mark Level 5

                John, thats what I've been doing for the time being (whilst trying to learn). Just piping stdout to file. Then have JavaScript read in the results. Output re-directed back to Bridge was something I was hoping (adobe) may add later (Im only CS2). Coming over form the AppleScript side I've got used to calling on shell from time to time and having the results directed straight back. I only looked into this as a possible work around to my version of CS not being able to execute a shell with permissions set.