foxidrive & squashman: my experience level is such that I can't understand where to go with those hints.
All of the help from this thread and other recent posts from Aacini on hybrid scripts has helped me to
get it running with the batch file below. The goal of this project was to have the test-code
do what its supposed to do without error on my Win 7 laptop, Win 7 desktop, and the same
desktop booted in XP: activate the DOS window after starting a media player. I've explained in
a previous post that this method of activating a DOS window will be implemented in a larger batch project.
The code, before the latest revision, ran as expected on the laptop.
I found that when I tested on the desktop PC in Win 7, a blinking icon for the DOS window appeared
on the taskbar about once every 4 iterations. Then I would need to click on the DOS window.
Occasionally the media player kept the focus. I added closing media player before opening it, and
adding a 2nd AppActivate statement in the script, plus the "on error..." statements before and after,
and then the DOS window was activated every time.
In this revision, I have added a second job for the vbScript, closing the media player.
Please add your comments if there are any parts of this code that can be corrected where I will gain
instruction from the corrections.
Code: Select all
<!-- : Begin batch script
@echo off
rem hybrid vbScript code demonstrates opening media player from batch and
rem then activating the DOS cmd window.
rem this batch file uses these 3 files: sample1.mp3, sample2.mp3, wmplayer.exe
Set "mPlayer=wmplayer.exe"
set "title=This is an unique title"
title %title%
:top
Set "rsp="
Set "mFile="
Echo(&Echo Enter 1 or 2 to play music file #1 or #2
Set /p "rsp=or 's' to stop player, or just press Enter to quit. "
If "%rsp%"=="" Exit /b
Set "rsp=%rsp:~0,1%"
If "%rsp%"=="1" Set "mFile=sample1.mp3"
If "%rsp%"=="2" Set "mFile=sample2.mp3"
If /I "%rsp%"=="S" (
cscript //nologo "%~f0?.wsf" "%mPlayer%" "%title%" 1
) Else If Not "%mFile%"=="" (
If Not EXIST "%mFile%" (
Echo %mFile% was not found.
Echo Copy two mp3 music files to this folder and name them
Echo sample1.mp3 and sample2.mp3, then run this batch file again.
exit /b
) Else (
cscript //nologo "%~f0?.wsf" "%mPlayer%" "%title%" 1
start %mPlayer% /play %~dp0\%mFile%
cscript //nologo "%~f0?.wsf" "%mPlayer%" "%title%" 0
)
) Else (
Echo(&Echo That was not one of the choices. Try again.&GoTo:top
)
GoTo:top
----- Begin wsf script --->
<job><script language="VBScript">
Set args = Wscript.Arguments
Dim objWMIService, objProcess, colProcess
Dim strComputer
Dim strMediaPlayer
Dim numArgs, found, c
numArgs = args.Count
If numArgs = 0 Then WScript.Quit
strMediaPlayer = Args(0)
strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" _
& strComputer & "\root\cimv2")
Set colProcess = objWMIService.ExecQuery _
("Select * from Win32_Process")
found = 0
For c = 1 To 5
If found > 0 Then Exit For
For Each objProcess in colProcess
If objProcess.Name = strMediaPlayer Then
If args(2) = 1 Then
On Error Resume Next
objProcess.Terminate()
On Error GoTo 0
found = 2
WScript.Sleep 100
Exit For
Else
found = 1
Exit For
End If
End If
Next
If args(2) <> 1 Then WScript.Sleep 250
Next
If found = 1 Then
Set WshShell = WScript.CreateObject("WScript.Shell")
WScript.Sleep 100
On Error Resume Next
WshShell.AppActivate (Args(1))
WshShell.AppActivate (Args(1))
On Error GoTo 0
End If
</script></job>