Help in translate javascript to c sharp

playmarius wrote on 11/30/2021, 7:52 AM

Hi, i found those lines of codes in Export EDL,js, but i can't figure out how to convert them to c sharp. The ideea is that when you try to run the script, but a project has no tracks (video or audio), a message box shoud appear saying that are no tracks to export.

    var videoTrack = FindTopTrackOfType(MediaType.Video);
    var audioTrack = FindTopTrackOfType(MediaType.Audio);
    if ((null == videoTrack) && (null == audioTrack))
    {
        throw "no tracks to export";
    }

Comments

jetdv wrote on 11/30/2021, 6:25 PM

"FindTopTrackOfType" will be a function in the code you are writing. So that routine would need to be correct as well. But those lines could be:

 

    Track videoTrack = FindTopTrackOfType(MediaType.Video);
    Track audioTrack = FindTopTrackOfType(MediaType.Audio);
    if ((null == videoTrack) && (null == audioTrack)) 
    {
         throw "no tracks to export"; 
    }

You have to change all "var" statements to be the type of variable it will be receiving. So the "FindTopTrackOfType" must return a type of "Track".

playmarius wrote on 12/1/2021, 1:51 AM

Sorry i'm new in programming. I work on the same script that you helped before, i thought it will be simpler. This is my code so far:

using System;
using System.Text;
using System.Collections.Generic;
using System.Windows.Forms;
using ScriptPortal.Vegas;

public class EntryPoint
{
    public void FromVegas(Vegas vegas)
    {
        string tk_error = "Text2Reg error:";
        string tk_warning = "Text2Reg Warning:";

        //find tracks
        // Track videoTrack = FindTopTrackOfType(MediaType.Video);
        // if (null == videoTrack)) 
        // {
             // MessageBox.Show(String.Format("No video track to export"),tk_error);
        // }
        
        foreach (Track track in vegas.Project.Tracks)
        {

            try
            {
                // only the selected tracks            
                if (!track.Selected) continue;
                
                foreach (TrackEvent trackEvent in track.Events)
                {
                    Media media = trackEvent.ActiveTake.Media;
                    OFXEffect ofxEffect= media.Generator.OFXEffect;
                    OFXStringParameter txtparam = (OFXStringParameter) ofxEffect.FindParameterByName("Text");
                    RichTextBox rtfText = new RichTextBox();
                    rtfText.Rtf = txtparam.Value;

                    string MiniActiveTakeName = rtfText.Text;

                    if (MiniActiveTakeName == "")
                    {
                        if (MessageBox.Show("Some media contains no text.\r\nDo you want to continue?", 
                            tk_warning, MessageBoxButtons.YesNo) == DialogResult.No)
                        {
                            vegas.Project.Regions.Clear();
                            return;
                        }
                    }
                    Region region = new Region(trackEvent.Start, trackEvent.Length, MiniActiveTakeName);
                    try
                    {
                        vegas.Project.Regions.Add(region);    
                    }
                    catch (Exception e)
                    {
                        MessageBox.Show(String.Format("Couldn't place Region at {0}\nThe error message: {1}", trackEvent.Start.ToString(), e.Message));
                    }
                }                
            }
            catch (Exception e)
            {
                MessageBox.Show(String.Format("Selected track has no media text\r\nor some media text are in (Legacy) Text.", e.Message),tk_error);
                vegas.Project.Regions.Clear();
                return;
            }            
        }
    }
    
    // public void FindTopTrackOfType(mediaType) :Track
    // {
        // var trackEnum = new Enumerator(Vegas.Project.Tracks);
        // while (!trackEnum.atEnd()) {
            // var track : Track = Track(trackEnum.item());
            // if (track.MediaType == mediaType) {
                // return track;
            // }
            // trackEnum.moveNext();
        // }
        // return null;
    // }
}

The code works i just want to add an option, if a project has no tracks to give me a error message. The only script that i saw doing that is Export EDL.js but is js no cs.

jetdv wrote on 12/1/2021, 1:14 PM

You don't need to check up front. Just count the tracks as you process them. Then if there were no tracks to process, display that message:
 

using System;
using System.Text;
using System.Collections.Generic;
using System.Windows.Forms;
using ScriptPortal.Vegas;

public class EntryPoint
{
    public void FromVegas(Vegas vegas)
    {
        string tk_error = "Text2Reg error:";
        string tk_warning = "Text2Reg Warning:";

        int CountTracks = 0;
        
        foreach (Track track in vegas.Project.Tracks)
        {

            try
            {
                // only the selected tracks            
                if (track.Selected)
                {
                    CountTracks++;
                
                    foreach (TrackEvent trackEvent in track.Events)
                    {
                        Media media = trackEvent.ActiveTake.Media;
                        OFXEffect ofxEffect= media.Generator.OFXEffect;
                        OFXStringParameter txtparam = (OFXStringParameter) ofxEffect.FindParameterByName("Text");
                        RichTextBox rtfText = new RichTextBox();
                        rtfText.Rtf = txtparam.Value;

                        string MiniActiveTakeName = rtfText.Text;

                        if (MiniActiveTakeName == "")
                        {
                            if (MessageBox.Show("Some media contains no text.\r\nDo you want to continue?",
                                tk_warning, MessageBoxButtons.YesNo) == DialogResult.No)
                            {
                                vegas.Project.Regions.Clear();
                                return;
                            }
                        }
                        Region region = new Region(trackEvent.Start, trackEvent.Length, MiniActiveTakeName);
                        try
                        {
                            vegas.Project.Regions.Add(region);    
                        }
                        catch (Exception e)
                        {
                            MessageBox.Show(String.Format("Couldn't place Region at {0}\nThe error message: {1}", trackEvent.Start.ToString(), e.Message));
                        }
                    }
                }             
            }
            catch (Exception e)
            {
                MessageBox.Show(String.Format("Selected track has no media text\r\nor some media text are in (Legacy) Text.", e.Message),tk_error);
                vegas.Project.Regions.Clear();
                return;
            }            
        }
        if (CountTracks == 0)
        {
            MessageBox.Show("There were no selected tracks to process");
        }
    }
    
}

But, if you do want to check ahead of time, this modification to your code will let you do that. The subroutine has been converted to C# and it can now be properly called.
 

using System;
using System.Text;
using System.Collections.Generic;
using System.Windows.Forms;
using ScriptPortal.Vegas;

public class EntryPoint
{
    Vegas myVegas = null; // This is so the subroutine can access the "Vegas" variable

    public void FromVegas(Vegas vegas)
    {
        myVegas = vegas; // This is so the subroutine can access the "Vegas" variable
        string tk_error = "Text2Reg error:";
        string tk_warning = "Text2Reg Warning:";

        //find tracks
        Track videoTrack = FindTopTrackOfType(MediaType.Video);
        if (null == videoTrack))
        {
             MessageBox.Show(String.Format("No video track to export"),tk_error);
             return;  // Since there are no tracks to process
        }
        
        foreach (Track track in vegas.Project.Tracks)
        {

            try
            {
                // only the selected tracks            
                if (!track.Selected) continue;
                
                foreach (TrackEvent trackEvent in track.Events)
                {
                    Media media = trackEvent.ActiveTake.Media;
                    OFXEffect ofxEffect= media.Generator.OFXEffect;
                    OFXStringParameter txtparam = (OFXStringParameter) ofxEffect.FindParameterByName("Text");
                    RichTextBox rtfText = new RichTextBox();
                    rtfText.Rtf = txtparam.Value;

                    string MiniActiveTakeName = rtfText.Text;

                    if (MiniActiveTakeName == "")
                    {
                        if (MessageBox.Show("Some media contains no text.\r\nDo you want to continue?",
                            tk_warning, MessageBoxButtons.YesNo) == DialogResult.No)
                        {
                            vegas.Project.Regions.Clear();
                            return;
                        }
                    }
                    Region region = new Region(trackEvent.Start, trackEvent.Length, MiniActiveTakeName);
                    try
                    {
                        vegas.Project.Regions.Add(region);    
                    }
                    catch (Exception e)
                    {
                        MessageBox.Show(String.Format("Couldn't place Region at {0}\nThe error message: {1}", trackEvent.Start.ToString(), e.Message));
                    }
                }                
            }
            catch (Exception e)
            {
                MessageBox.Show(String.Format("Selected track has no media text\r\nor some media text are in (Legacy) Text.", e.Message),tk_error);
                vegas.Project.Regions.Clear();
                return;
            }            
        }
    }
    
    public Track FindTopTrackOfType(mediaType)
    {
        foreach (Track myTrack in myVegas.Project.Tracks)
        {
            if (myTrack.MediaType == mediaType)
            {
                return myTrack;
            }
        }
        return null;
    }

}

 

playmarius wrote on 12/1/2021, 6:48 PM

Many thanks. I tried ways to convert code from jscript to c sharp.

First script works. Second script gives me an error at this line:

"public Track FindTopTrackOfType(mediaType)"

SelectedTrackstoRegions3.cs(72) : Identifier expected

jetdv wrote on 12/2/2021, 8:56 AM

@playmarius, sorry about that. Change:

public Track FindTopTrackOfType(mediaType)

to:

public Track FindTopTrackOfType(MediaType mediaType)

You have to specify the variable types and I missed that one.