Author Topic: Check if process or app is running  (Read 10796 times)

Exergist

  • Global Moderator
  • Sr. Member
  • *****
  • Posts: 405
  • Ride the lightning
Check if process or app is running
« on: December 15, 2017, 10:57:50 AM »



EDIT by Pfeil: The "{PROCESSCOUNT:}" token was implemented in v1.7.1, and is now a native feature that does not require an inline function





VoiceAttack currently does not have a "{PROCESSEXISTS:}" token, but I believe this functionality would be useful to have. So as a workaround we can achieve this with an inline function.

Code: [Select]
// Define the process (or application) name you want to check
Set Text [~ProcessToCheck] to 'mspaint'

// Search for the specified process among all running processes
Inline C# Function: Check if process is running, wait until execution finishes

// Perform additional actions based on result of process search
Begin Boolean Compare : [ProcessCheckResult] Equals True
    Say, 'Process is running'
Else
    Say, 'Process not found'
End Condition

...with the inline function using System.dll as the referenced assembly along with the following code:

Code: [Select]
using System.Diagnostics;

public class VAInline
{
    public void main()
    {
        string processName = VA.GetText("~ProcessToCheck");

        Process[] pname = Process.GetProcessesByName(processName);

        if (pname.Length == 0)
            VA.SetBoolean("ProcessCheckResult", false);
        else
            VA.SetBoolean("ProcessCheckResult", true);

        //if (pname.Length == 0)
            //VA.WriteToLog("Process doesn't exist", "red");
        //else
            //VA.WriteToLog("Process is running", "green");
    }
}

// References:
  // https://stackoverflow.com/questions/262280/how-can-i-know-if-a-process-is-running

I've included some code lines in the inline function (commented out) if you just want to output the result to the event log while minimizing the VoiceAttack command actions. I've also provided the above content in the attached profile.

I've found that the "ProcessToCheck" value needs to be the executable name (without the .exe). So for the above example we're looking for the Microsoft Paint application. If you load Paint, open up the Task Manager, and search for Paint's Image Name you'll find it listed as "mspaint.exe." So as the above sample shows the "ProcessToCheck" would be "mspaint". I've also found that the "ProcessToCheck" value does not appear to be case sensitive.

Cheers! :)
« Last Edit: February 03, 2022, 11:08:56 PM by Pfeil »

Exergist

  • Global Moderator
  • Sr. Member
  • *****
  • Posts: 405
  • Ride the lightning
Re: Check if process or app is running
« Reply #1 on: April 24, 2018, 12:25:35 PM »
The token '{PROCESSEXISTS:}' was added as of VoiceAttack v1.7.0.4. This natively offers the functionality provided by the previously described inline function but in a much simpler way.

Here is a basic example of this token in action:
Code: [Select]
// Store name of process of interest in text variable. You can also use wildcards (*)
Set Text [~~MyProcess] to 'notepad'

// Check if process exists
Begin Text Compare : [{PROCESSEXISTS:~~MyProcess}] Equals '1'
    // Output info to event log
    Write '[Blue] The process exists!' to log
Else
    // Output info to event log
    Write '[Blue] The process does not exist' to log
End Condition
« Last Edit: April 24, 2018, 12:29:34 PM by Exergist »

Tumbler

  • Newbie
  • *
  • Posts: 7
Re: Check if process or app is running
« Reply #2 on: June 01, 2018, 10:28:45 AM »
Exergist,

For the life of me, i cant get it to work.
Literally copied the command, line by line, into my voice attack profile to test it but it doesnt work. Even with Notepad open, the command doesnt recognize its open.

Pfeil

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 4759
  • RTFM
Re: Check if process or app is running
« Reply #3 on: June 01, 2018, 10:56:27 AM »
Could you post what you have in your action list? I rebuilt the example, and it works as expected on my machine.

Tumbler

  • Newbie
  • *
  • Posts: 7
Re: Check if process or app is running
« Reply #4 on: June 01, 2018, 04:36:07 PM »
Code: [Select]
Set Text [[~~MyProcess]] to 'notepad'
Begin Text Compare : [[{PROCESSEXISTS:~~MyProcess}]] Equals '1'
    Say, 'Program running'
Else
    Say, 'Executing'  (and wait until it completes)
    Run application 'C:\Windows\notepad.exe'
End Condition - Exit when condition met

Pretty much same as yours. I have tried in the stable version and beta. Same result. Always opens notepad.

Pfeil

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 4759
  • RTFM
Re: Check if process or app is running
« Reply #5 on: June 02, 2018, 12:59:22 AM »
Code: [Select]
Set Text [[~~MyProcess]] to 'notepad'
Begin Text Compare : [[{PROCESSEXISTS:~~MyProcess}]] Equals '1'
    Say, 'Program running'
Else
    Say, 'Executing'  (and wait until it completes)
    Run application 'C:\Windows\notepad.exe'
End Condition - Exit when condition met

Pretty much same as yours. I have tried in the stable version and beta. Same result. Always opens notepad.

You've added square brackets to the variable names/tokens.

"[~~MyProcess]" should still work, it just won't be a scoped variable, but "[{PROCESSEXISTS:~~MyProcess}]" will never return "1", instead it would return "[Not Set]" because "~~MyProcess" isn't assigned a value(and being a scoped variable, it would have to be assigned a value within this or another command in the same execution chain), and the square brackets don't do anything, so VoiceAttack leaves treats them as regular text.


As a side note, "End Condition - Exit when condition met" is unnecessary unless the command loops; When execution reaches the end of the action list, the command will exit anyway, there's no need to do it explicitly.

Tumbler

  • Newbie
  • *
  • Posts: 7
Re: Check if process or app is running
« Reply #6 on: June 02, 2018, 08:18:31 AM »
Sorry, i copy pasted the wrong command. That was an earlier attemp that which i actually noticed that mistake.

Code: [Select]
Set Text [~~MyProcess] to 'notepad'
Begin Text Compare : [{PROCESSEXISTS:~~MyProcess}] Equals '1'
    Say, 'Notepad open'
Else
    Say, 'Opening Notepad'
    Run application 'C:\Windows\System32\notepad.exe'
End Condition

I quite didnt understand the part about the process. When text compare the PROCESSEXISTS to ~~Myprocess which value will return? Value has been Set or 1?

Pfeil

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 4759
  • RTFM
Re: Check if process or app is running
« Reply #7 on: June 02, 2018, 08:58:09 AM »
What you have should work. Which version of VoiceAttack are you running?

I quite didnt understand the part about the process. When text compare the PROCESSEXISTS to ~~Myprocess which value will return? Value has been Set or 1?
Tokens always return text, so "{PROCESSEXISTS:}" will either return a literal "0" or a literal "1", which you then compare to a literal "1".

Text is unique in that it accepts tokens as well as variables in the same field, "Has Been Set" only applies to variables, as tokens are essentially a function, not a datatype.

Tumbler

  • Newbie
  • *
  • Posts: 7
Re: Check if process or app is running
« Reply #8 on: June 02, 2018, 09:42:58 AM »
Ok i got it working now, which is wierd because i only deleted the command and redid it. I was always running as administrator and using the newest Beta version of VA.

F*** me, really dont know what was the issue. But it works now. Thank you for your patience and time Pfeil, glad you could help and explain it to me.

Exergist

  • Global Moderator
  • Sr. Member
  • *****
  • Posts: 405
  • Ride the lightning
Re: Check if process or app is running
« Reply #9 on: June 04, 2018, 07:58:49 AM »
Thanks for chiming in Pfeil!

I'm glad you got it working Tumbler!

Tumbler

  • Newbie
  • *
  • Posts: 7
Re: Check if process or app is running
« Reply #10 on: June 04, 2018, 09:26:53 AM »
All thanks to you guys!

Cheers for the support.