Sign in to follow this  
Followers 0
redndahead

One Process

4 posts in this topic

I created this program so that our students couldn't open two instances of a program by clicking one hundred times on an icon.

Here is how you use it.

Create a shortcut to the exe you compile with this. Add these command line options

OneProcess.exe "Full Program Path" "Working Dir" "Windows that*the program*uses seperated*by a asterisk" "Process name if different than the exe name"

All fields are required and required in that order. If you don't need one of those fields just use " " the only one that has to have something in it is the Full Program Path

Just change the Icon and away you go.

If it finds a process and a matching window it will activate the matching window and pop up an error message stating that it is already running. If there is no matching window it will just state that it is already running.

Here's the code

autoitsetoption("WinTitleMatchMode",2)

If $CmdLine[0]< 1 Then
    Exit
EndIf

$PosNumber = StringInStr($CmdLine[1],".")
$PosNumber = $PosNumber + 3

$ProgramLine = StringLeft($CmdLine[1],$PosNumber)

$splitline = StringSplit($ProgramLine,"\")

$processname = $splitline[$splitline[0]]

If $CmdLine[0] > 3 Then
    If $CmdLine[4] <> ""  Then
  $processname = $CmdLine[4]
    EndIF
EndIf

If ProcessExists($processname) Then
    If $CmdLine[3] = "" Then
  MsgBox(0,"Program Running","The program is already running",5)
  Exit
    EndIf

    $windowtitle = StringSplit($CmdLine[3],"*")
    for $i = 1 to $windowtitle[0]
  If winexists($windowtitle[$i]) Then
     WinShow($windowtitle[$i],"",@SW_RESTORE)
     winActivate($windowtitle[$i])
     MsgBox(0,"Program Running","The program is already running",5)
     Exit
  EndIf
    Next
    MsgBox(0,"Program Running","The program is already running",5)
    Exit
EndIf

$ProgramPath = $cmdline[2]

If $ProgramPath = " " then
    $StringLength = StringLen($ProcessName)
    $ProgramPath = StringTrimRight($ProgramLine,$StringLength)
EndIf 

MsgBox(0," ",$ProgramPath)
Run($CmdLine[1],$ProgramPath)
Exit

Hope you like it

red

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

Nice script :whistle:

I had used a third-party program Activate for similar behavior, but I like using AutoIt instead when possible.

One suggestion: Change the second line to If $CmdLine[0] < 4 Then Exit

Edited by CyberSlug

Use Mozilla | Take a look at My Disorganized AutoIt stuff | Very very old: AutoBuilder 11 Jan 2005 prototype I need to update my sig!

Share this post


Link to post
Share on other sites

Yeah that's possible but really the fourth option is still optional. I just like to say it is necessary for safety's sake. I wrote that line to make sure there was no problems by people not having any command line options. But that makes sense.

Thanks for the suggestion

Share this post


Link to post
Share on other sites

Decided to change it a little bit. Now uses switches to define each option. Every one is optional except /f.

/f Fullpath and filename of the program including any switches

/d Alternate working directory

/p Alternate process to check

/w Windows the program uses seperated by an *.

Tell me if you see anything wrong. Hope people can use it. I know it's a life saver now in my lab.

red

autoitsetoption("WinTitleMatchMode",2)

$Program = "Null"
$ProgramPath = "Null"
$ProcessName = "Null"
Dim $WindowTitle[10]

If $CmdLine[0]< 1 Then
    ErrorCode(4)
EndIf

For $i=1 to $CmdLine[0] Step 2
    If $CmdLine[0]< $i +1 Then
  ErrorCode(3)
    EndIf
    Select
  Case $CmdLine[$i] = "/f"
     If $ProcessName = "Null" Then
    $PosNumber = StringInStr($CmdLine[$i+1],".")
    $PosNumber = $PosNumber + 3
    $ProgramLine = StringLeft($CmdLine[$i+1],$PosNumber)
    $splitline = StringSplit($ProgramLine,"\")
    $processname = $splitline[$splitline[0]]
     EndIf
     
     If $ProgramPath = "Null" Then
    $StringLength = StringLen($ProcessName)
    $ProgramPath = StringTrimRight($ProgramLine,$StringLength)
     EndIf
     
     $Program = $CmdLine[$i+1]
    
  Case $CmdLine[$i] = "/d"
     $ProgramPath = $CmdLine[$i + 1]
  
  Case $CmdLine[$i] = "/p"
     $Processname =$CmdLine[$i+1]
  
  Case $CmdLine[$i] = "/w"
     $windowtitle = StringSplit($CmdLine[$i+1],"*")
     
  Case Else
     ErrorCode(2)
    EndSelect
Next

If $Program = "Null" or $Program = " " Then
    ErrorCode(1)
EndIf

If ProcessExists($processname) Then
    If $WindowTitle[0] = 0 Then
  ErrorCode(0)
    EndIf
    
    for $i = 1 to $windowtitle[0]
  If winexists($windowtitle[$i]) Then
     WinShow($windowtitle[$i],"",@SW_RESTORE)
     winActivate($windowtitle[$i])
     ErrorCode(0)
  EndIf
    Next
    ErrorCode(0)
EndIf

Run($Program,$ProgramPath)
Exit

Func ErrorCode($a)
    Select
  Case $a = 0
     MsgBox(0,"Program Running","The program is already running",5)
  
  Case $a = 1
     MsgBox(0,"Need Program","A program needs to be entered.")
  
  Case $a = 2
     MsgBox(0,"Error In Command Line Options",$CmdLine[$i] & " is not a valid command line option." & @CRLF & @CRLF & "Please enter one of the following command-line options:" &@CRLF &"/f     Full file path with filename" &@CRLF &"/d     Alternate working directory" & @CRLF & "/p     Alternate process name to look for other than the executable name" & @CRLF & "/w     Window names the program can have seperated by an *")
  
  Case $a = 3
     MsgBox(0,"Command Line Options","Invalid number of command line options pls check and try again.")
  
  Case $a = 4
     MsgBox(0,"Command Line Options","Please enter one of the following command-line options:" &@CRLF &"/f     Full file path with filename" &@CRLF &"/d     Alternate working directory" & @CRLF & "/p     Alternate process name to look for other than the executable name" & @CRLF & "/w     Window names the program can have seperated by an *")
    EndSelect
    Exit
EndFunc

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0