Welcome Dialog

Welcome to the Community!

We have a brand new look! Take a tour with us and explore the latest updates on Adobe Support Community.


Global Variables only Called Once then Lost their Scope!

Enthusiast ,
Aug 03, 2021 Aug 03, 2021

Copy link to clipboard

Copied

Hi Experts,

I Have a Problem with my Script and hope helping me, and thanks in advance, i still learning javascript and try to learn somthing new everyday, i have a script that draw modeless interface (palette) and have button including (option) that make another new palette (for options), i made the variables as globals for the option palette, but the problem is the global variables is only called once!, and the script lose the global variable scope!.

sample.jpg

here is the code (i made it short  as i can to be just example) :

https://pastebin.com/HzGjzE43 

 

 

 

//Global Variables only Called Once then Lost their Scope!
#targetengine "session1";
var w = new Window("palette", {independent:true}); //Main Palette Windows
var findoptions = new Window("palette"); //Options Palette

function Main() {
    // Check to see whether any InDesign documents are open.
    // If no documents are open, display an error message.
    if (app.documents.length > 0) {
        var myDoc = app.activeDocument;
    }else{
        // No documents are open, so display an error message.
        alert("No InDesign documents are open. Please open a document and try again.");
    }
}

//---------------------------------------------------------
//Making Palettes Windows
//---------------------------------------------------------
#targetengine "session1";
var w = new Window("palette", {independent:true}); //Main Palette Windows
var findoptions = new Window("palette"); //Options Palette
//gqmanager is the (GREP Query Manager) outside the main Function
w.text = "Test the Connection Between Global Variables and Palettes";
w.preferredSize.width = 500;
w.alignChildren = ["center", "center"];  //"left";
w.orientation = "column"; //"row"; 
w.spacing = 10;
w.margins = 16;

//Parent - Input Panel Prepare
var InputPanel = w.add("panel", undefined, undefined, { name: "panel1" });
InputPanel.text = "Text Find : ";
InputPanel.preferredSize.width = 1000;
InputPanel.orientation = "row";
InputPanel.alignChildren = ["center", "center"];
InputPanel.spacing = 10;
InputPanel.margins = 16;

//Children -  input Panel Inside Prepare
var myInputPanelInside = InputPanel.add("group", undefined, { name: "myInput" });
//--Adding Find What
myInputPanelInside.add("statictext", undefined, "Find What :");
//myInputPanelInside.alignment = "center";
var myGREPString = myInputPanelInside.add("edittext", undefined, "SAMPLE");
myGREPString.helpTip = "Enter Your Text"
myGREPString.characters = 20;
myGREPString.enabled = true;
myGREPString.preferredSize.width = 460;

var Button1 = myInputPanelInside.add("button", undefined, "Options");

//Parent - Radio Panel Prepare
var RadioPanel = w.add("panel", undefined, undefined, { name: "panel2" });
RadioPanel.text = "Select Desired Option : ";
RadioPanel.preferredSize.width = 1000;
RadioPanel.orientation = "row";
RadioPanel.alignChildren = ["center", "center"];
RadioPanel.spacing = 10;
RadioPanel.margins = 16;

//Children -  input Panel Inside Prepare
var myRadioPanelInside = RadioPanel.add("group", undefined, { name: "myRadio" });
myRadioPanelInside.preferredSize.width = 500;
myRadioPanelInside.alignChildren = ["center", "center"];

//Adding Radio Buttons 
var radio1 = myRadioPanelInside.add("radiobutton", undefined, "Option 1");
var radio2 = myRadioPanelInside.add("radiobutton", undefined, "Option 2");
var radio3 = myRadioPanelInside.add("radiobutton", undefined, "Option 3");
radio1.preferredSize.width = 200;
radio2.preferredSize.width = 200;
radio3.preferredSize.width = 200;
//Previous Default Condition
radio1.value = true;

var myButtonGroup = w.add("group");
myButtonGroup.alignment = "center";

var Button2 = myButtonGroup.add("button", undefined, "Show Alert Due Options");
var Button3 = myButtonGroup.add("button", undefined, "Exit");

Button1.onClick = function () {
    CalltheFindOptions();
    }
Button2.onClick = function () { Find(); };
function Find() {
    doRadioButtonOpt();
}

Button3.onClick = function() {Canceled();};
function Canceled() {
    ExitSure();
}

//After Drawing Interface
var a = w.show();

function ExitSure() {
  var a = w.close();
  exit(0);
}

//User Selection for Radio Buttons
function doRadioButtonOpt() {
    myDoc = app.activeDocument; 
  if (radio1.value == true) {
    TestVars();
    }
}

function TestVars() {
    #targetengine "session1";
    var myDoc = app.activeDocument
    var TimeMs = Number(SliderControlText.text); //Converting Text to Number
    //Show Results Found as User Wish
if (DontShowResults.value == true) { //no Show only Apply
    alert("you Select not to Show Results!");
            }else{ //Direct Show and Apply
        if (ShowResultsDirect.value == true) {
    alert("you Select to Show Results in real time!");
            }else{ //Show and Apply By WaitinhTime!
                if (ShowResults.value == true) { //Show and Apply
            alert("you Select to Show Results with Specific time!");
            $.sleep(TimeMs); //Wait ms
                    }
                }
            }
alert("Do you need somthing else?, try again", "Finish Report");
}

var DontShowResults; 
var ShowResultsDirect; 
var ShowResults; 
var SliderControlText;
var slider;
//--------------------------------------------Building the Find Options Palette-----------------------------------------//
//--------------------------------------------------------------------------------------------------------------------------------//
function CalltheFindOptions() {
#targetengine "session1";
//Find Options Window
findoptions.text = "Find Options";
//Parent - Input Panel Prepare
SelectPanel = findoptions.add("panel", undefined, undefined, { name: "panel1" });
SelectPanel.text = " Find Options : ";
SelectPanel.preferredSize.width = 1000;
SelectPanel.orientation = "row";
SelectPanel.alignChildren = ["center", "center"];
SelectPanel.spacing = 10;
SelectPanel.margins = 16;

//Children -  input Panel Inside Prepare
mySelectPanelInside = SelectPanel.add("group", undefined, { name: "mySelOpt" });
DontShowResults = mySelectPanelInside.add("checkbox", undefined, "Don't Show Results");
DontShowResults.value = true; //by Default
DontShowResults.alignment = "left"; 

ShowResultsDirect = mySelectPanelInside.add("checkbox", undefined, "Show Results");
ShowResultsDirect.value = false; //by Default

ShowResults = mySelectPanelInside.add("checkbox", undefined, "Show Results Delayed in milliseconds(Ms) :");
ShowResults.value = false; //by Default

//Adding Slider to Control MS Time
SliderControlText = mySelectPanelInside.add ("edittext", undefined, 10, {readonly: false}); //read only prevent user Entering Nums
SliderControlText.characters = 3;
slider = mySelectPanelInside.add ("slider {minvalue: 1, maxvalue: 100, value: 10}");

//Slider Listener Plus SliderControl Text Listener
slider.onChanging = function () {SliderControlText.text = slider.value;} //Listen to Slider
var c = findoptions.show();
}

 

 

 

so my question is how to make the variables not losing its scope and retain in the memory? as long the script run, as an Example if the user move the slider and  hit (Show alert due options) its only run once and then lose the scope, even the slider no longer interact with the user and update text box, please test the code to see the problem, and thank again for any help or advice.

Best
M.Hasanain
TOPICS
Scripting

Views

144

Likes

Translate

Translate

Report

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 2 Correct answers

Adobe Community Professional , Aug 03, 2021 Aug 03, 2021
I put an "if" statement at the beginning of the function, line 141. It seems to work correctly. However, the controls inherits each parameter from the last time the palette was closed.That makes sense, but does it meet your needs? Also, the targetengine declaration in each function is probably unnecessary. Because it has the same targetengin name as at the beginning of the code.

Likes

Translate

Translate
Adobe Community Professional , Aug 03, 2021 Aug 03, 2021
If you want to reset the parameters every time, redefine findoption at the beginning of the function "CalltheFindOptions".function CalltheFindOptions() { findoptions = new Window("palette");This is a bit rough technique, but I personally don't care too much. Finally, lines 1-15 of the code seem completely unnecessary.

Likes

Translate

Translate
Adobe Community Professional ,
Aug 03, 2021 Aug 03, 2021

Copy link to clipboard

Copied

Hi, 

In your code, it looks like you're adding its controlls in the "findoptions" palette every time you press the "Options" button.
If you're aiming to reset "findoptions" palette's controlls, the "findoptions" palette must discard its contents each time you close it.
Perhaps another set of controls has been created outside the window size range of the "findoptions" palette opened a second time or later, and global variables are scoped to that new controls.
And since the controls are not displayed on the palette, it cannot be operated.

 

IMG_5551.JPG

 

Here are some suggestions for solving this problem.
-Stop declaring the "findoptions" palette as a global variable and create a new one every time.
or
· Check if the controls already exists before creating it in the function "CalltheFindOptions".

example.

スクリーンショット 2021-08-04 13.50.44.png

 

Regards.

Likes

Translate

Translate

Report

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
Enthusiast ,
Aug 03, 2021 Aug 03, 2021

Copy link to clipboard

Copied

Thank you @ajabon grinsmith  for the reply,

stopped declaring the "findoptions" palette as a global variable and create a new one every time, but unlucky didn't solve the problem! , this for method one, but for method two what shouid i do if i need to discard the content of palette (Find Options) and reset it everytime user click the button ? and thank you again.

Best
M.Hasanain

Likes

Translate

Translate

Report

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
Adobe Community Professional ,
Aug 03, 2021 Aug 03, 2021

Copy link to clipboard

Copied

スクリーンショット 2021-08-04 15.00.56.png

 

I put an "if" statement at the beginning of the function, line 141. It seems to work correctly.
However, the controls inherits each parameter from the last time the palette was closed.

That makes sense, but does it meet your needs?

 

Also, the targetengine declaration in each function is probably unnecessary. Because it has the same targetengin name as at the beginning of the code.

Likes

Translate

Translate

Report

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
Adobe Community Professional ,
Aug 03, 2021 Aug 03, 2021

Copy link to clipboard

Copied

If you want to reset the parameters every time, redefine findoption at the beginning of the function "CalltheFindOptions".

function CalltheFindOptions() {
    findoptions = new Window("palette");

This is a bit rough technique, but I personally don't care too much.

 

Finally, lines 1-15 of the code seem completely unnecessary.

Likes

Translate

Translate

Report

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
Enthusiast ,
Aug 03, 2021 Aug 03, 2021

Copy link to clipboard

Copied

Thanks a lot @ajabon grinsmith for your great help, but please i want to understand what is the relation with (if) statement in (reseting the values!) , i just want to understand it clearly!, and thanks again.

Best
M.Hasanain

Likes

Translate

Translate

Report

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
Adobe Community Professional ,
Aug 04, 2021 Aug 04, 2021

Copy link to clipboard

Copied

I have shown two pieces of code. They cannot be applied simultaneously in one code.

 

In the code with the "if" statement added, when the condition (whether controls exist) is met, the controls addition part of the palette "findoption" is skipped and only "show" is executed. I think it's very simple.
The cause of your trouble was the excessive addition of Controls. So I tried not to do it.
In this case, the Controls parameters are retained. Because I haven't changed anything. It's a very simple theory.

 

The code that redefined the palette "findoption" respects the additional controls part in the function "CalltheFindOptions".
The palette "findoption" will be overwritten and Controls will be added to the empty palette. So the parameters are initialized.
I don't know much more than writing a script for my purposes, but I'm imagining that it may be necessary to do something like free memory before overwriting the palette.

However, I thought that there would be almost no burden on the system at this level.

Simple should be the best for your understanding.

Is this enough for my answer?

Likes

Translate

Translate

Report

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
Enthusiast ,
Aug 04, 2021 Aug 04, 2021

Copy link to clipboard

Copied

LATEST

Yes @ajabon grinsmith now thats very clear, Thanks again for clearing the theroy, i learned a lot today.

Best
M.Hasanain

Likes

Translate

Translate

Report

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