GP SoftwareTwitter
Opus FAQsManualCommandsObjects

Feature request - if process exists


#1

I found that there is a way to check if task is running or not by typing this in CMD:
tasklist /fi "imagename eq notepad.exe" |find ":"
I can find is notepad running (return none) or not (return info).

It will be very useful if there will be function in opus like this:
@ifprocessexists:nameofprocess
program.exe
@ifprocessexists:else
otherprogram.exe

Or, maybe is any way to read that by script. Unfortunatelly, I don't know how to do it, but if you can help me with that small script, I bet that I will be not the only person who find this useful.

Why that option may be useful?

  1. Preventing program from starting if is already started
  2. Preventing program from starting if other program is started.
  3. Running different functions if some program is started (for example - show image in default viewer or show in GIMP if is already open).

Of course I don't know if read that process name exists or not is easy for you to do. I hope it's not something that must be made with third party software but somehow is possible by adding small code.


#2

Thanks for the suggestion. We'll add this in the next version.


#3

While @modifiers still make sense to make this much easier, you can do this already using scripting and the WMI API.

An example can be found in the ExitOpusAndWait script in the How to Exit Directory Opus FAQ where it talks about automating shutdown and restart of Opus.

Here's the script code (this is a standalone script that runs outside of Opus, but the same objects and functions will work with scripts inside of Opus):

option explicit

Dim OpusInstallPath
OpusInstallPath = "C:\Program Files\GPSoftware\Directory Opus"

Private Function ProcessIsRunning( wmi, strProcess )
	Dim colProcessList

	Set colProcessList = wmi.Execquery("Select * from Win32_Process Where Name ='" & strProcess & "'")

	If colProcessList.Count > 0 Then
		ProcessIsRunning = True
	Else
		ProcessIsRunning = False
	End If

	Set colProcessList = Nothing
End Function

Private Function WaitForProcess( wmi, strProcess )
	Dim i

	WaitForProcess = False

	For i = 0 to 100
		If ProcessIsRunning( wmi, strProcess ) Then
			WScript.Sleep 200
		Else
			WaitForProcess = True
			Exit For
		End If
	Next
End Function

Dim Shell
Set Shell = CreateObject("WScript.Shell")

Dim wmi
Set wmi = Getobject("Winmgmts:")

If ProcessIsRunning(wmi, "dopusrt.exe") Then
	Shell.Run """" & OpusInstallPath & "\dopusrt.exe"" /dblclk=off", 1, TRUE
End If

If ProcessIsRunning(wmi, "dopus.exe") Then
	Shell.Run """" & OpusInstallPath & "\dopusrt.exe"" /CMD Close PROGRAM", 1, TRUE
End If


' Wait for dopusrt.exe and dopus.exe to finish shutting down.

Dim AllStopped
AllStopped = False

If WaitForProcess(wmi, "dopus.exe") Then
	If WaitForProcess(wmi, "dopusrt.exe") Then
		AllStopped = True
	End If
End If

If Not AllStopped Then
	MsgBox WScript.ScriptName & ": Error -- Opus is still running."
	WScript.Quit 1
End If

#4

It works! I made a test button:

Function ProcessIsRunning( wmi, strProcess )
	Dim colProcessList

	Set colProcessList = wmi.Execquery("Select * from Win32_Process Where Name ='" & strProcess & "'")

	If colProcessList.Count > 0 Then
		ProcessIsRunning = True
	Else
		ProcessIsRunning = False
	End If

	Set colProcessList = Nothing
End Function

Function OnClick(ByRef ClickData)
Dim wmi
Set wmi = Getobject("Winmgmts:")
Set objCmd = DOpus.CreateCommand
If ProcessIsRunning(wmi, "notepad.exe") Then
objCmd.RunCommand("Set SORTBY=modified")
Else
objCmd.RunCommand("Set SORTBY=name")
End If
End Function

It's just for a test (sorting as debug that shows is working or not). Is this code correct?

Anyway - modifiers will be much easier. I don't know if faster since it will be internal Opus function, but easier for sure.