;===========================================================================
; Run a program or switch to it if already running.
;     Target - Program to run. E.g. Calc.exe or C:\Progs\Bobo.exe
;     WinTitle - Optional title of the window to activate. Programs like
;        MS Outlook might have multiple windows open (main window and email
;        windows). This parm allows activating a specific window.
;
;Original Source comes from forum on http://www.autohotkey.com
;Optimized by alongside (http://hi.baidu.com/alongside) @ 2007-04-26
; ===========================================================================
RunOrActivate(Target, WinTitle = "")
{
   ; Get the filename without a path
   SplitPath, Target, TargetNameOnly

   Process, Exist, %TargetNameOnly%
   If ErrorLevel > 0
      PID = %ErrorLevel%
   Else
      Run, %Target%, , , PID


   If WinTitle <>
   {
      SetTitleMatchMode, 2
      WinWait, %WinTitle%, , 3
      WinActivate, %WinTitle%
   }
   Else
   {
      WinWait, ahk_pid %PID%, , 3
      WinActivate, ahk_pid %PID%
   }

}


Return




; Example uses...

#`::RunOrActivate("taskmgr")          ;Run Task


;~ #1::RunOrActivate("D:\download")     ;Mend to your own folders
#1::RunOrActivate("D:\abc")     ;Mend to your own folders


#f::RunOrActivate("D:\Program Files\Mozilla Firefox\firefox.exe")    ;Mend to your own programs
#y::RunOrActivate("wmplayer.exe")     ;Run WindowsMediaplayer


#-::RunOrActivate("mspaint.exe")
#=::RunOrActivate("calc.exe")