We recently discussed a complex script that lets say imports a very special FMEA from an Excel sheet. The project that contains the script has FMEAs on various levels, say "System" and "Component" level and already contains some predefined system models, FMEA worksheets and some collections. Like this simplified one:

Now the script uses the ui.js which as you know offers very basic dialogs as the selectElement function. The script is asking the user 3 times to select model, then FMEA and last the collection. That's highly flexible but cumbersome for the user and even risky as user may select the wrong things. Anyway, here an example how it may look in the code:
// $EXPERIMENTAL$ $STRICT_MODE$ $WARNING_AS_ERROR$
load(".lib/ui.js");
var model = undefined;
var fmea = undefined;
var measures = undefined;
// this selection is mandatory
function selectMandatoryModel() {
// select a few things
model = selectElement("System Model", Metamodel.sysml.SysMLContainerPackage, false, finder.project);
return model != undefined;
}
// do some optional selections
function selectOptionalModels() {
fmea = selectElement("FMEA", Metamodel.FMEA.FMEAWorksheet, false, finder.project);
if (!fmea) {
return false;
}
measures = selectElement("Measures", Metamodel.safetyModel.MeasureCatalog, false, finder.project);
if (!measures) {
return false;
}
return true;
}
function doSomething() {
if (!model || !fmea || !measures) {
alert("Something is fishy!");
}
}
function main() {
selectMandatoryModel() && selectOptionalModels() && doSomething();
}
main();
As you can see I already prepared the code to distinguish between mandatory and optional models. The selection of the system model is mandatory, all others can be maybe derived from the first selection. One way would be to trace the system model with all other models and use these traces to find the related one.
Here in this example I propose another simple mechanism: since the project is prepared, the other models can be found by the prefix, for example "Component". See now my improved script which uses the finder to find the other models:
// $EXPERIMENTAL$ $STRICT_MODE$ $WARNING_AS_ERROR$
load(".lib/ui.js");
var model = undefined;
var fmea = undefined;
var measures = undefined;
// this selection is mandatory
function selectMandatoryModel() {
// select a few things
model = selectElement("System Model", Metamodel.sysml.SysMLContainerPackage, false, finder.project);
return model != undefined;
}
// do some optional selections
function selectOptionalModels() {
fmea = selectElement("FMEA", Metamodel.FMEA.FMEAWorksheet, false, finder.project);
if (!fmea) {
return false;
}
measures = selectElement("Measures", Metamodel.safetyModel.MeasureCatalog, false, finder.project);
if (!measures) {
return false;
}
return true;
}
function findOptionalModels() {
var prefix = model.name.split(' ')[0];
fmea = finder.findByType(Metamodel.FMEA.FMEAWorksheet).and("name", prefix + " FMEA").first();
measures = finder.findByType(Metamodel.safetyModel.MeasureCatalog).and("name", prefix + " Measures").first();
return true;
}
function doSomething() {
if (!model || !fmea || !measures) {
alert("Something is fishy!");
}
}
function main() {
// selectMandatoryModel() && selectOptionalModels() && doSomething();
selectMandatoryModel() && findOptionalModels() && doSomething();
}
main();
That is not a full fledged solution but maybe gives you a starting point when writing own solutions that require less clicks.
Happy Scripting
Jan