5 Replies Latest reply on Jul 6, 2009 10:31 AM by richardh6

    [JS, CS2] Is there a variable containing the name of the calling script?

    richardh6 Level 1

      Hi,

       

      I need to have a script be able to know its own name.

       

      This is because I have a situation where I have a find/change routine for cleaning up and styling text that I need to run on a few different kinds of stories (I work for a newspaper and they're along the lines of "default body copy," oped page," "arts section," etc.), but the vast bulk of the find/change routine is the same for all of them.  My co-workers have to be able to have each of the routines be in different scripts to they can click on them.  So I want each specialized script to have nothing in it but one #include statement which will #include the text of one main script.  That way whenever I make changes to it, I don't have to save it as a million slightly different versions.

       

      So if I need to be able to determine the name (or path) of the script that has been called, so the script can then perform the few specialized actions needed for that story type.  Is there a way to do that?

       

      This would be something like the php variable $argv[0].

       

      Thanks.

        • 1. Re: [JS, CS2] Is there a variable containing the name of the calling script?
          Thomas B. Nielsen Level 3

          function getActiveScriptPath() {

              try {

                  var script = app.activeScript;

              } catch(e) {

                  // we are running from the ESTK

                  var script = File(e.fileName);

              }

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

          }

          1 person found this helpful
          • 2. Re: [JS, CS2] Is there a variable containing the name of the calling script?
            richardh6 Level 1

            Thanks.

             

            Strangely, if you run it from the ESTK it give you a "No Script is active" error, but if you push through and tell it not to clear runtime errors, it finishes the script properly.

             

            What I actually needed was not the folder path but the name of the file itself, just to use as a kind of parameter so that the script can know which actions to perform.  So if I modify the script Thomas B. Nielsen provided as follows, it's perfect:

             

            function getActiveScriptName() {

                try {

                    var script = app.activeScript;

                } catch(e) {

                    // we are running from the ESTK

                    var script = File(e.fileName);

                }

                return script.name;

            }

             

            By the way, is there any way to pass parameters to Indesign scripts directly (like the rest of the argv array in php)?

            • 3. Re: [JS, CS2] Is there a variable containing the name of the calling script?
              Thomas B. Nielsen Level 3

              I dont think i understand what you mean by,

              is there any way to pass parameters to Indesign scripts directly (like the rest of the argv array in php)?

               

              Where should these variables come from? Please elaborate.

               

              You can send parameters to a script like in the following example (from the javascript examples)

               

              //DoScriptParameters.jsx
              //An InDesign CS3 JavaScript
              //
              //Shows how to send parameters to a script called using
              //the doScript method.
              var myParameters = ["Hello from DoScript", "Your message here."];
              var myJavaScript = "alert(\"First argument: \" + arguments[0] + \"\\rSecond argument: \" + arguments[1]);";
              app.doScript(myJavaScript, ScriptLanguage.javascript, myParameters);
              if(File.fs == "Windows"){
                  var myVBScript = "msgbox arguments(1), vbOKOnly, \"First argument: \" & arguments(0)";
                  app.doScript(myVBScript, ScriptLanguage.visualBasic, myParameters);
              }
              else{
                  var myAppleScript = "tell application \"Adobe InDesign CS3_J\\rdisplay dialog(\"First argument\" & item 1 of arguments & return & \"Second argument: \" & item 2 of arguments & return & end tell";
                  app.doScript(myAppleScript, ScriptLanguage.applescriptLanguage, myParameters);
              }

              • 5. Re: [JS, CS2] Is there a variable containing the name of the calling script?
                richardh6 Level 1

                Actually, I'm not sure if what you are suggesting works in InDesign CS2.

                 

                What I mean by passing parameters to a script is this:  one script would run, and it would pass parameters to another script.  I'm aware that there is no way to pass any variables to a script that is directly double-clicked by the user, but one that was run by another script should be able to receive parameters, which it looks like it does in InDesign CS3, from your example.  However, from the InDesign CS2 Scripting Reference, it appears that its app.doScript method only takes two parameters:  the file, and the language name.

                 

                I tried it, too, just to make sure, and it didn't work.

                 

                Am I doing something wrong, or is the ability to pass parameters from one script to another something that was added in InDesign CS3?