Skip to main content
Known Participant
July 2, 2009
Answered

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

  • July 2, 2009
  • 1 reply
  • 1118 views

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.

This topic has been closed for replies.
Correct answer interesting_Flower157F

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);
}

1 reply

interesting_Flower157F
Inspiring
July 2, 2009

function getActiveScriptPath() {

    try {

        var script = app.activeScript;

    } catch(e) {

        // we are running from the ESTK

        var script = File(e.fileName);

    }

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

}

richardh6Author
Known Participant
July 2, 2009

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)?

interesting_Flower157F
Inspiring
July 2, 2009

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);
}