Book a Demo

Author Topic: Scripting error - window.prompt() not recognised?  (Read 5337 times)

Wiz

  • EA Novice
  • *
  • Posts: 8
  • Karma: +0/-0
    • View Profile
Scripting error - window.prompt() not recognised?
« on: August 29, 2011, 03:00:28 pm »
Hi hi,

I am trying to prompt users for a text string to write to multiple elements, but I am having trouble with getting an input prompt. I use the following code:

var newalias = window.prompt("Enter value", "Type you name here");
window.alert("You have entered " + newalias);

And it tells me window is undefined... am I using the wrong function?

Cheers
Wiz

pha

  • EA User
  • **
  • Posts: 39
  • Karma: +0/-0
    • View Profile
Re: Scripting error - window.prompt() not recognis
« Reply #1 on: August 29, 2011, 08:34:10 pm »
Use JScript sample JScript-Dialog.
For example:

Code: [Select]
function InputBox( promptText, title, defaultText )
{
      Repository.EnsureOutputVisible("Script");
      var vbe = new ActiveXObject("ScriptControl");
      vbe.Language = "VBScript";

      promptText = "promptText";
      title = "title";
      defaultText = "defaultText";

      output = vbe.eval( "InputBox(\"" + promptText + "\",\"" + title + "\",\"" + defaultText + "\")" );
        
      Session.Output("Text: "+output);
}
InputBox();
« Last Edit: August 29, 2011, 08:41:44 pm by 666999 »

Wiz

  • EA Novice
  • *
  • Posts: 8
  • Karma: +0/-0
    • View Profile
Re: Scripting error - window.prompt() not recognis
« Reply #2 on: August 30, 2011, 07:18:38 am »
Thanks, very helpful!

Wiz

  • EA Novice
  • *
  • Posts: 8
  • Karma: +0/-0
    • View Profile
Re: Scripting error - window.prompt() not recognis
« Reply #3 on: August 30, 2011, 10:04:46 am »
Pha, I am almost there with the code to update the field, I am just missing the piece of code to update the field on each element as commented below right after the loop begins, any ideas? Also, I am testing the script on a local sandpit repository, not sure if SQL will work here, however the main repository is an SQL database.

Code: [Select]
!INC Local Scripts.EAConstants-JScript

 function SetAlias()
{
      // Show the script output window
      Repository.EnsureOutputVisible( "Script" );

      // Get the currently selected package in the tree to work on
      var thePackage as EA.Package;
      thePackage = Repository.GetTreeSelectedPackage();
      
      if ( thePackage != null && thePackage.ParentID != 0 )
      {
            var testElementID = 0;
            
            Session.Output( "Modify Requirements Alias Function" );
            Session.Output( "=======================================" );
            Session.Output( "Working on package '" + thePackage.Name + "' (ID=" +
                  thePackage.PackageID + ")" );
            
            var elements as EA.Collection;
            elements = thePackage.Elements;
            
            function InputBox( promptText, title, defaultText )
{
      Repository.EnsureOutputVisible("Script");
      var vbe = new ActiveXObject("ScriptControl");
      vbe.Language = "VBScript";

      promptText = "promptText";
      title = "title";
      defaultText = "defaultText";

      output = vbe.eval( "InputBox(\"" + promptText + "\",\"" + title + "\",\"" + defaultText + "\")" );
        
      Session.Output("Replacing Alias Text: "+output);
}
InputBox();
            //Go through all elements in package            
            for ( var i = 0 ; i < elements.Count ; i++ )
            {
                  var currentElement as EA.Element;
                  currentElement = elements.GetAt( i );
                  
                  Session.Output( "Element: " + currentElement.Name );
                  //What goes in here to update the field for the current element?                  }
            }
            
            Session.Output( "Done!" );
      }
      else
      {
            Session.Prompt( "This script requires a package to be selected in the Project Browser.\n" +
                  "Please select a package in the Project Browser and try again.", promptOK );
      }
}

SetAlias();

Many thanks
Wiz
« Last Edit: August 30, 2011, 12:56:15 pm by Wiz »

pha

  • EA User
  • **
  • Posts: 39
  • Karma: +0/-0
    • View Profile
Re: Scripting error - window.prompt() not recognis
« Reply #4 on: August 30, 2011, 01:12:24 pm »
try this:
Code: [Select]
//Go through all elements in package            
            for ( var i = 0 ; i < elements.Count ; i++ )
            {
                  var currentElement as EA.Element;
                  currentElement = elements.GetAt( i );
                  currentElement.Alias = output;
                  currentElement.Update();
                  thePackage.Elements.Refresh();
                  
                  Session.Output( "Element: " + currentElement.Name );
             }
SQLQuery and Execute will work on a local repository :-)
« Last Edit: August 30, 2011, 01:45:25 pm by 666999 »

Wiz

  • EA Novice
  • *
  • Posts: 8
  • Karma: +0/-0
    • View Profile
Re: Scripting error - window.prompt() not recognis
« Reply #5 on: August 30, 2011, 03:06:24 pm »
Brilliant, pretty much got it working now. I get some errors after the script is finished, but seems to work anyway. I have expanded it to first ask for the Attribute you wish to update, and then the value to update it with, but don't have any error catching, and can't seem to get some of the other fields recognised. For example, currentElement.Keywords throws an exception, how would I write to that field? Below is my entire code, so you can see how I've done it. I thought it was all working well, but then when I enter a rubbish value for the attribute to modify it just goes ahead and writes to the alias field anyway... it is something to do with my if() statement, since I output the variable before hand and the value is not "Alias", therefore should not pass the if() test. If I enter a valid field (either "Alias" or "Author") there are no problems...

Code:
Code: [Select]
!INC Local Scripts.EAConstants-JScript

//
// Script to bulk-update elements in a Requirements Package.
//
// NOTE: Requires a package to be selected in the Project Browser
//
// Related APIs
// =================================================================================
// Element API - http://www.sparxsystems.com/uml_tool_guide/index.html?element2.htm
//

//Get name of Attribute to be updated
function InputBoxAttribute( promptText, title, defaultText )
{
      Repository.EnsureOutputVisible("Script");
      var vbe = new ActiveXObject("ScriptControl");
      vbe.Language = "VBScript";
      outputattribute = vbe.eval( "InputBox(\"" + promptText + "\",\"" + title + "\",\"" + defaultText + "\")" );
      Session.Output("Attribute to be modified: "+outputattribute);
}

//Get value to write to attributes
function InputBoxValue( promptText, title, defaultText )
{
      Repository.EnsureOutputVisible("Script");
      var vbe = new ActiveXObject("ScriptControl");
      vbe.Language = "VBScript";
      outputvalue = vbe.eval( "InputBox(\"" + promptText + "\",\"" + title + "\",\"" + defaultText + "\")" );
      Session.Output("Value to be written: "+outputvalue);
}

InputBoxAttribute("Enter field to update","Bulk Element Update","Enter Attribute Name");
InputBoxValue("Enter value to write","Bulk Element Update","Enter Value");

function SetFields()
{
      // Show the script output window
      Repository.EnsureOutputVisible( "Script" );

      // Get the currently selected package in the tree to work on
      var thePackage as EA.Package;
      thePackage = Repository.GetTreeSelectedPackage();
      
      if ( thePackage != null && thePackage.ParentID != 0 )
      {
            Session.Output( "Update " + outputattribute + " Field" );
            Session.Output( "=======================================" );
            Session.Output( "Working on package '" + thePackage.Name + "' (ID=" +
                  thePackage.PackageID + ")" );
            
            var elements as EA.Collection;
            elements = thePackage.Elements;      
            
            /*
            * Go through all elements, check what field is to be updated
            * currently only Alias and Author possible) and update the field
            * based on previously entered value
            */
            for ( var i = 0 ; i < elements.Count ; i++ )
            {
                  var currentElement as EA.Element;
                  currentElement = elements.GetAt( i );
                  if(outputattribute = "Alias"){
                        currentElement.Alias = outputvalue;
                        currentElement.Update();
                        thePackage.Elements.Refresh();
                        Session.Output( "Updating " + outputattribute + " for Element: " + currentElement.Name );
                  }
                  //if(outputattribute = "Keywords"){currentElement.Keywords = outputvalue;}
                  else if(outputattribute = "Author"){
                        currentElement.Author = outputvalue;
                        currentElement.Update();
                        thePackage.Elements.Refresh();
                        Session.Output( "Updating Author for Element: " + currentElement.Name );
                  }
                  else {
                        Session.Prompt("You have entered an invalid Attribute to update, please try again.", promptOK);
                  }
            }
            
            
            Session.Output( "Done!" );
      }
      else
      {
            Session.Prompt( "This script requires a package to be selected in the Project Browser.\n" +
                  "Please select a package in the Project Browser and try again.", promptOK );
      }
}

SetFields();


and the output (I enter "rubbish" into the attribute field and "Wiz" into the value):

Attribute to be modified: rubbish      
Value to be written: Wiz
      
Update rubbish Field      
=======================================      
Working on package 'Functional Requirements' (ID=298)      
Updating Alias for Element: test      
Updating Alias for Element: test2      
Updating Alias for Element: test3      
Done!      


Somehow when it does the first if() test it must set the <outputattribute> to "Alias" or something... any thoughts?

Wiz

  • EA Novice
  • *
  • Posts: 8
  • Karma: +0/-0
    • View Profile
Re: Scripting error - window.prompt() not recognis
« Reply #6 on: August 30, 2011, 03:20:09 pm »
What a rookie, I just realised using "=" in a test sets the value, just changed them to "==" for comparing, everythings ok now.

Still unsure how to access other fields such as Keywords though...

Cheers
Wiz

pha

  • EA User
  • **
  • Posts: 39
  • Karma: +0/-0
    • View Profile
Re: Scripting error - window.prompt() not recognis
« Reply #7 on: August 30, 2011, 03:28:32 pm »
Happens :-)

Keywords:

Code: [Select]
...
currentElement.Tag = output;
currentElement.Update();
thePackage.Elements.Refresh();
...