Created
December 2, 2011 08:18
-
-
Save hidsh/1422309 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
' usage: start-app \path\to\foo.exe | |
' | |
Set args = WScript.Arguments | |
directory = get_directory(args(0)) | |
exe_name = get_filename(args(0)) | |
params = "" | |
' | |
' main | |
' | |
Set wsh_shell = WScript.CreateObject("WScript.Shell") | |
pid = get_pid(exe_name) ' whether the app is already running or Not | |
If pid <>0 Then ' running --> activate it | |
wsh_shell.AppActivate(pid) | |
else ' not --> launch it | |
cwd = wsh_shell.CurrentDirectory | |
If directory <> "" then wsh_shell.CurrentDirectory = directory ' change working directory | |
wsh_shell.Run exe_name & params | |
wsh_shell.CurrentDirectory = cwd | |
End If | |
' | |
' sub functions | |
' | |
Function get_filename(path) | |
pos = InStrRev(path, "\") + 1 | |
get_filename = mid(path, pos) | |
End Function | |
Function get_directory(path) | |
pos = InStrRev(path, "\") | |
get_directory = left(path, pos) | |
End Function | |
Function get_pid(proc_name) | |
ret = 0 | |
Set objProcessList = GetObject("winmgmts:").InstancesOf("win32_process") | |
For Each proc In objProcessList | |
If StrComp(LCase(proc.Name), proc_name, vbTextCompare) = ZERO Then | |
If Err.Number = ZERO Then | |
ret = proc.ProcessID | |
Exit For | |
Else | |
WScript.Echo Err.Description | |
End If | |
End If | |
Next | |
get_pid = ret | |
End Function |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment