Skip navigation
Rasmus.Olsen
Currently Being Moderated

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

May 10, 2012 2:27 AM

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

 
Replies
  • Currently Being Moderated
    May 11, 2012 12:58 AM   in reply to Rasmus.Olsen

    Rasmus,

     

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

     

    Peter

     
    |
    Mark as:
  • Currently Being Moderated
    May 11, 2012 8:15 AM   in reply to Rasmus.Olsen

    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

     
    |
    Mark as:
  • Currently Being Moderated
    May 12, 2012 2:53 AM   in reply to Rasmus.Olsen

    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

     
    |
    Mark as:
  • John Hawkinson
    5,527 posts
    Jun 25, 2009
    Currently Being Moderated
    May 12, 2012 5:32 AM   in reply to Peter Kahrel

    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.

     
    |
    Mark as:
  • Currently Being Moderated
    May 12, 2012 6:17 AM   in reply to John Hawkinson

    > Of course you should avoid global variables.

     

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

     

    Peter

     
    |
    Mark as:
  • John Hawkinson
    5,527 posts
    Jun 25, 2009
    Currently Being Moderated
    May 12, 2012 6:28 AM   in reply to Peter Kahrel

    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.

     
    |
    Mark as:
  • Currently Being Moderated
    May 12, 2012 6:28 AM   in reply to John Hawkinson

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

     
    |
    Mark as:

More Like This

  • Retrieving data ...

Bookmarked By (0)

Answers + Points = Status

  • 10 points awarded for Correct Answers
  • 5 points awarded for Helpful Answers
  • 10,000+ points
  • 1,001-10,000 points
  • 501-1,000 points
  • 5-500 points