JS Scripting API: checking if event has a multi-instrument

Hello,

I’m writing a script with JS and writing a function that returns true if an event has a multi instrument on any parameter sheet. This is my current code:

function HasVariations( event )
{
	var eventSheets = event.parameters;
	eventSheets.push( event.timeline );
	console.log("EVENT SHEETS: " + eventSheets );
		
	for( var i = 0; i < eventSheets.length; i++ )
	{
		var instruments = eventSheets[ i ].modules;
		console.log("INSTRUMENTS IN SHEET: " + instruments );
		
		for( var j = 0; j < instruments.length; j++ )
		{
			if (instruments[ j ].entity === "MultiSound" )
			{
				return true;
			}
		}
	}
	
	return false;
}

If my eventSheets variable only includes a timeline, this works fine. But if I’m iterating over an action sheet or other parameter sheet then I get the following error:

TypeError: Result of expression 'instruments[ j ]' [undefined] is not an object., callstack:HasVariations(event = (ManagedObject:Event))

How could I make this code work when iterating over both timelines and param sheets?

Thanks in advance!

EDIT: actually the error only seems to happen when iterating over an Action Sheet - timelines and other parameter sheets work fine

Same error happens when I try to dump() information from a module/instrument that lives in an action sheet vs a timeline/param sheet

Ok found a solution in this forum thread: How can I get sheet content by javascript? - #4 by MisakaCirno

TL;DR: Action sheets are actually multi instruments so instead of eventSheets[ i ].modules it had to be eventSheets[ i ].modules.sounds for action sheets.

Function ended up looking like this:

function HasVariations( event )
{
	var eventSheets = event.parameters;
	eventSheets.push( event.timeline );
	
	for( var i = 0; i < eventSheets.length; i++ )
	{
		var instruments = [];
		
		if( eventSheets[ i ].isOfExactType('ActionSheet'))
		{
			instruments = eventSheets[ i ].modules.sounds;
		}
		else
		{
			instruments = eventSheets[ i ].modules;
		}
		
		for( var j = 0; j < instruments.length; j++ )
		{
			if (instruments[ j ].entity == "MultiSound" )
			{
				return true;
			}
		}
	}	
	return false;
}
1 Like

Thank you for sharing the solution here!