KiXtart has the built-in ability to play a WAV or SPK file using the Play command. To play an audio file using KiXtart, proceed as follows :
Create a new directory to store all files included in this example.
Download and extract the latest version of KiXtart, from http://www.kixtart.org, to the new directory.
Select StartRun and enter "kix32 scriptfile. "
Here, scriptfile is the full path of the new directory from step 1 and file name of a script file that contains the following:
$Aud - "filename" Play File $Aud
Here, filename is the full path and file name of the WAV or SPK file to play.
Windows 2000/XP/2003 includes a free application called Media Player, designed to play audio and video files. Mplay32.exe is the 32-bit version of the standard Media Player, and this utility can play audio, video, and DirectShow files. This utility supports a limited amount of command-line switches.
Microsoft Media Player 9 is a Windows add-on that provides extremely enhanced functionality when compared to the older Windows multimedia players. Some of these features include media rights, MP3 (Motion Pictures Expert Group Layer 3 Audio) support, video streaming, radio tuners, and play list support. This player is intended to be the core Windows multimedia player and manager while replacing the older, built-in multimedia players, such as CDPlayer.exe and Mplay32.exe. This utility has limited support for Windows Script Host.
To play and then close a media file using Mplay32.exe and shell scripting, proceed as follows:
Create a new directory to store all files included in this example.
Select StartRun and enter " scriptfile. bat."
Here, scriptfile is the full path and file name of a script file that contains the following:
@Echo Off MPLAY32 /PLAY /CLOSE "filename"
Here, filename is the full path and file name to play.
To play and then close a media file using Mplay32.exe and Windows Script Host, proceed as follows:
Create a new directory to store all files included in this example.
Download and install the latest version of Windows Script Host, from http://www.microsoft.com, to the new directory.
Select StartRun and enter "cscript scriptfile. vbs."
Here, scriptfile is the full path and file name of a script file that contains the following:
On Error Resume Next Set SHELL - CreateObject("wscript.shell") SHELL. Run "MPLAY32 /PLAY /CLOSE filename ". 0
Here, filename is the full path and file name to play. The value within the Run command causes the media player to be hidden.
Many new audio players (for example, winamp ) utilize play lists to play one audio file after another. To play multiple media files using a play list, Mplay32.exe, and Windows Script Host, proceed as follows:
Create a new directory to store all files included in this example.
Download and install the latest version of Windows Script Host, from http://www.microsoft.com, to the new directory.
Select StartRun and enter "cscript scriptfile. vbs."
Here, scriptfile is the full path and file name of a script file that contains the following:
On Error Resume Next PlayList ( "playlist" ) SUB PlayList ( TXTfile ) Set SHELL - CreateObject ("wscript.shell") Set FSO - CreateObject ("Scripting.FileSystemObject") Set readfile - FSO. OpenTextFile( TXTfile, 1, false) Do while readfile. AtEndOfStream <> true contents - Trim(readfile.Readline) If contents <> "" Then SHELL. RUN "MPLAY32 /PLAY /CLOSE " & contents, 3, True End If Loop End Sub
Here, playlist is the full path and file name of a playlist file. Each line of this file contains the full path and file name of an audio file to play.
Tip | The value 3 within the SHELL.RUN command specifies to maximize the player. You can change this value to if you would like the player hidden. |
Microsoft Media Player 9 does not currently support access through Windows Script Host. You can, however, use the Media Player 9 object model to display information and control the CD player. To eject a CD using the Media Player 9 object model and Windows Script Host, proceed as follows:
Create a new directory to store all files included in this example.
Download and install Microsoft Media Player 9 and the latest version of Windows Script Host, from http://www.microsoft.com, to the new directory.
Select StartRun and enter "cscript scriptfile. vbs."
Here, scriptfile is the full path and file name of a script file that contains the following:
On Error Resume Next Set MPlayer - CreateObject("WMPlayer.OCX") MPlayer. cdromCollection.item(x).eject()
Here, x is the number of the CD-ROM drive (starting at 0).
To eject all CDs using the Media Player 9 object model and Windows Script Host, proceed as follows:
Create a new directory to store all files included in this example.
Download and install Microsoft Media Player 9 and the latest version of Windows Script Host, from http://www.microsoft.com, to the new directory.
Select StartRun and enter "cscript scriptfile. vbs."
Here, scriptfile is the full path and file name of a script file that contains the following:
On Error Resume Next Set MPlayer - CreateObject("WMPlayer.OCX") Set FSO - CreateObject("Scripting.FileSystemObject") Count - 1 For Each Drive in FSO. Drives If Drive. DriveType - 4 Then Count-Count +1 End If Next If Count > -1 Then For x - 0 to Count MPlayer. cdromCollection.item( x ).eject() Next End If
Here, a DriveType value of 4 indicates a CD-ROM player.
RealOne is an advanced multimedia player from RealNetworks (http://www.real.com). Although this player is commonly used to play streaming media on the Internet, you can use these same ActiveX control calls to script RealPlayer using Windows Script Host.
To play an audio file using the RealPlayer ActiveX control and Windows Script Host, proceed as follows:
Create a new directory to store all files included in this example.
Download and install the latest version of Windows Script Host, from http://www.microsoft.com, to the new directory.
Download and install the latest version of RealOne, from http://www.real.com, to the new directory.
Select StartRun and enter "cscript scriptfile. vbs."
Here, scriptfile is the full path and file name of a script file that contains the following:
On Error Resume Next Set RPlayer - CreateObject ("rmocx.RealPlayer G2 Control.1") RPlayer. SetSource "file: filename " RPlayer. DoPlay Wscript.Echo "Press OK to end."
Here, filename is the full path and file name to play.
To play an audio file with basic controls using the RealPlayer ActiveX control and Windows Script Host, proceed as follows:
Create a new directory to store all files included in this example.
Download and install the latest version of Windows Script Host, from http://www.microsoft.com, to the new directory.
Download and install the latest version of RealOne, from http://www.real.com, to the new directory.
Select StartRun and enter "cscript scriptfile. vbs."
Here, scriptfile is the full path and file name of a script file that contains the following:
On Error Resume Next Set RPlayer - CreateObject("rmocx.RealPlayer G2 Control.1") CMD - 2 Do While CMD <> 10 Select Case CMD Case 0 RPlayer. DoPlay Case 1 RPlayer. DoPause Case 2 If AUD - "" Then AUD - "filename" AUD - InputBox("Please enter the name of the audio file to play", "Audio File", AUD ) RPlayer. SetSource "file:" & AUD Case 3 WScript.Quit End Select Message - "Choose a command:" & vblf & vblf & _ "0: Play file" & vblf & _ "1: Pause file" & vblf & _ "2: Choose file" & vblf & _ "3: Quit" & vblf CMD - InputBox(Message, "RealPlayer Commands", "0") Loop
Note | The highlighted code above must be entered as one paragraph. |
Here, filename is the full path and file name to play.
Many new audio players (for example, winamp) utilize play lists to play one audio file after another. To play multiple media files using a play list, the RealPlayer ActiveX control, and Windows Script Host, proceed as follows:
Create a new directory to store all files included in this example.
Download and install the latest version of Windows Script Host, from http://www.microsoft.com, to the new directory.
Download and install the latest version of RealOne, from http://www.real.com, to the new directory.
Select StartRun and enter "cscript scriptfile. vbs."
Here, scriptfile is the full path and file name of a script file that contains the following:
On Error Resume Next Set RPlayer - CreateObject("rmocx.RealPlayer G2 Control.1") Set FSO - CreateObject("Scripting.FileSystemObject") Set readfile - FSO .OpenTextFile( TXTfile , 1, false) PlayList (" playlist ") Wscript.Echo "Press OK to end." SUB PlayList( TXTfile ) Do while readfile .AtEndOfStream <> true filename - Trim(readfile.Readline) If filename <> "" Then RPlayer .SetSource "file: filename " RPlayer .DoPlay End If Loop End Sub
Here, filename is the full path and file name to play.
The Office Assistant is an interactive animated character used to help and entertain users of Microsoft Office. You can only access the assistant object model through an Office application object model. This means that you must have an Office application installed in order to automate an office assistant. To script the Office Assistant in Excel using Windows Script Host, proceed as follows:
Create a new directory to store all files included in this example.
Install the latest version of Microsoft Excel.
Download and install the latest version of Windows Script Host, from http://www.microsoft.com, to the new directory.
Select Start/Run and enter "cscript scriptfile .vbs."
Here, scriptfile is the full path and file name of a script file that contains the following:
On Error Resume Next Set FSO - CreateObject("Scripting.FileSystemObject") Set objXL - CreateObject("Excel.Application") objXL .Workbooks.Add objXL .Visible - False objXL .Assistant.Visible - True With objXL .Assistant .Reduced - True .Left - 300 .Top - 300 .MoveWhenInTheWay - True End With Set Balloon - objXL .Assistant.NewBalloon Balloon .Heading - "Multiple Selections" Balloon .Text - "Please make a selection" Balloon .CheckBoxes(1).Text - "Selection 1" Balloon .CheckBoxes(2).Text - "Selection 2" Balloon .Show If Balloon .CheckBoxes(1).Checked Then Wscript.Echo "You selected check box 1." End If If Balloon .CheckBoxes(2).Checked Then Wscript.Echo "You selected check box 2." End If objXL .quit
Related solution: | Found on page: |
---|---|
Automating Applications through an Application Object | 100 |
Microsoft Agent is an ActiveX technology that allows you to use animated characters to present information to your users. This technology can be used in presentations, logon scripts, new user setups, and any other situation where an interaction is needed.
Many developers use Microsoft Agent to entertain, educate, or guide their users through a process. To script a Microsoft Agent character to speak using Windows Script Host, proceed as follows:
Create a new directory to store all files included in this example.
Download and install the latest version of Microsoft Agent, a text-to-speech engine, a Microsoft Agent character, and Windows Script Host, from http://www.microsoft.com, to the new directory.
Select Start/Run and enter "cscript scriptfile .vbs."
Here, scriptfile is the full path and file name of a script file that contains the following:
On Error Resume Next Set SHELL - CreateObject("wscript.shell") Set FSO - CreateObject("Scripting.FileSystemObject") aCHAR - "charname" Set ACTL - CreateObject("Agent.Control.2") ACTL .Connected - True If Not IsObject( ACTL ) Then Wscript.Echo "Microsoft Agent was not found on your "& _ "system." & vblf & "Please install and try again." Wscript.Quit End If ACTL .Connected - True ACTL .Characters.Load aCHAR, aCHAR & ".acs" If Err.Number <> 0 Then Wscript.Echo "Could not locate the Agent called" & aCHAR Wscript.Quit End If Set CREF - ACTL .Characters( aCHAR ) CREF .Show CREF .Speak "Hello there!" WScript.Echo "Press OK to close"
Here, charname is the name of the agent character to use.
Microsoft Agent has the ability to accept a WAV (WAVeform Audio) file and appear to speak it based on the gaps of silence detected . This allows you to use a real voice, as opposed to a synthesized voice, to speak to your users. To use Microsoft Agent to speak a WAV file, proceed as follows:
Create a new directory to store all files included in this example.
Download and install the latest version of Microsoft Agent, a text-to-speech engine, a Microsoft Agent character, and Windows Script Host, from http://www.microsoft.com, to the new directory.
Select Start/Run and enter "cscript scriptfile .vbs."
Here, scriptfile is the full path and file name of a script file that contains the following:
On Error Resume Next Set SHELL - CreateObject("wscript.shell") Set FSO - CreateObject("Scripting.FileSystemObject") aCHAR - "charname" Set ACTL - CreateObject("Agent.Control.2") ACTL .Connected - True If Not IsObject( ACTL ) Then Wscript.Echo "Microsoft Agent was not found on your" & _ "system" & vblf & "Please install and try again." Wscript.Quit End If ACTL .Connected - True ACTL .Characters. Load aCHAR, aCHAR & ".acs" If Err.Number <> 0 Then Wscript.Echo "Could not locate the Agent called" & aCHAR Wscript.Quit End If Set CREF - ACTL .Characters( aCHAR ) CREF .Show CREF .Speak "", " WAVFile " WScript.Echo "Press OK to close"
Here, charname is the name of the agent character to use, and WAVFile is the full path and file name of the WAV file to use.
You can make the Microsoft Agent appear to sing by modifying the pitch and speed of the agent's voice. To make a Microsoft Agent character sing the Imperial March from Star Wars, proceed as follows:
Create a new directory to store all files included in this example.
Download and install the latest version of Microsoft Agent, a text-to-speech engine, a Microsoft Agent character, and Windows Script Host, from http://www.microsoft.com, to the new directory.
Select Start/Run and enter "cscript scriptfile .vbs."
Here, scriptfile is the full path and file name of a script file that contains the following:
On Error Resume Next Set SHELL - CreateObject("wscript.shell") Set FSO - CreateObject("Scripting.FileSystemObject") aCHAR - " charname " Set ACTL - CreateObject("Agent.Control.2") ACTL .Connected - True If Not IsObject( ACTL ) Then Wscript.Echo "Microsoft Agent was not found on your" & _ "system." & vblf & "Please install and try again." Wscript.Quit End If ACTL .Connected - True ACTL .Characters. Load aCHAR, aCHAR & ".acs" If Err.Number <> 0 Then Wscript.Echo "Could not locate the Agent called" & aCHAR Wscript.Quit End If Set CREF - ACTL .Characters( aCHAR ) CREF .Show CREF .Speak "\Chr-""Monotone""\Map-""\Pit-98\Spd-50\DUN DUN \Spd-134\DUN \Spd-50\DUN \Pit-78\DUN \Pit-117\Spd-200\DUN \Pit-98\Spd-50\DUN \Pit-78\DUN \Pit-117\Spd-150\DUN \Pit-98\Spd-50\DUN" "-" "" "\" CREF .Speak "\Chr-""Monotone""\Map-""\Pit-147\Spd-50\DUN DUN DUN \Pit-156\Spd-67\DUN \Pit-117\Spd-134\DUN \Pit-92\Spd-67\DUN \Pit-78\Spd-80\DUN \Pit-117 \Spd-77\DUN \Pit-98\Spd-67\DUN""-""""\" Wscript.Echo "Press OK to end the show"
Note | The highlighted code above must be placed on one line. |
Here, charname is the name of the agent character to use.
You can make the Microsoft Agent speak any text that you can interpret in Windows Script Host. To make a Microsoft Agent character read a text file using Windows Script Host, proceed as follows:
Create a new directory to store all files included in this example.
Download and install the latest version of Microsoft Agent, a text-to-speech engine, a Microsoft Agent character, and Windows Script Host, from http://www.microsoft.com, to the new directory.
Select Start/Run and enter "cscript scriptfile .vbs."
Here, scriptfile is the full path and file name of a script file that contains the following:
On Error Resume Next Set SHELL - CreateObject("wscript.shell") Set FSO - CreateObject("Scripting.FileSystemObject") aCHAR - " charname " Set ACTL - CreateObject("Agent.Control.2") ACTL .Connected - True If Not IsObject( ACTL ) Then Wscript.Echo "Microsoft Agent was not found on your" & _ "system." & vblf & "Please install and try again." Wscript.Quit End If ACTL .Connected - True ACTL .Characters. Load aCHAR, aCHAR & ".acs" If Err.Number <> 0 Then Wscript.Echo "Could not locate the Agent called" & aCHAR Wscript.Quit End If Set CREF - ACTL .Characters( aCHAR ) CREF .Show ReadTXT (" textfile ") WScript.Echo "Press OK to close" SUB ReadTXT( TXTfile ) Set FSO - CreateObject("Scripting.FileSystemObject") Set readfile - FSO .OpenTextFile( TXTfile , 1, false) Do while readfile .AtEndOfStream <> true contents - readfile .Readline If contents <> "" THEN CREF .Speak contents End IF Loop contents - NULL readfile .close End Sub
Here, charname is the name of the agent character to use, and textfile is the full path and file name of the text file to read.
In Chapter 8, you learned how to check for events using Windows Management Instrumentation. To make a Microsoft Agent character notify you of events using WMI and Windows Script Host, proceed as follows:
Create a new directory to store all files included in this example.
Download and install the latest version of Microsoft Agent, a text-to-speech engine, the Merlin Microsoft Agent character, and Windows Script Host, from http://www.microsoft.com, to the new directory.
Select Start/Run and enter "cscript scriptfile .vbs."
Here, scriptfile is the full path and file name of a script file that contains the following:
On Error Resume Next Set SHELL - CreateObject("wscript.shell") Set FSO - CreateObject("Scripting.FileSystemObject") aCHAR - "Merlin" Set ACTL - CreateObject("Agent.Control.2") ACTL .Connected - True If Not IsObject( ACTL ) Then Wscript.Echo "Microsoft Agent was not found on your" & _ "system." & vblf & "Please install and try again." Wscript.Quit End If ACTL .Connected - True ACTL .Characters. Load aCHAR, aCHAR & ".acs" If Err.Number <> 0 Then Wscript.Echo "Could not locate the Agent called" & aCHAR Wscript.Quit End If Wscript.Echo "Press CTRL+C to end this script." Set CREF - ACTL .Characters( aCHAR ) CREF .MoveTo 200,200 CREF .Show CREF .Play "Wave" CREF .Play "Restpose" CREF .Speak "Hello, my name is Merlin!" CREF .Play "Greet" CREF .Play "Restpose" CREF .Speak "I am your personal CPU monitoring assistant!" CREF .Play "Announce" CREF .Play "Restpose" CREF .MoveTo 0.0 CREF .Speak "I will now monitor your CPU usage and notify" & _ "you when an overload occurs." CREF .Play "StartListening" Computer - InputBox("Enter the computer name", "CPU Monitor", "localhost") CPULoad - InputBox("Enter the CPU overload threshhold", "CPU threshhold", "75") Poll - InputBox("Enter the polling interval", "Poll Interval", "5") If Computer - "" Then Computer - "Localhost" If CPULoad - "" Then CPULoad - 75 If Poll - "" Then Poll - 5 Set ProLoad - GetObject("winmgmts:{impersonationLevel- impersonate}!\" & Computer & "\root\cimv2") .ExecNotificationQuery("SELECT * FROM ___InstanceModificationEvent WITHIN " & Poll & " WHERE TargetInstance ISA `Win32_Processor' and TargetInstance .LoadPercentage > " & CPULoad ) If Err.Number <> 0 then WScript.Echo Err.Description, Err.Number, Err.Source End If Do Set ILoad - ProLoad .nextevent If Err.Number <> 0 then WScript.Echo Err.Number, Err.Description, Err.Source Exit Do Else AMessage - ILoad .TargetInstance.DeviceID & _ " is overloaded at " & _ ILoad .TargetInstance.LoadPercentage & "%!" CREF .Stop CREF .Show CREF .Play "GetAttention" CREF .Play "GetAttentionContinued" CREF .Play "GetAttentionReturn" CREF.Speak AMessage RandomAction End If Loop Sub RandomAction() ulimit - 5.0 llimit - 1.0 Randomize X - Int(( ulimit - llimit )*Rnd() + llimit ) Select Case X Case 1 CREF .Play "Acknowledge" Case 2 CREF .Play "Alert" Case 3 CREF .Play "Explain" Case 4 CREF .Play "Sad" Case 5 CREF .Play "Uncertain" End Select End Sub
Note | The highlighted code above must be placed on one line. |
Here, computer is the name of the system to monitor; CPULoad is the CPU utilization threshold to monitor for (1 “100); and poll is the number of seconds to set as the polling interval to check for events. The subprocedure RandomAction creates a random number and then specifies an animation based on that number.
Warning | If you run this script with WSCRIPT, you will only be able to terminate the script by ending the WSCRIPT.EXE process through the Task Manager. |
Related solution: | Found on page: |
---|---|
Monitoring CPU Utilization | 192 |