Handle region name conflict

johnmeyer wrote on 11/6/2003, 12:36 PM
I've created a script that lets you preview each event on a track, and then assign region names from pre-configured combo boxes. It is pretty much working, except for one thing. If a region has previously been assigned to an event, I get an error message, and the script stops. The error message is:

"Error: failed to add marker, one may already exist at that position."

Is it possible to test whether a region has already been assigned to an event? If not, is there a simple error handler routine I could use? I am not too familiar with jscript (which is what I am using).

Here is the relevant code. The dialog box code mentioned above is finished, but not included here. This code fragment simply plays the video for each event and then assigns a machine-generated region name. It works fine, except for the problem mentioned above.


/**
* PURPOSE OF THIS SCRIPT:
*
* This script creates a region for every event on the selected track.
* As it creates each reagion, it lets you name that region.
* Each event is optionally previewed for five seconds. //this version plays entire clip
* You can use the combo boxes to add fixed text strings to the region names.
* The region number will be appended to each region name to ensure unique names.
*
* Dialog box code to actually add region names is in another script,
* and will be added later.
*
* Written By: John Meyer
* November 5, 2003
*
**/

import System;
import System.IO;
import System.Collections;
import System.Text;
import System.Windows.Forms;
import SonicFoundry.Vegas;
import System.Threading;

var evnt : TrackEvent;
var myRegion : Region;
var i : Double;
var dLength : Double;

try {

//Clear all regions (Use this code if I can't solve region conflict problem)
//Vegas.Project.Regions.Clear();

//Find the selected event
var track = FindSelectedTrack();
if (null == track)
throw "no selected track";

var eventEnum = new Enumerator(track.Events);
while (!eventEnum.atEnd()) {
evnt = TrackEvent(eventEnum.item());
dLength = evnt.Length.ToMilliseconds();

// Preview the video (change sleep value to change playback rate)
// Change For loop and dLength multiplier to play only portions of event.
for (i = 0; i <= 10; i++) {
Vegas.Cursor = evnt.Start + new Timecode(0.1 * i * dLength);
Thread.Sleep(35);
Vegas.UpdateUI();
}

//Insert a region over this event. Name the region "hello 1", "hello 2", etc.)

// *** Here is where error occurs. If region has already been assigned to this event,
// I get an error message and the routine stops.

myRegion = new Region(evnt.Start,evnt.Length,"hello " + evnt.Index.ToString());
Vegas.Project.Regions.Add(myRegion);
eventEnum.moveNext();

}


} catch (e) {
MessageBox.Show(e);
}


function FindSelectedTrack() : Track {
var trackEnum = new Enumerator(Vegas.Project.Tracks);
while (!trackEnum.atEnd()) {
var track : Track = Track(trackEnum.item());
if (track.Selected) {
return track;
}
trackEnum.moveNext();
}
return null;
}

Comments

jetdv wrote on 11/6/2003, 2:11 PM
You could scan through the regions and see if any existing regions start at the location you are wishing to start this one.
johnmeyer wrote on 11/6/2003, 5:57 PM
Edward,

Thanks. I created a function to walk through the regions, just as you suggested. I'll upload the "finished" script in a few minutes.