Example scripts for COM automation
The example scripts below demonstrate COM automation via .vbs scripts and via web page implementing VBscript. See also: Echoview example scripts in VBS, Python, R, Perl, and Matlab.
To run scripts in Echoview you will need to have a Automation module license.
If you have any trouble with the examples below please contact Echoview for support.
Examples
COM script example |
Description |
A single export. |
|
Multiple exports from multiple variables. |
|
Multiple exports from multiple variables in multiple EV files |
Multiple exports from multiple variables in multiple EV files. |
Multiple exports from multiple variables in all EV files in a folder |
Multiple exports from multiple variables in all EV files in a folder. |
Lists the variables in any open EV files. |
|
Picks a line on a particular variable in a particular file and exports an .evl file for the picked line. |
|
Changes an operand for the Statistical combination operator. |
|
Web page COM | Uses an Internet Explorer web page script to list all the variables in a selected EV file. |
Windows Script File (.wsf) with one job and two scripts (VBScript and JScript). |
The examples can be edited to create your own scripts. The text in maroon are the strings that will need to be changed to suit your own files and export needs.
A single export
To try the sample VBS script:
- Save the following code to a file called A single export (COM) .vbs
- Run A single export (COM) .vbs
This script will open FileA.EV located in C:\Echoview\Example Scripts\EVFiles folder. The variable Sv Q1 telegrams T1 will be processed and an Integration by Regions will be exported to the output folder Script1Output. The output file will be called: FileA.EV-Sv1-ARIN.csv.
Note that the output folder must exist prior to running the script or the export will fail.
A single export (COM).vbs
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' ' A single export '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' ' Open the EV file Dim EvApp: Set EvApp = CreateObject("EchoviewCom.EvApplication") Dim EvFile: Set EvFile = EvApp.OpenFile("C:\Echoview\Example Scripts\Example scripts\EVFiles\FileA.EV") If EvFile Is Nothing Then MsgBox "Failed to open EV file" Else ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' ' Get the variable Dim Var: Set Var = EvFile.Variables.FindByName("Sv Q1 telegrams T1") If Var Is Nothing Then MsgBox "Failed to find variable" Else Dim VarAc: Set VarAc = Var.AsVariableAcoustic If VarAc Is Nothing Then MsgBox "Variable is not acoustic" Else ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' ' Export the variable If Not VarAc.ExportIntegrationByRegions("C:\Echoview\Example Scripts\Example scripts\Script1Output\FileA-Sv1-ARIN.csv", Nothing) Then MsgBox "Failed to export by regions" End If End If End If End If ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' ' Tidy up EvApp.CloseFile(EvFile) EvApp.Quit
Multiple exports from multiple variables
To try the sample VBS script:
- Save the following code to a file called Multiple exports from multiple variables (COM).vbs
- Open one or more EV files in Echoview.
- Run Multiple exports from multiple variables (COM).vbs
This script will open FileA.EV located in C:\Echoview\Example Scripts\Example scripts folder. The script will do an Integration by Regions and an Integration by Cells on two variables: Sv Q1 telegrams T1 and Sv Q2 telegrams T2.
Note that the output folder must exist prior to running the script or the export will fail.
Multiple exports from multiple variables (COM) .vbs
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' ' Multiple exports from multiple variables ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' ' The main program Dim EvApp: Set EvApp = CreateObject("EchoviewCom.EvApplication") Dim EvFile: Set EvFile = EvApp.OpenFile("C:\Echoview\Example Scripts\Example scripts\EVFiles\FileA.EV") If EvFile Is Nothing Then MsgBox "Failed to open EV file" EvApp.Quit WScript.Quit End If Export("Sv Q1 telegrams T1") Export("Sv Q2 telegrams T2") EvApp.CloseFile(EvFile) EvApp.Quit ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' ' A subroutine to export the named variable Sub Export(VarName) Dim Var: Set Var = EvFile.Variables.FindByName(VarName) If Var Is Nothing Then MsgBox "Failed to find variable '" & VarName & "'" Exit Sub End If Dim VarAc: Set VarAc = Var.AsVariableAcoustic If VarAc Is Nothing Then MsgBox "Variable '" & VarName & "' is not acoustic" Exit Sub End If If Not VarAc.ExportIntegrationByRegions("C:\Echoview\Example Scripts\Example scripts\Script1Output\FileA-" & Left(VarName, 2) & Right(VarName, 1) & "-ARIN.csv", Nothing) Then MsgBox "Failed to export '" & VarName & "' by regions" End If If Not VarAc.ExportIntegrationByCells("C:\Echoview\Example Scripts\Example scripts\Script1Output\FileA-" & Left(VarName, 2) & Right(VarName, 1) & "-ACIN.csv", Nothing) Then MsgBox "Failed to export '" & VarName & "' by cells" End If End Sub
Multiple exports from multiple variables in multiple EV files
To try the sample VBS script:
- Save the following code to a file called Multiple exports from multiple variables in multiple EV files (COM).vbs
- Open one or more EV files in Echoview.
- Run Multiple exports from multiple variables in multiple EV files (COM).vbs
This script will open FileA.EV and FileB.EV located in C:\Echoview\Example Scripts\Example scripts\EVFiles folder. The script will run two exports on two variables in two EV files.
Note that the output folder must exist prior to running the script or the export will fail.
Multiple exports from multiple variables in multiple EV files (COM).vbs
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' ' Multiple exports from multiple variables in multiple EV files ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' Dim fso: Set fso = CreateObject("Scripting.FileSystemObject") Dim EvApp: Set EvApp = CreateObject("EchoviewCom.EvApplication") ExportFromFile "C:\Echoview\Example Scripts\Example scripts\EVFiles\FileA.EV" ExportFromFile "C:\Echoview\Example Scripts\Example scripts\EVFiles\FileB.EV" EvApp.Quit ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' ' A subroutine to export from the named file Sub ExportFromFile(FileName) Dim EvFile: Set EvFile = EvApp.OpenFile(FileName) If EvFile Is Nothing Then MsgBox "Failed to open EV file '" & FileName & "'" Exit Sub End If ExportVariable EvFile, "Sv Q1 telegrams T1" ExportVariable EvFile, "Sv Q2 telegrams T2" EvApp.CloseFile(EvFile) End Sub ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' ' A subroutine to export the named variable Sub ExportVariable(EvFile, VarName) Dim Var: Set Var = EvFile.Variables.FindByName(VarName) If Var Is Nothing Then MsgBox "Failed to find variable '" & VarName & "' in EV file '" & EvFile.FileName & "'" Exit Sub End If Dim VarAc: Set VarAc = Var.AsVariableAcoustic If VarAc Is Nothing Then MsgBox "Variable '" & VarName & "' is not acoustic in EV file '" & EvFile.FileName & "'" Exit Sub End If Dim OutFileName: OutFileName = CreateFileName(EvFile.FileName, VarName, "ARIN") If Not VarAc.ExportIntegrationByRegions(OutFileName, Nothing) Then MsgBox "Failed to export '" & VarName & "' by regions from EV file '" & EvFile.FileName & "'" End If OutFileName = CreateFileName(EvFile.FileName, VarName, "ACIN") If Not VarAc.ExportIntegrationByCells(OutFileName, Nothing) Then MsgBox "Failed to export '" & VarName & "' by cells from EV file '" & EvFile.FileName & "'" End If End Sub ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' ' A function to create the output file name Function CreateFileName(FileName, VarName, OpName) Dim FolderName: FolderName = "C:\Echoview\Example Scripts\Example scripts\Script1Output\" Dim File: Set File = fso.GetFile(FileName) CreateFileName = FolderName & Left(File.Name, Len(File.Name) - 3) & "-" & Left(VarName, 2) & Right(VarName, 1) & "-" & OpName & ".csv" End Function
Multiple exports from multiple variables in all EV files in a folder
To try the sample VBS script:
- Save the following code to a file called Multiple exports from multiple variables in all EV files in a folder (COM) .vbs
- Open one or more EV files in Echoview.
- Run Multiple exports from multiple variables in all EV files in a folder (COM) .vbs
This script will run the same set of export tasks on the same set of variables in EVERY EV file in the input folder.
Multiple exports from multiple variables in all EV files in a folder (COM) .vbs
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' ' Multiple exports from multiple variables in all EV files in a folder ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' Dim FolderName: FolderName = "C:\Echoview\Example Scripts\Example Scripts\EVFiles" Dim fso: Set fso = CreateObject("Scripting.FileSystemObject") Dim Files: Set Files = fso.GetFolder(FolderName).Files Dim EvApp: Set EvApp = CreateObject("EchoviewCom.EvApplication") For Each File In Files ExportFromFile FolderName & "\" & File.Name Next EvApp.Quit ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' ' A subroutine to export from the named file Sub ExportFromFile(FileName) If (UCase(Right(FileName, 3)) ".EV" Or UCase(Right(FileName, 12)) = " (BACKUP).EV") Then Exit Sub End If Dim EvFile: Set EvFile = EvApp.OpenFile(FileName) If EvFile Is Nothing Then MsgBox "Failed to open EV file '" & FileName & "'" Exit Sub End If ExportVariable EvFile, "Sv Q1 telegrams T1" ExportVariable EvFile, "Sv Q2 telegrams T2" ExportVariable EvFile, "Sv Q3 telegrams T3" EvApp.CloseFile(EvFile) End Sub ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' ' A subroutine to export the named variable Sub ExportVariable(EvFile, VarName) Dim Var: Set Var = EvFile.Variables.FindByName(VarName) If Var Is Nothing Then MsgBox "Failed to find variable '" & VarName & "' in EV file '" & EvFile.FileName & "'" Exit Sub End If Dim VarAc: Set VarAc = Var.AsVariableAcoustic If VarAc Is Nothing Then MsgBox "Variable '" & VarName & "' is not acoustic in EV file '" & EvFile.FileName & "'" Exit Sub End If Dim OutFileName: OutFileName = CreateFileName(EvFile.FileName, VarName, "ARIN") If Not VarAc.ExportIntegrationByRegions(OutFileName, Nothing) Then MsgBox "Failed to export '" & VarName & "' by regions from EV file '" & EvFile.FileName & "'" End If OutFileName = CreateFileName(EvFile.FileName, VarName, "ACIN") If Not VarAc.ExportIntegrationByCells(OutFileName, Nothing) Then MsgBox "Failed to export '" & VarName & "' by cells from EV file '" & EvFile.FileName & "'" End If OutFileName = CreateFileName(EvFile.FileName, VarName, "ABIN") If Not VarAc.ExportIntegrationByRegionsByCells(OutFileName, Nothing) Then MsgBox "Failed to export '" & VarName & "' by regions by cells from EV file '" & EvFile.FileName & "'" End If End Sub ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' ' A function to create the output file name Function CreateFileName(FileName, VarName, OpName) Dim FolderName: FolderName = "C:\Echoview\Example Scripts\Example scripts\Script1Output\" Dim File: Set File = fso.GetFile(FileName) CreateFileName = FolderName & Left(File.Name, Len(File.Name) - 3) & "-" & Left(VarName, 2) & Right(VarName, 1) & "-" & OpName & ".csv" End Function
ListVariables
To try the sample VBS script:
- Save the following code to a file called ListVariables.vbs
- Open one or more EV files in Echoview.
- Run ListVariables.vbs
This script demonstrates:
- Connecting to Echoview.
- Testing for an Echoview COM license.
- Logging to the message window and script log.
- Traversing the object hierarchy.
- Accessing variables according to type.
ListVariables.vbs
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' ' List variables ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' ' Attach to Echoview Dim EvApp: Set EvApp = CreateObject("EchoviewCom.EvApplication") If Not EvApp.IsLicensed Then MsgBox "Echoview is not licensed for COM scripting" WScript.Quit End If ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' ' List files Dim FileIndex for FileIndex = 0 to EvApp.EvFiles.Count-1 Dim EvFile: Set EvFile = EvApp.EvFiles.Item(FileIndex) ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' ' List variables EvApp.Log "EV File '" & EvFile.FileName & "' contains " & EvFile.Variables.Count & " variables" Dim VarIndex for VarIndex = 0 to EvFile.Variables.Count-1 Dim Var: Set Var = EvFile.Variables.Item(VarIndex) ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' ' Report variable type Dim Description: Description = " is of unspecified type" If Not Var.AsVariableAcoustic Is Nothing Then Description = " is acoustic" If Not Var.AsVariableVirtual Is Nothing Then Description = Description & " and virtual" End If Elseif Not Var.AsVariableVirtual Is Nothing Then Description = " is virtual" End If EvApp.Log Var.Name & Description Next Next
PickLine.vbs
To try the sample VBS script:
- Save the following code to a file called PickLine.vbs.
- Open one or more EV files in Echoview.
- Run PickLine.vbs
Note: The output folder 'Picked Lines' must exist prior to running the script or the export will fail.
This script demonstrates:
- Opening and using a text log file.
- Opening and closing an EV file.
- Using a VBS script subroutine.
- Finding a named variable.
- Picking a line.
- Exporting the line to an .evl file.
PickLine.vbs
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' ' Pick Line ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' Dim ScriptsFolder: ScriptsFolder = "C:\Echoview\Example Scripts\Example Scripts\" Dim FSO: Set FSO = CreateObject("Scripting.FileSystemObject") Dim fOut: Set fOut = FSO.OpenTextFile("PickLine.txt", 2, true) Dim EvApp: Set EvApp = CreateObject("EchoviewCom.EvApplication") fOut.WriteLine "Echoview version: " & EvApp.Version & " at " & Date & " " & Time ' ' Open the EV file Dim EvFile: Set EvFile = EvApp.OpenFile(ScriptsFolder & "EVFiles\FileA.EV") If EvFile Is Nothing Then fOut.WriteLine "Could not open EV file - " & EvApp.GetLastLogMessage WScript.Quit End If fOut.WriteLine "EV file '" & EvFile.FileName & "'"PickLine "Sv Q1 telegrams T1" ' ' Close EV file and quit If Not EvApp.CloseFile(EvFile) Then fOut.WriteLine EvApp.GetLastLogMessage End If EvApp.Quit
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' ' Pick line in the variable Sub PickLine(VarName) ' ' Get the variable Dim Var: Set Var = EvFile.Variables.FindByName(VarName) If Var Is Nothing Then fOut.WriteLine EvApp.GetLastLogMessage Exit Sub End If Dim VarAc: Set VarAc = Var.AsVariableAcoustic If VarAc Is Nothing Then fOut.WriteLine Var.Name & " is not an acoustic variable" Exit Sub End If ' ' Pick Line Dim Line: Set Line = VarAc.PickLine(VarName & " - Picked Line") If Line Is Nothing Then fOut.WriteLine EvApp.GetLastLogMessage Exit Sub Else fOut.WriteLine "'" & Line.Name & "'" End If ' ' Export line Dim FileName: FileName = ScriptsFolder & "Picked Lines\" & VarName & ".evl" If Line.Export(FileName) Then fOut.WriteLine "Exported to '" & FileName & "'" Else fOut.WriteLine EvApp.GetLastLogMessage End If End Sub
StatCombinationChangeOperands.vbs
To try the sample VBS script:
- Save the following code to a file called StatCombinationChangeOperands.vbs.
- Run StatCombinationChangeOperands.vbs
Note:
This script demonstrates:
- Opening an EV file (that contains the Statistical combination variable).
- Specifying and setting the acoustic variable Sv pings for Operand 1.
StatCombinationChangeOperands.vbs
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' ' Statistical combination operator change operand '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' ' 'Open the EV file Dim EvApp: Set EvApp = CreateObject("EchoviewCom.EvApplication") Dim EvFile: Set EvFile = EvApp.OpenFile("C:\YourFolder\Stat Combo line.EV") strLineName = "Statistical Combination 1" strOpName = "Sv pings" If EvFile Is Nothing Then MsgBox "Failed to open EV file" WScript.Quit End If ' 'Look for the Statistical combination variable Dim Line: Set Line = EvFile.Variables.FindByName(strLineName) If Line Is Nothing Then Msgbox "Failed to find '" + strLineName + "'" WScript.Quit End If ' 'Look for the operands of the Statistical combination variable Dim Op: Set Op = EvFile.Variables.FindByName(strOpName) If Op Is Nothing Then Msgbox "Failed to find '" + strOpName + "'" WScript.Quit End If ' 'Specify new operand 1 for the Statistical combination variable Line.SetFullName "Pomatoceros" Msgbox "New name: " + Line.Name Msgbox Line.OperatorName + " (" + CStr(Line.Operator) + ") operand 2: " + Line.GetOperand(2).Name Line.SetOperand 1, Op Msgbox Line.OperatorName + " (" + CStr(Line.Operator) + ") operand 1: " + Line.GetOperand(1).Name
COM automation web page
This example uses an ActiveX object. Use an internet browser that supports ActiveX. You may need to customize the security settings in the internet browser.
Various security settings affect the execution of ActiveX objects in web pages. The default settings may safeguard against exploitation by embedded ActiveX objects and implement a Medium-Low security level. If the sample script below does not function, select a Low security level setting.
To try the sample web page:
- Save the following code to a file named Web page COM.html
- Open Web page COM.html in Internet Explorer
- Click Open or Connect to Echoview.
- Click Browse and select an EV file.
- Click List variables
- A page lists the variables in the EV file that was opened and closed.
Note: This html script executes once; it connects to Echoview, opens and then closes the EV file and lists the variables in the EV file. To list variables from another EV file use the Internet Explorer Back arrow and follow the script steps again.
Web page COM.html
<html>
<head>
<script language="VBScript" type="text/vbscript">
'------------------------------------------------------------------------------
Dim FSO: Set FSO = CreateObject("Scripting.FileSystemObject")
Dim EvApp: Set EvApp = Nothing
'------------------------------------------------------------------------------
Sub OpenEchoview()
If EvApp Is Nothing Then
Set EvApp = CreateObject("EchoviewCom.EvApplication")
End If
End Sub
'------------------------------------------------------------------------------
Sub DisconnectEchoview
Set EvApp = Nothing
End Sub
'------------------------------------------------------------------------------
Sub ListVariables()
If EvApp Is Nothing Then
Exit Sub
End If
Dim FileName: FileName = document.getElementById("txtEVFile").value
If FileName = "" Then
Exit Sub
End If
Dim EvFile: Set EvFile = EvApp.OpenFile(FileName)
If Not EvFile Is Nothing Then
Dim BaseName: BaseName = FSO.GetFileName(FileName)
Document.Open
Document.Writeln "<HTML><TITLE>Variables in " & BaseName & "</TITLE>"
Document.Writeln "<BODY>"
Document.Writeln "<H1>" & BaseName & "</H1>"
Document.Writeln "<UL>"
For v = 0 To EvFile.Variables.Count - 1
Dim Var: Set Var = EvFile.Variables(v)
If Not Var Is Nothing Then
Dim MeasurementCount: MeasurementCount = ""
Dim VarAc: Set VarAc = Var.AsVariableAcoustic
If Not VarAc Is Nothing Then
MeasurementCount = " (" & CStr(VarAc.MeasurementCount) & " pings)"
End If
Document.Writeln "<LI>" & Var.Name & " " & MeasurementCount & "</LI>"
End If
Next
Document.Writeln "</UL>"
End If
Document.Writeln "</BODY>"
Document.Close
EvApp.CloseFile(EvFile)
End Sub
'------------------------------------------------------------------------------
</script>
</head>
<body>
<form action="" method="post" enctype="multipart/form-data" name="form1">
<input name="btnOpenConnect" type="button" id="btnOpenConnect" value="Open or Connect to Echoview" onClick="OpenEchoview()">
<input name="btnDisconnect" type="button" id="btnDisconnect" value="Disconnect from Echoview" onClick="DisconnectEchoview()">
<br>
<input name="txtEVFile" type="file" id="txtEVFile" size="100">
<br>
<input name="btnListVariables" type="button" id="btnListVariables" value="List Variables" onClick="ListVariables()">
</form>
</body>
</html>