Can i duplicating event in script?

i want duplicate event in script, like context menu “duplicate” , then only replace audio asset in event

Hi,

You can duplicate an event via script by selecting your event and calling studio.window.triggerAction(studio.window.actions.Duplicate);. To select an event from a script, you can get an array of all events using studio.project.model.Event.findInstances(), get your event from that list, and then use studio.window.navigateTo(yourEventhere) to select the event in the Event Browser.

However, I definitely feel like there should be a way to do this via the Scripting API that doesn’t explicitly require you to select an event in the Event Browser, so I’ve flagged this in our internal feature/improvement tracker.

As for replacing audio assets, you’ll need two things: the instrument’s underlying ManagedObject:SingleSound object, and the ManagedObject:AudioFile of the audio asset you want the instrument to use. From there, you can access either SingleSound.relationships.audioFile or AudioFile.relationships.sounds, and use ManagedRelationship.add() on either while passing the opposite ManagedObject as an argument. This will replace the existing relationship between the instrument and the old audio asset with a new relationship between the instrument and the new audio asset.

A simple example of changing the audio asset of a Single Instrument on a timeline by script is the following:

// get the first instrument (single instrument) on the selected event's timeline
var singleSound = studio.window.browserCurrent().timeline.modules[0];
// get the audio asset you want the instrument to use
var newAudioAsset = studio.project.workspace.masterAssetFolder.getAsset("newAudio.wav");

// do one of the below:
// either add a relationship via the instrument
singleSound.relationships.audioFile.add(newAudioAsset);
// or add a relationship via the audio asset
newAudioAsset.relationships.sounds.add(singleSound);

If your event has instruments on parameter sheets or an action sheet, then you can access those sheets via the parameters property on your event. It’s also worth noting that with instruments that contain a playlist of multiple instruments (i.e. a Multi Instrument), you should be able to access their underlying single instruments via the sounds property. In general, you can see the properties and functions of an entity using the dump() function.

Hope that helps!

1 Like