Author Topic: Send Argument from one Command to another containing inline VB script  (Read 872 times)

Maldayne

  • Newbie
  • *
  • Posts: 2
Hi.

Firstly I am new to making my own commands for VA and writing script for it. I tried reading the documentation, but I don't seem to understand to correct steps to take and feel a bit like an idiot because I can't seem to figure it out.

During my practice I wanted to make commands to open specific applications on my computer, but with the goal of not duplicating code unnecessarily. I made a command that would check if a process is running, if so it would focus the program. If not then it would open the process. This worked fine, but as I want it to work for multiple programs, I wrote a reusable version of the code which would accept the two variables processName and processPath. With the following VB.Net inline script stored in the command 'Start Program':

Code: [Select]
Imports Microsoft.VisualBasic
Imports System
Imports System.Collections
Imports System.Collections.Generic
Imports System.Data
Imports System.Drawing
Imports System.Diagnostics
Imports System.Windows.Forms
Imports System.Linq
Imports System.Xml.Linq
Imports System.Threading.Tasks
Imports System.Runtime.InteropServices

Public Class VAInline

    <DllImport("user32.dll")> _
    Private Shared Function SetForegroundWindow(ByVal hWnd As IntPtr) As Boolean
    End Function

    Public Sub Main()
        ' Dim processName As String = processName
        ' Dim processPath As String = processPath

        Dim p As Process() = Process.GetProcessesByName(processName)
        If p.Count() = 0 Then
            Process.Start(processPath)
        Else
            Dim hWnd As IntPtr = p(0).MainWindowHandle
            SetForegroundWindow(hWnd)
        End If
    End Sub

End Class

Now if I have for example have a new command called 'Whatsapp' and the arguments:
processName: 'Whatapp'
processPath: 'C:\Users\%USERNAME%\AppData\Local\WhatsApp\WhatsApp.exe'

How would I pass these parameters as arguments to the 'Start Program' command from the 'Whatsapp' Command?

Ideally I would want a single command for all Programs with the correct pairing of the processNames and processPaths based in the input word, but baby steps  ;)

Pfeil

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 4759
  • RTFM
Re: Send Argument from one Command to another containing inline VB script
« Reply #1 on: January 18, 2023, 05:54:27 AM »
Are the native actions that perform those functions not suitable for your purposes?


Given that you're apparently more comfortable writing VB.NET, the "VoiceAttack Plugins (for the truly mad)" section of the documentation lists the methods that are available via the proxy object, including those to retrieve variable values.


To perform a lookup of a given value and retrieve associated data, you could use a dictionary within your inline function.

Personally I tend to set up commands like that so that running it from the command list will output the appropriate spoken phrases for pasting into the "When I say" field. You could use tokens in the command name and set it up to work dynamically, but unless your lookup list is dynamically populated that seems unnecessarily complicated, especially given that the command would then no longer be self-contained (as the variable value would need to be populated before reloading the profile).

Maldayne

  • Newbie
  • *
  • Posts: 2
Re: Send Argument from one Command to another containing inline VB script
« Reply #2 on: January 18, 2023, 07:24:51 AM »
Thanks for the assistance!

I tried using the native actions, but honestly I struggled to get it to do what I wanted so the VB code was easier to implement, with exception of the arguments part.

Thank for mentioning the section "VoiceAttack Plugins (for the truly mad)" as I think this is the most likely solution at this point for me, and I will dig into tokens too.