Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

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

Community Beginner ,
Jul 02, 2009 Jul 02, 2009

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.

TOPICS
Scripting
1.0K
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Engaged , Jul 02, 2009 Jul 02, 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."];
va

...
Translate
Engaged ,
Jul 02, 2009 Jul 02, 2009

function getActiveScriptPath() {

    try {

        var script = app.activeScript;

    } catch(e) {

        // we are running from the ESTK

        var script = File(e.fileName);

    }

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

}

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Jul 02, 2009 Jul 02, 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)?

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
Jul 02, 2009 Jul 02, 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);
}

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Jul 02, 2009 Jul 02, 2009

That's it.  Thanks.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Jul 06, 2009 Jul 06, 2009
LATEST

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?

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines