Get pid of the process started using RunProgram

Technical support and scripting issues

Moderators: Dorian (MJT support), JRL

Post Reply
akshay11june
Newbie
Posts: 1
Joined: Mon Feb 26, 2018 7:06 am

Get pid of the process started using RunProgram

Post by akshay11june » Mon Feb 26, 2018 8:32 am

Hi,
I am using Macro Scheduler's trial version to make a windows task automated. Till now, its working well and i am looking forward to take the product license as well.
One problem i am facing is that - i am not able to get the process id of the process started using RunProgram command.

Is there a way to get the same in Macro Scheduler ??


Thanks,
Akshay

User avatar
JRL
Automation Wizard
Posts: 3276
Joined: Mon Jan 10, 2005 6:22 pm
Location: Iowa

Re: Get pid of the process started using RunProgram

Post by JRL » Sun Dec 30, 2018 6:04 am

Here is a Macro Scheduler script subroutine that uses the GetProcessIDs> function to retrieve a list of process IDs for a specified program prior to the program being run by the script, then again immediately after the program has been run by the script. The two lists are compared and the latest process ID (the process ID of the program run by the script) is captured and assigned to a variable.

Place this subroutine in your script and call the subroutine with parameters similar to the example. The example is using msPaint. The process ID will be assigned to the result variable. Use the result variable as needed.

Code: Select all

//Usage:
//GoSub>GetProgramProcessID,PathToTheProgramLocation\,ProgramToBeRun.exe,ProcessIDVariable
GoSub>GetProgramProcessID,C:\Windows\System32\,mspaint.exe,vResult
//
MDL>vResult
//
//
SRT>GetProgramProcessID
Let>RP_Wait=0
Let>RP_Windowmode=1
//If the program is not running start it and get its process ID.
ProcessExists>GetProgramProcessID_var_2,vProcExistTest
If>vProcExistTest=False
  RunProgram>%GetProgramProcessID_var_1%%GetProgramProcessID_var_2%
  Wait>0.5
  GetProcessIDs>GetProgramProcessID_var_2,vPID
  Let>%GetProgramProcessID_var_3%=vPID_1
  Goto>LbLPIDFoundComplete
Else
//If the program is already running, get all current process IDs for this program.
  GetProcessIDs>GetProgramProcessID_var_2,vProcIdStart
//Start the program again
  RunProgram>%GetProgramProcessID_var_1%%GetProgramProcessID_var_2%
  Wait>0.5
//Get a second list of Process IDs which will include the one just started.
  GetProcessIDs>GetProgramProcessID_var_2,vProcIdEnd
EndIf
//Cycle through the new process ID list and compare to the old
//process ID list to discover the process ID of the program
//started by this script.
Let>PIDE={%vProcIdEnd_Count%+1}
Repeat>PIDE
  Sub>PIDE,1
  Let>vPIDEvalue=vProcIdEnd_%PIDE%
  ArrayFind>vProcIdStart,vPIDEvalue,vPIDRes,0
  If>vPIDRes=-1
    Let>%GetProgramProcessID_var_3%=vPIDEvalue
    Goto>LbLPIDFoundComplete
  EndIf
Until>PIDE=1
Let>%GetProgramProcessID_var_3%=Process ID not found
Label>LbLPIDFoundComplete
END>GetProgramProcessID

User avatar
Grovkillen
Automation Wizard
Posts: 360
Joined: Fri Aug 10, 2012 2:38 pm
Location: Hudiksvall, Sweden
Contact:

Re: Get pid of the process started using RunProgram

Post by Grovkillen » Mon Jun 24, 2019 5:33 am

I thought I'd add my way of doing this:

Code: Select all

Let>APP_EXE=your_unique_exe_file_name
GetProcessIDs>%APP_EXE%.exe,RAW_LIST
Let>TEMP_TEST=RAW_LIST_1*0
If>TEMP_TEST=0
  Let>SCRIPT_PID=RAW_LIST_1
Else>
  Let>SCRIPT_PID=EDITOR
Endif>
I have all my scripts populate a "script_heartbeat.ini" file with stuff regarding their position, run time, handles... etc. etc.

The "Let>SCRIPT_PID=MS" is only used to show that if no process is found it's very likely you're running the script in the editor, thus I let the variable equals to "EDITOR".
Let>ME=%Script%

Running: MS14.5.4

Post Reply