Script to select event to the left or right?

Sebaz wrote on 10/23/2010, 1:27 PM
Is there a script that will allow me to select the event immediately to the left of the event that is under the cursor? And then another script like it, except that it would select the event to the right?

I'm trying to duplicate a functionality in Edius that I find very productive for editing cuts, which is Alt+ left arrow = selects event to the left and same but with right arrow and event to the right. That saves me a lot of time in Edius and I would like to bring it to Vegas. Can such as script be done, or has anybody done it so far? I remember a script that selected the event right under the cursor, so I imagine what I propose here isn't so far away.

Comments

JohnnyRoy wrote on 10/23/2010, 6:06 PM
Here are two scripts that will do what you want:

This one selects the event to the left of the current event as long as the event is under the cursor:


//*******************************************************************
//* Program: SelectLeftEvent.cs
//* Author: John Rofrano
//* Updated: October 23, 2010
//* Description: Selects the event that is to the left of the current
//* selected event under the cursor.
//*------------------------------------------------------------------
//* Copyright: (c) 2010, John Rofrano, All Rights Reserved
//*******************************************************************
using System;
using System.Diagnostics;
using System.Windows.Forms;
using Sony.Vegas;

public class EntryPoint
{
public void FromVegas(Vegas vegas)
{
try
{
TrackEvent trackEvent = FindFirstSelectedEventUnderCursor(vegas);
if (trackEvent != null)
{
if (trackEvent.Index > 0)
{
((TrackEvent)trackEvent.Track.Events[trackEvent.Index - 1]).Selected = true;
}
else
{
MessageBox.Show("There is no event to the left of the selected event", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
else
{
MessageBox.Show("You must select a video event before running this script", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
catch (Exception e)
{
MessageBox.Show(e.Message, "Critical Error", MessageBoxButtons.OK, MessageBoxIcon.Stop);
}
}

/// <summary>
/// Returns the first selected event that's under the cursor
/// </summary>
/// <param name="vegas">The vegas instance</param>
/// <returns>The first selected event or null if no event selected</returns>
public TrackEvent FindFirstSelectedEventUnderCursor(Vegas vegas)
{
foreach (Track track in vegas.Project.Tracks)
{
foreach (TrackEvent trackEvent in track.Events)
{
if (trackEvent.Selected && trackEvent.Start <= vegas.Transport.CursorPosition && trackEvent.End >= vegas.Transport.CursorPosition)
{
return trackEvent;
}
}
}

return null;
}

}


This one selects the event to the right of the current event as long as the event is under the cursor:


//*******************************************************************
//* Program: SelectRightEvent.cs
//* Author: John Rofrano
//* Updated: October 23, 2010
//* Description: Selects the event that is to the right of the current
//* selected event under the cursor.
//*------------------------------------------------------------------
//* Copyright: (c) 2010, John Rofrano, All Rights Reserved
//*******************************************************************
using System;
using System.Diagnostics;
using System.Windows.Forms;
using Sony.Vegas;

public class EntryPoint
{
public void FromVegas(Vegas vegas)
{
try
{
TrackEvent trackEvent = FindFirstSelectedEventUnderCursor(vegas);
if (trackEvent != null)
{
int nextIndex = trackEvent.Index + 1;
if (trackEvent.Track.Events.Count > nextIndex)
{
((TrackEvent)trackEvent.Track.Events[nextIndex]).Selected = true;
}
else
{
MessageBox.Show("There is no event to the right of the selected event", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
else
{
MessageBox.Show("You must select a video event before running this script", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
catch (Exception e)
{
MessageBox.Show(e.Message, "Critical Error", MessageBoxButtons.OK, MessageBoxIcon.Stop);
}
}

/// <summary>
/// Returns the first selected event that's under the cursor
/// </summary>
/// <param name="vegas">The vegas instance</param>
/// <returns>The first selected event or null if no event selected</returns>
public TrackEvent FindFirstSelectedEventUnderCursor(Vegas vegas)
{
foreach (Track track in vegas.Project.Tracks)
{
foreach (TrackEvent trackEvent in track.Events)
{
if (trackEvent.Selected && trackEvent.Start <= vegas.Transport.CursorPosition && trackEvent.End >= vegas.Transport.CursorPosition)
{
return trackEvent;
}
}
}

return null;
}

}


Just assign these to a key stroke with the keyboard mapper (Options | Customize Keyboard...) and you should be all set.

Enjoy,

~jr
Sebaz wrote on 10/23/2010, 7:51 PM
Thanks, Jon, but it doesn't work as I hoped. If the event under the cursor is not selected, when I try to run either script it gives me an error saying "You must select a video event before running this script". If the event under the cursor is selected, activating the left script will select the event to the left, but it won't deselect the one under the cursor. Same for the right event. Also, it doesn't jump from event to event selecting it like in Edius, where you press Alt+Right and it selects one script at a time and each time you press the right key with Alt depressed it jumps to the next event. Is there a way to modify it to do these things? Many times when you're editing the event you're working on is not selected, so moving the right hand to the mouse to select it before running the script would defeat the purpose of the script.

Just in case, I'm running Vegas 10, I don't know if that makes a difference.

Thanks
Rosebud wrote on 10/23/2010, 10:34 PM
Not exactly the same, but the '7' and '9' key from the numeric keypad allow you to edit the next/previous event.
JohnnyRoy wrote on 10/24/2010, 8:19 AM
> but it doesn't work as I hoped. If the event under the cursor is not selected, when I try to run either script it gives me an error saying "You must select a video event before running this script". If the event under the cursor is selected, activating the left script will select the event to the left, but it won't deselect the one under the cursor. Same for the right event. Also, it doesn't jump from event to event selecting it like in Edius, where you press Alt+Right and it selects one script at a time and each time you press the right key with Alt depressed it jumps to the next event.

Well... that's not what you described initially. You seemed to have left all of these behavioral details out of your original post.

> Is there a way to modify it to do these things?

Probably but it isn't worth it. I thought you want both events selected to group them or something. If you just want to move to the next event, Vegas already does this. As Rosebud said, just use the '7' and '9' key from the numeric keypad.

~jr
johnmeyer wrote on 10/24/2010, 3:33 PM
OK, try this script. I posted this years ago, and use it every day, so I'm pretty sure it works exactly as you want. This script selects the event under the cursor. You must first select a track, and that track must contain video. If the track immediately below contains audio, that event is selected as well. ALL OTHER EVENTS are de-selected. You do not need to first select an event; you only need to make sure the track header has been selected (so the script knows which event you want to select.

Keep reading further on down because I then did a quick hack to this script so that it selects the event immediately before the event under the cursor. I then made another modification to select the event immediately AFTER the event under the cursor. The initial script has been pretty thoroughly tested and I've used over 10,000 times (or more). It works. By contrast, the two modifications, although trivial, have not been tested. I am quite sure that if you select the first event and then try to select the (non-existent) event prior to that, the script will give an error message. Same problem selecting the last event and then asking to select the event after that one. I could add error-checking, but you get what you pay for.

/** 
* PURPOSE OF THIS SCRIPT:
*
* Select event at cursor, and de-select all other events.
*
* A video track must be selected. If an audio track is selected, nothing happens.
* If the track beneath the video track contains audio, the audio event in that track
* that lies beneath the cursor will also be selected.
*
* Copyright © John Meyer 2004
* Written: September 23, 2004
*
**/

import System;
import System.IO;
import System.Windows.Forms;
import Sony.Vegas;


try {

//Global declarations
var dStart : Double;
var dLength : Double;
var dCursor : Double;
var trackEnum : Enumerator;
var evnt : TrackEvent;
var CurrentEvnt : TrackEvent;

var track = FindSelectedTrack(); // Use this function to find the first selected track.
var eventEnum = new Enumerator(track.Events);

if (track.IsVideo()) { // Proceed only if selected track is video track.

if ( SelectEventAtCursor() ) { // Function that selects events under cursor.

// Get set to look at track directly below the video track.
trackEnum.moveNext(); // Go to next track.

if (!trackEnum.atEnd()) { // Only proceed if there is a track below the video track.
track = Track(trackEnum.item()); // When doing the first track (above), these two lines were executed
eventEnum = new Enumerator(track.Events); // in the FindSelectedTrack() function.
if (track.IsAudio()) { // Only trim the event if this is an audio track.
SelectEventAtCursor();
}
}
}
}
// Vegas.Cursor = CurrentEvnt.Start; // Enable this line to move cursor to start of selected event.
Vegas.UpdateUI();

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

// End of main program



// Beginning of functions

function FindSelectedTrack() : Track {
trackEnum = new Enumerator(Vegas.Project.Tracks);
while (!trackEnum.atEnd()) {
var track : Track = Track(trackEnum.item());

if (track.Selected) {
return track;
}
trackEnum.moveNext();
}
return null;
}


/**
*
* The following function finds the event on the selected track
* that lies under the cursor. It also deselects all other events.
*
**/

function SelectEventAtCursor() {

var EventFound : boolean = false; // Function returns false if no video media under cursor.

dCursor = Vegas.Cursor.ToMilliseconds(); // Remember the cursor position.

//Go through each event on the track.

while (!eventEnum.atEnd()) {
evnt = TrackEvent(eventEnum.item());
evnt.Selected = false; // De-select the event

// Get the event's start and length timecode, in milliseconds.
dStart = evnt.Start.ToMilliseconds();
dLength = evnt.Length.ToMilliseconds();


/**
* If the cursor timecode is between the beginning and end of the
* event timecodes, then select the event.
**/

if ( (dCursor >= dStart) && ( dCursor < (dLength + dStart) ) ) {
evnt.Selected = true; // Select this event.
EventFound = true;
CurrentEvnt = evnt;
}
eventEnum.moveNext(); // Go to next event on this timeline.

}
return EventFound;
}



This one selects the event prior to the one that lies under the cursor:
/** 
* PURPOSE OF THIS SCRIPT:
*
* Select event prior to the one under the cursor, and de-select all other events.
*
* A video track must be selected. If an audio track is selected, nothing happens.
* If the track beneath the video track contains audio, the audio event in that track
* that lies beneath the cursor will also be selected.
*
* Copyright © John Meyer 2004
* Written: September 23, 2004
* Revised, John Meyer October 2010
*
**/

import System;
import System.IO;
import System.Windows.Forms;
import Sony.Vegas;


try {

//Global declarations
var dStart : Double;
var dLength : Double;
var dCursor : Double;
var trackEnum : Enumerator;
var evnt : TrackEvent;
var CurrentEvnt : TrackEvent;
var PreviousEvent : TrackEvent;
var track = FindSelectedTrack(); // Use this function to find the first selected track.
var eventEnum = new Enumerator(track.Events);

if (track.IsVideo()) { // Proceed only if selected track is video track.

if ( SelectEventAtCursor() ) { // Function that selects events under cursor.

// Get set to look at track directly below the video track.
trackEnum.moveNext(); // Go to next track.

if (!trackEnum.atEnd()) { // Only proceed if there is a track below the video track.
track = Track(trackEnum.item()); // When doing the first track (above), these two lines were executed
eventEnum = new Enumerator(track.Events); // in the FindSelectedTrack() function.
if (track.IsAudio()) { // Only trim the event if this is an audio track.
SelectEventAtCursor();
}
}
}
}
Vegas.Cursor = CurrentEvnt.Start; // Enable this line to move cursor to start of selected event.
Vegas.UpdateUI();

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

// End of main program



// Beginning of functions

function FindSelectedTrack() : Track {
trackEnum = new Enumerator(Vegas.Project.Tracks);
while (!trackEnum.atEnd()) {
var track : Track = Track(trackEnum.item());

if (track.Selected) {
return track;
}
trackEnum.moveNext();
}
return null;
}


/**
*
* The following function finds the event on the selected track
* that lies under the cursor. It also deselects all other events.
*
**/

function SelectEventAtCursor() {

var EventFound : boolean = false; // Function returns false if no video media under cursor.

dCursor = Vegas.Cursor.ToMilliseconds(); // Remember the cursor position.

//Go through each event on the track.
PreviousEvent = TrackEvent(eventEnum.item());
while (!eventEnum.atEnd()) {
evnt = TrackEvent(eventEnum.item());
evnt.Selected = false; // De-select the event

// Get the event's start and length timecode, in milliseconds.
dStart = evnt.Start.ToMilliseconds();
dLength = evnt.Length.ToMilliseconds();


/**
* If the cursor timecode is between the beginning and end of the
* event timecodes, then select the event.
**/

if ( (dCursor >= dStart) && ( dCursor < (dLength + dStart) ) ) {
PreviousEvent.Selected = true; // Select this event.
EventFound = true;
CurrentEvnt = PreviousEvent;
}
PreviousEvent = evnt;
eventEnum.moveNext(); // Go to next event on this timeline.

}
return EventFound;
}


/** 
* PURPOSE OF THIS SCRIPT:
*
* Select event AFTER the one under the cursor, and de-select all other events.
*
* A video track must be selected. If an audio track is selected, nothing happens.
* If the track beneath the video track contains audio, the audio event in that track
* that lies beneath the cursor will also be selected.
*
* Copyright © John Meyer 2004
* Written: September 23, 2004
* Revised, John Meyer October 2010
*
**/

import System;
import System.IO;
import System.Windows.Forms;
import Sony.Vegas;


try {

//Global declarations
var dStart : Double;
var dLength : Double;
var dCursor : Double;
var trackEnum : Enumerator;
var evnt : TrackEvent;
var CurrentEvnt : TrackEvent;
var PreviousEvent : TrackEvent;
var track = FindSelectedTrack(); // Use this function to find the first selected track.
var eventEnum = new Enumerator(track.Events);

if (track.IsVideo()) { // Proceed only if selected track is video track.

if ( SelectEventAtCursor() ) { // Function that selects events under cursor.

// Get set to look at track directly below the video track.
trackEnum.moveNext(); // Go to next track.

if (!trackEnum.atEnd()) { // Only proceed if there is a track below the video track.
track = Track(trackEnum.item()); // When doing the first track (above), these two lines were executed
eventEnum = new Enumerator(track.Events); // in the FindSelectedTrack() function.
if (track.IsAudio()) { // Only trim the event if this is an audio track.
SelectEventAtCursor();
}
}
}
}
Vegas.Cursor = CurrentEvnt.Start; // Enable this line to move cursor to start of selected event.
Vegas.UpdateUI();

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

// End of main program



// Beginning of functions

function FindSelectedTrack() : Track {
trackEnum = new Enumerator(Vegas.Project.Tracks);
while (!trackEnum.atEnd()) {
var track : Track = Track(trackEnum.item());

if (track.Selected) {
return track;
}
trackEnum.moveNext();
}
return null;
}


/**
*
* The following function finds the event on the selected track
* that lies under the cursor. It also deselects all other events.
*
**/

function SelectEventAtCursor() {

var EventFound : boolean = false; // Function returns false if no video media under cursor.

dCursor = Vegas.Cursor.ToMilliseconds(); // Remember the cursor position.

//Go through each event on the track.
PreviousEvent = TrackEvent(eventEnum.item());
while (!eventEnum.atEnd()) {
evnt = TrackEvent(eventEnum.item());
evnt.Selected = false; // De-select the event

// Get the event's start and length timecode, in milliseconds.
dStart = evnt.Start.ToMilliseconds();
dLength = evnt.Length.ToMilliseconds();


/**
* If the cursor timecode is between the beginning and end of the
* event timecodes, then select the event.
**/

if ( (dCursor >= dStart) && ( dCursor < (dLength + dStart) ) ) {
EventFound = true;
evnt.Selected = false; // De-Select event under cursor.
eventEnum.moveNext(); // Go to next event on this timeline.
evnt = TrackEvent(eventEnum.item());
CurrentEvnt = evnt;
evnt.Selected = true; // Select this event.
}
eventEnum.moveNext(); // Go to next event on this timeline.
}
return EventFound;
}
Sebaz wrote on 10/26/2010, 12:11 PM
John, thanks for posting the scripts. They're not working, unfortunately. I get this error when I try to use the one that selects the previous event:

C:\Program Files (x86)\Sony\Vegas Pro 10.0\Script Menu\Select event to the left.cs(16) : A namespace does not directly contain members such as fields or methods
C:\Program Files (x86)\Sony\Vegas Pro 10.0\Script Menu\Select event to the left.cs(33) : Expected class, delegate, enum, interface, or struct
C:\Program Files (x86)\Sony\Vegas Pro 10.0\Script Menu\Select event to the left.cs(44) : Expected class, delegate, enum, interface, or struct
C:\Program Files (x86)\Sony\Vegas Pro 10.0\Script Menu\Select event to the left.cs(48) : Type or namespace definition, or end-of-file expected

And this one when I try to use the script to select the next event:

C:\Program Files (x86)\Sony\Vegas Pro 10.0\Script Menu\Select event to the right.cs(16) : A namespace does not directly contain members such as fields or methods
C:\Program Files (x86)\Sony\Vegas Pro 10.0\Script Menu\Select event to the right.cs(33) : Expected class, delegate, enum, interface, or struct
C:\Program Files (x86)\Sony\Vegas Pro 10.0\Script Menu\Select event to the right.cs(44) : Expected class, delegate, enum, interface, or struct
C:\Program Files (x86)\Sony\Vegas Pro 10.0\Script Menu\Select event to the right.cs(48) : Type or namespace definition, or end-of-file expected
jetdv wrote on 10/26/2010, 5:44 PM
Sebaz, those are .js scripts - not .cs.
johnmeyer wrote on 10/26/2010, 6:05 PM
These definitely work as advertised with Vegas 10.0a. Please follow Ed's advice and save them with the extension .js instead of .cs and try again.