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

[ScriptUI] edittext fiddle (show/hide and set)

New Here ,
May 10, 2012 May 10, 2012

Cheers,

I'm writing a small script that does some some find/change stuff. My idea is to output any error info to a edittext field nested in a panel, and only show this textfield if my grep function fails. I've read Peer Kahrels ScriptUI Basics (thank you for that reference ), but I'm still not sure how to update the textfield outside of my create-palette-function.

Can you help a hopeless newbie?

showPalette();

function showPalette() {

    var j_logo = new File("~/Desktop/Jysk_GREP/jysk_logo.png");

    var e_logo = new File("~/Desktop/Jysk_GREP/env_logo.png");

    var myDialog = new Window('palette', 'Jysk Search-Replace');

    var myIconGroup1 = myDialog.add('group', undefined, '');

        myIconGroup1.add('image', undefined, j_logo);

    var myGroup = myDialog.add('group', undefined, '');

    myGroup.orientation = 'row';

        var myPanel1 = myGroup.add('panel', undefined, 'Berthold');

            myPanel1.margins = [15,20,15,15];

            var bDK = myPanel1.add('button', undefined, 'DK', {name:'DK'});

            var bSE = myPanel1.add('button', undefined, 'SE', {name:'SE'});

        var myPanel2 = myGroup.add('panel', undefined, 'Compacta');

            myPanel2.margins = [15,20,15,15];       

            var cDK = myPanel2.add('button', undefined, 'DK', {name:'DK'});

            var cSE = myPanel2.add('button', undefined, 'SE', {name:'SE'});   

    var myGroup2 = myDialog.add('group', undefined);

            var myAlertPanel = myGroup2.add('panel', undefined);

            var list = myAlertPanel.add('edittext', undefined, ["test","test"].toString(), {multiline: true, scrolling: true});

            list.maximumSize.height = myDialog.maximumSize.height-100;

            list.maximumSize.width = 204;

    myDialog.show();   

}

Best wishes,

Rasmus

TOPICS
Scripting
2.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

Community Expert , May 12, 2012 May 12, 2012

Ah, like that. The section 'Communication between windows' (in the ScriptUI guide) explains how you could do it, but doesn't make something clear, so here's a summary.

First you need to do you window as a palette. And you need to name your edittext control:

. . .

var myText = w.add ("edittext", [0, 0, 150, 70], "", {multiline: true, name: 'runGrepErrors'});

. . .

Now you can access that control with any other script:

function runGrep(params) {

     try {

          //do text change stuff

     } catch(e) {

...
Translate
Community Expert ,
May 11, 2012 May 11, 2012

Rasmus,

Could explain in some more detail what you want to do?

Peter

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
New Here ,
May 11, 2012 May 11, 2012

Cheers Peter,

Apologies for not beeing specific enough. The script palette lets the user apply some predefined grep commands by calling a runGrep() method via event onClick. If runGrep() catches an error I'd like to return this error to the dialog, show the edittext field and set the error message as content. The idea is to notify of any errors in the script dialog directly instead of having to write error stuff to log files. Is this possible, in your opinion?

function showPalette() {

    var myDialog = new Window('palette', 'Jysk Search-Replace');   

    var j_logo = new File("~/Desktop/Jysk_GREP/jysk_logo.png");

    var myIconGroup1 = myDialog.add('group', undefined, '');

        myIconGroup1.add('image', undefined, j_logo);

    var myGroup = myDialog.add('group', undefined, '');

    myGroup.orientation = 'row';

    //------- Berthold buttons -------           

        var myPanel1 = myGroup.add('panel', undefined, 'Berthold');

            myPanel1.margins = [15,20,15,15];

            //------- Berthold DK -------                               

            var bDK = myPanel1.add('button', undefined, 'DK', {name:'DK'});

                //------- event handling -------           

                bDK.onClick = function() {

                    alert("You've clicked on bDK!");                   

                    var myFolder = Folder.selectDialog ("Choose root");

                    runGrep([Folder(myFolder+"/AllCountries"),Folder(myFolder+"/DK")]);   

                }

}

function runGrep(array) {

    //grep stuff here

    //return any error message to dialogs edittext field

}

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 ,
May 11, 2012 May 11, 2012

I guess what you could do is to catch any errors and add them to the palette's edittext control. Something like this (the advanced editor appears to have disappeared):

try

    {

    // find something

    }

catch (e)

    {

    list.text = e.message

    // or if you want to add the message to what's aleady there:

    // list.text = list.text + \r\r + e.message

    }

Where list is the edittext control in your palette.

Is that what you're after?

Peter

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
New Here ,
May 12, 2012 May 12, 2012

Cheers Peter,

This is what I'm trying to do - but I'd like to set the list.text from my runGrep() method. Can you do that?

createWindow(params) {

     var w = new Window ("dialog", "Multiline");

     var myText = w.add ("edittext", [0, 0, 150, 70], "", {multiline: true});

     myText.text = "Line 1\rLine 2\rLine 3\rLine 4\rLine 5\rLine 6\r";

     myText.active = true;

     w.show ();

}

function runGrep(params) {

     try {

          //do text change stuff

     } catch(e) {

          //set myText.text = e.description;

     }

}

BTW. code formatting is availabe if you edit your post

Best wishes,

Rasmus

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 ,
May 12, 2012 May 12, 2012

Ah, like that. The section 'Communication between windows' (in the ScriptUI guide) explains how you could do it, but doesn't make something clear, so here's a summary.

First you need to do you window as a palette. And you need to name your edittext control:

. . .

var myText = w.add ("edittext", [0, 0, 150, 70], "", {multiline: true, name: 'runGrepErrors'});

. . .

Now you can access that control with any other script:

function runGrep(params) {

     try {

          //do text change stuff

     } catch(e) {

          var w = Window.find ('palette', 'Multiline');

          if (w !== null){

               if (!w.visible) w.show();

               w.findElement ('runGrepErrors').text = e.message;

          }

     }

}

Rundown: use Window.find() to get a reference to your palette. Make sure the palette is visible, then use findElement() get a reference to the palette's edittext control.

Peter

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
LEGEND ,
May 12, 2012 May 12, 2012

Peter:

First you need to do you window as a palette. And you need to name your edittext control:

Rundown: use Window.find() to get a reference to your palette. Make sure the palette is visible, then use findElement() get a reference to the palette's edittext control.

You certainly can do it that way, but it's not necessary.

You can just use scoping in javascript to make sure the variable that holds the edittext control is avaialble to your function.

For instance:

var myText;

function createWindow(params) {

var w = new Window ("dialog", "Multiline"); myText = w.add ("edittext", [0, 0, 150, 70], "", {multiline: true});

  ...

}

...

function runGrep() {

  ...

  myText.text = "whatever";

  ...

}

That example uses a global variable. Of course you should avoid global variables. Without a little more information it's hard to tell you the best way to avoid it, but it's not the end of the world most likely. One choice is to wrap everything in another function, since variables in JavaScript have function scope.

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 ,
May 12, 2012 May 12, 2012

> Of course you should avoid global variables.

And by using Window.find() and findElement() you do just that, it seems to me.

Peter

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
LEGEND ,
May 12, 2012 May 12, 2012

And by using Window.find() and findElement() you do just that, it

seems to me.

Yes, but then you blow two calls to the DOM, which is bad for performance.

Better to use a local variable.

For instance

(function() {

  var myText;

  function createWindow() {

...

  }

  function runGrep() {

...

  }

}());

or alternatively, nest the functions:

function createWindow() {

  var myText;

  function runGrep() {

  ...

  }

  ...

}

or a myriad of other choices.

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 ,
May 12, 2012 May 12, 2012

But the point is that the script/function that creates the window is not (necessarily) the same script that writes into the window.

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
New Here ,
May 13, 2012 May 13, 2012
LATEST

Yesss Master Kahrel, this is exactly what I were looking for!

Cheers,

Rasmus

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