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
Get pid of the process started using RunProgram
Moderators: JRL, Dorian (MJT support)
-
- Newbie
- Posts: 1
- Joined: Mon Feb 26, 2018 7:06 am
Re: Get pid of the process started using RunProgram
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.
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
- Grovkillen
- Automation Wizard
- Posts: 1132
- Joined: Fri Aug 10, 2012 2:38 pm
- Location: Bräcke, Sweden
- Contact:
Re: Get pid of the process started using RunProgram
I thought I'd add my way of doing this:
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".
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>
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".