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:
!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?