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

Set global variables using #targetengine

Engaged ,
Sep 21, 2016 Sep 21, 2016

When i write a script i would like to be able to use global variable so that i don't have to set the variables in each script. (eg. I use var doc = app.activeDocument; and a few others, in basically every script I write) I tried to set the variables as a startup script using #targetengine "mine" but that runs before a document opens (even with the "afterOpen" eventListener) invoking an error: "No documents are open." I also tried to create a menu button to set the variables :

#targetengine "mine"

var myMenuItemName = "Set Variables";

// Create the Script Menu Action (myMenuItemAction)

var myMenuItemAction = app.scriptMenuActions.add(myMenuItemName);

// Add an Event Listener

myMenuItemAction.addEventListener(

    /*event type*/   'onInvoke',

    /*event handler*/ setVariablesFunction

    );

// Create a new menu item in the Help submenu

var fileMenu = app.menus.item("$ID/Main").submenus.item("$ID/&Help");

fileMenu.menuItems.add(myMenuItemAction);

function setVariablesFunction(){

var doc = app.activeDocument;

var test = "Test ";

alert(test + "Success")

}

but the second script would cause an error: "test is undefined"

#targetengine "mine"

alert(test + "failed")

I am able to set the variables if i run the script manually as

#targetengine "mine"

var test = "success";

and then

#targetengine "mine"

alert(test);

and i know i can set a shortcut to this (which i may have to do anyway) but I don't really want to have to remember to run the script each time i open indesign.

Hopefully I haven't complicated the question too much. Can anyone provide guidance as to what I need to do to make this work preferably as a startup but any other suggestions will be accepted.

TOPICS
Scripting
674
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 Expert ,
Sep 21, 2016 Sep 21, 2016

If you need an open document to run a start-up script, open a document, do your thing, and close it.

But it won't do you any good because app.activeDocument refers to that document only and its reference becomes invalid the moment you close it.

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
Mentor ,
Sep 23, 2016 Sep 23, 2016

Besides what Peter wrote, just don't bother with what you are trying to do.

You will pay for your laziness many, many times over.

There is very little place for globules.

@Peter

Completely off the topic!

You wrote on another thread about having Javascript higher on the syntax list.

If you use Java syntax instead then I highly doubt that anyone would notice.

Could be wrong but you can give it a go

Regards,

Trevor

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 Expert ,
Sep 23, 2016 Sep 23, 2016
LATEST

Aha -- will do!

P.

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