Select last event of first track

stefan-m2020 wrote on 1/2/2022, 5:21 PM

Hello experts and a happy new year to you all!

I would like to realize the following with the script:
- Go to first track
- Select only last event

It throws this error message:
"; expected."
"Expression has no effect."
"Variable i not declared."

import SonicFoundry.Vegas;
import System.Windows.Forms;

try
{    
    var tracks = Vegas.Project.Tracks;
    int i == 0;

        for (var evnt in tracks.Events) // Step through all 10 events
        {
          i++ // Increment by 1
          if (i == 10) // If it's the 10th event (should be the last):
            
            {
                evnt.Selected = true; // Select this event
            }
        }
}

catch (errorMsg)
{

    MessageBox.Show(errorMsg, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);

}

I know using the incrementer logic is not good, but I have no idea
if there is some "Select.tracks.events.last" method.
And in my case 10 would be enough, but I could always raise it to an unrealistic value.

Any help will be appreciated!

Best regards,

Stefan

Comments

jetdv wrote on 1/3/2022, 8:50 AM

@stefan-m2020, does it have to be in jScript? Here's c# code that will select the last event on the first track:
 

using ScriptPortal.Vegas;

namespace Test_Script
{
    public class Class1
    {
        public Vegas myVegas;

        public void Main(Vegas vegas)
        {
            myVegas = vegas;

            Track myTrack = myVegas.Project.Tracks[0]; //zero is the first track

            TrackEvent myEvent = myTrack.Events[myTrack.Events.Count - 1]; //Select the last event which will be event "count - 1"

            myEvent.Selected = true; //Select that event
        }

    }
}

public class EntryPoint
{
    public void FromVegas(Vegas vegas)
    {
        Test_Script.Class1 test = new Test_Script.Class1();
        test.Main(vegas);
    }
}

 

The first issue with your code is that you're not selecting a track. You have to get to a single "track" in order to go through the events on that track. Right now your variable has "all tracks".

 

Modifying your code...

import ScriptPortal.Vegas; //SonicFoundry.Vegas; was only for version 4 
import System.Windows.Forms; 
try 
{
   var tracks = Vegas.Project.Tracks;
   var firstTrack = tracks[0]; //You have to get an individual track before you can go through the events on that track

   //Just use the "count" of events to get the last one
   var evnt = firstTrack.Events[firstTrack.Events.Count - 1];

   evnt.Selected = true; // Select this event
} 
catch (errorMsg) 
{ 
   MessageBox.Show(errorMsg, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); 
}

 

stefan-m2020 wrote on 1/3/2022, 12:02 PM

Hello jetdv,

Thank you very much!
It works perfectly.

It does not have neccessarily to be js,
because my programming knowledge is equally "good" for all languages.

Great that you even put comments!

 

Edit

I realized that I have a small issue:

It selects first track, but on the Vegas GUI it still has another track and another event selected additionally.

My main aim is that I read in a text file, select the last (or pre-last) event in Vegas, move my cursor to a specific point and paste the event.

Most of the stuff I just do with AutoHotkey.
The relevent part was the selection of the last event.

Hopefully your script can be updated this way, that:
- anything else is unselected
- the first track is "really selected"

Thanks again and best regards!

jetdv wrote on 1/3/2022, 12:20 PM

@stefan-m2020, I'm still not quite sure what you're asking for here. Yes, the script can easily go through multiple tracks. It could even find the "last" event in the entire project - but I don't think that's what you're really wanting to do. If you're wanting to add an event at a specific point, how is the script supposed to determine that specific point? Or is it just supposed to be at the "END" of the project?

A script can easily move the cursor for you. It can even add the event to the timeline for you. It just needs to know what to add and where to add it. If you could provide more details, that would help.

stefan-m2020 wrote on 1/3/2022, 1:35 PM

Hello,

I could update the Vegas script so that everything works now.

import ScriptPortal.Vegas; //SonicFoundry.Vegas; was only for version 4
import System.Windows.Forms;
try
{
  var track = Vegas.Project.Tracks;
  var firstTrack = track[0]; //You have to get an individual track before you can go through the events on that track

  for (var track in Vegas.Project.Tracks)
  {
    for (var evnt in track.Events) // Step through all events
    {
      evnt.Selected = false; // Deselect all events to prevent wrong or multiple selections
    }
  }

  var evnt = firstTrack.Events[firstTrack.Events.Count - 1]; //Just use the "count" of events to get the last one
  evnt.Selected = true; // Select this event
}
catch (errorMsg)
{
   MessageBox.Show(errorMsg, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}

I copied the script, changed the [firstTrack.Events.Count - 1] to -2
and renamed it to SelectPreLastEvent.js

Both Vegas Scripts can be activated by Ctrl+F11/Ctrl+F12 and the rest gets done with AHK.

Example of an AHK script (sorry for the German comments):
F5::
Vegas_Letztes_Event_Auswählen(0) ; Wählt das vorletzte oder letzte Event der ersten Spur aus
return

F6::
Vegas_Letztes_Event_Auswählen(1) ; Wählt das vorletzte oder letzte Event der ersten Spur aus
return

Vegas_Letztes_Event_Auswählen(letztes) ; Wählt das vorletzte oder letzte Event der ersten Spur aus - #FUNKTION#
{
  Send !0 ; Fokus auf Timeline
  Sleep, 10
  Send {Tab} ; Fokus auf Trackliste
  Sleep, 10
  Send {Home} ; Erste Spur
  Sleep, 100
  if letztes ; Letztes Event auswählen
  {
    Send ^{F12} ; Vegas Skript SelectLastEvent.js ausführen
  }
  else ; Vorletztes Event auswählen
  {
    Send ^{F11} ; Vegas Skript SelectPreLastEvent.js ausführen
  }

Regarding the timeline positions:
That can be easily done with AHK.

It just reads in a text file line per line and pastes the time
into the timeline field of Vegas (Ctrl+G).
Then it pastes either the pre last or last file, depending on the 2nd value of each line.

I tested it and everything works perfectly.

Thank you again for your help!

Best regards

jetdv wrote on 1/3/2022, 1:58 PM

I guess my next question would be, why do you need AHK? Maybe I'm not understanding everything you are doing but it seems, from what I've seen so far, that the whole thing could be done in the script. JScript and C# can both read the text file just fine. The cursor can be placed easily from within the script. But I'm not sure what else your AHK script is doing.