{"id":334,"date":"2006-11-29T04:00:00","date_gmt":"2006-11-29T12:00:00","guid":{"rendered":"http:\/\/testblog.sapien.com\/index.php\/2006\/11\/29\/make-your-scripts-sing\/"},"modified":"2006-11-29T04:00:00","modified_gmt":"2006-11-29T12:00:00","slug":"make-your-scripts-sing","status":"publish","type":"post","link":"https:\/\/dev.sapien.com\/blog\/2006\/11\/29\/make-your-scripts-sing\/","title":{"rendered":"Make your scripts sing"},"content":{"rendered":"<p><span style=\"font-family: Arial,Helvetica,sans-serif;\">In an earlier <\/span><a style=\"font-family: Arial,Helvetica,sans-serif;\" href=\"http:\/\/sapien.eponym.com\/blog\/_archives\/2006\/11\/21\/2513727.html\">post<\/a><span style=\"font-family: Arial,Helvetica,sans-serif;\">, I showed you how to make your scripts talk back. Well, you can also use the SAPI object to &#8220;speak&#8221; a wav file. Imagine having a script play a sound when it finishes a long query or process.&nbsp; This is pretty easy to do. The limitation is that the object will only play wav files.<\/span><br style=\"font-family: Arial,Helvetica,sans-serif;\"><br style=\"font-family: Arial,Helvetica,sans-serif;\"><span style=\"font-family: Arial,Helvetica,sans-serif;\">What you need to do is create a&nbsp; <\/span><span style=\"font-family: Courier New,Courier,mono;\"><span style=\"font-family: Times New Roman,Times,serif;\"><span style=\"font-family: Arial,Helvetica,sans-serif;\">SAPI.SpFileStream.1 object and then invoke it&#8217;s OPEN method, passing it the parameter of a filename.<\/span><\/p>\n<p><\/span><\/span><span style=\"font-family: Courier New,Courier,mono;\">Set objFile = CreateObject(&#8220;SAPI.SpFileStream.1&#8221;)<br \/>objFile.Open &#8220;C:\\windows\\media\\tada.wav&#8221;<\/p>\n<p><span style=\"font-family: Arial,Helvetica,sans-serif;\">The voice object uses the SpeakStream method to &#8220;speak&#8221; the file:<\/span><br style=\"font-family: Arial,Helvetica,sans-serif;\"><br \/>objSPVoice.Speakstream objFile<\/p>\n<p><span style=\"font-family: Arial,Helvetica,sans-serif;\">It is good practice to close the file after you are done with it.<\/span><br style=\"font-family: Times New Roman,Times,serif;\"><br \/>objFile.Close<\/p>\n<p><span style=\"font-family: Arial,Helvetica,sans-serif;\">That&#8217;s all there is to it.&nbsp; I&#8217;ve put together a sample script that gets your media directory (typically %windir%\\media) and lists all the files that end in .wav.&nbsp; The default voice will speak each filename and then &#8220;speak&#8221; the file so you can hear it.<\/span><br \/><\/span><br \/><span style=\"font-family: Courier New,Courier,mono;\">On Error Resume Next<\/span><br style=\"font-family: Courier New,Courier,mono;\"><span style=\"font-family: Courier New,Courier,mono;\">Dim objShell,objFSO,objFiles<\/span><br style=\"font-family: Courier New,Courier,mono;\"><span style=\"font-family: Courier New,Courier,mono;\">Dim objSPVoice,objFile<\/span><br style=\"font-family: Courier New,Courier,mono;\"><span style=\"font-family: Courier New,Courier,mono;\">&nbsp;<\/span><br style=\"font-family: Courier New,Courier,mono;\"><span style=\"font-family: Courier New,Courier,mono;\">Set objSPVoice=CreateObject(&#8220;SAPI.SpVoice&#8221;)<\/span><br style=\"font-family: Courier New,Courier,mono;\"><span style=\"font-family: Courier New,Courier,mono;\">Set objFile = CreateObject(&#8220;SAPI.SpFileStream.1&#8221;)<\/span><br style=\"font-family: Courier New,Courier,mono;\"><span style=\"font-family: Courier New,Courier,mono;\">Set objShell=CreateObject(&#8220;wscript.shell&#8221;)<\/span><br style=\"font-family: Courier New,Courier,mono;\"><span style=\"font-family: Courier New,Courier,mono;\"><br \/>strWinDir= objShell.ExpandEnvironmentStrings(&#8220;%WinDir%&#8221;)<\/span><br style=\"font-family: Courier New,Courier,mono;\"><span style=\"font-family: Courier New,Courier,mono;\">strMediaDir=strWinDir &amp; &#8220;\\Media&#8221;<\/span><br style=\"font-family: Courier New,Courier,mono;\"><span style=\"font-family: Courier New,Courier,mono;\"><br \/>Set objFSO=CreateObject(&#8220;Scripting.FileSystemObject&#8221;)<\/span><br style=\"font-family: Courier New,Courier,mono;\"><span style=\"font-family: Courier New,Courier,mono;\">Set objFiles=objFSO.GetFolder(strMediaDir).Files<br \/><br style=\"font-family: Courier New,Courier,mono;\"><\/span><span style=\"font-family: Courier New,Courier,mono;\">For Each file In objFiles<\/span><br style=\"font-family: Courier New,Courier,mono;\"><span style=\"font-family: Courier New,Courier,mono;\">&#8216;only get .wav files<\/span><br style=\"font-family: Courier New,Courier,mono;\"><span style=\"font-family: Courier New,Courier,mono;\">&nbsp;&nbsp;&nbsp; If Right(file.name,3)=&#8221;wav&#8221; Then <\/span><br style=\"font-family: Courier New,Courier,mono;\"><span style=\"font-family: Courier New,Courier,mono;\">&nbsp;&nbsp;&nbsp; WScript.Echo file<\/span><br style=\"font-family: Courier New,Courier,mono;\"><span style=\"font-family: Courier New,Courier,mono;\">&nbsp;&nbsp;&nbsp; DemoWav file<\/span><br style=\"font-family: Courier New,Courier,mono;\"><span style=\"font-family: Courier New,Courier,mono;\">&nbsp;&nbsp;&nbsp; End If<\/span><br style=\"font-family: Courier New,Courier,mono;\"><span style=\"font-family: Courier New,Courier,mono;\">Next<\/span><br style=\"font-family: Courier New,Courier,mono;\"><br style=\"font-family: Courier New,Courier,mono;\"><span style=\"font-family: Courier New,Courier,mono;\">Sub DemoWav(File)<\/span><br style=\"font-family: Courier New,Courier,mono;\"><span style=\"font-family: Courier New,Courier,mono;\">On Error Resume Next<\/span><br style=\"font-family: Courier New,Courier,mono;\"><span style=\"font-family: Courier New,Courier,mono;\">&nbsp;&nbsp;&nbsp; Err.Clear<\/span><br style=\"font-family: Courier New,Courier,mono;\"><span style=\"font-family: Courier New,Courier,mono;\">&nbsp;&nbsp;&nbsp; objFile.Open file<\/span><br style=\"font-family: Courier New,Courier,mono;\"><span style=\"font-family: Courier New,Courier,mono;\">&nbsp;&nbsp;&nbsp; If Err.Number&lt;&gt;0 Then Exit Sub &#8216;bail if there is a problem<\/span><br style=\"font-family: Courier New,Courier,mono;\"><span style=\"font-family: Courier New,Courier,mono;\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &#8216;opening the file<\/span><br style=\"font-family: Courier New,Courier,mono;\"><span style=\"font-family: Courier New,Courier,mono;\">&nbsp;&nbsp;&nbsp; objSPVoice.Speak file.name<\/span><br style=\"font-family: Courier New,Courier,mono;\"><span style=\"font-family: Courier New,Courier,mono;\">&nbsp;&nbsp;&nbsp; objSPVoice.SpeakStream objFile<\/span><br style=\"font-family: Courier New,Courier,mono;\"><span style=\"font-family: Courier New,Courier,mono;\">&nbsp;&nbsp;&nbsp; objFile.Close<\/span><br style=\"font-family: Courier New,Courier,mono;\"><span style=\"font-family: Courier New,Courier,mono;\">End Sub<\/span><\/p>\n<p><span style=\"font-family: Arial,Helvetica,sans-serif;\">The DemoWav subroutine attempts to opens each file.&nbsp; I added some error handling as on my system some of the wav files caused an error when they were opened, yet they play just fine in Windows Media Player. I just skip those since this is a demo anyway.&nbsp; <\/span><br style=\"font-family: Arial,Helvetica,sans-serif;\"><br style=\"font-family: Arial,Helvetica,sans-serif;\"><span style=\"font-family: Arial,Helvetica,sans-serif;\">Now your scripts can talk back and sing to you.<\/p>\n<p>(The Microsoft Scripting Guys have a nice article on this topic at <a href=\"http:\/\/www.microsoft.com\/technet\/scriptcenter\/funzone\/games\/sapi.mspx\">http:\/\/www.microsoft.com\/technet\/scriptcenter\/funzone\/games\/sapi.mspx<\/a>)<br \/><\/span><\/p>\n","protected":false},"excerpt":{"rendered":"<p>In an earlier post, I showed you how to make your scripts talk back. Well, you can also use the SAPI object to &#8220;speak&#8221; a wav file. Imagine having a script play a sound when it finishes a long query or process.  This is pretty easy to do.<\/p>\n","protected":false},"author":3,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[2,24],"tags":[],"class_list":["post-334","post","type-post","status-publish","format-standard","hentry","category-general","category-vbscript"],"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/posts\/334","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/comments?post=334"}],"version-history":[{"count":0,"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/posts\/334\/revisions"}],"wp:attachment":[{"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/media?parent=334"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/categories?post=334"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/tags?post=334"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}