{"id":56293,"date":"2008-02-01T22:35:00","date_gmt":"2008-02-01T22:35:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2008\/02\/01\/hey-scripting-guy-how-can-i-randomly-assign-a-sound-to-a-sound-event\/"},"modified":"2008-02-01T22:35:00","modified_gmt":"2008-02-01T22:35:00","slug":"hey-scripting-guy-how-can-i-randomly-assign-a-sound-to-a-sound-event","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/hey-scripting-guy-how-can-i-randomly-assign-a-sound-to-a-sound-event\/","title":{"rendered":"Hey, Scripting Guy! How Can I Randomly Assign a Sound to a Sound Event?"},"content":{"rendered":"<p><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/q-for-powertip.jpg\" border=\"0\" align=\"left\" width=\"34\" alt=\"Hey, Scripting Guy! Question\" height=\"34\" title=\"Hey, Scripting Guy! Question\" class=\"nearGraphic\" \/><\/p>\n<p>Hey, Scripting Guy! Is there an easy way to randomize the selection of .wav files at the startup and shutdown of windows?<\/p>\n<p>&#8212; DW<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/scripting\/wp-content\/uploads\/sites\/29\/2019\/05\/spacer.gif\" border=\"0\" width=\"5\" alt=\"Spacer\" height=\"5\" \/><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/a-for-powertip.jpg\" border=\"0\" align=\"left\" width=\"34\" alt=\"Hey, Scripting Guy! Answer\" height=\"34\" title=\"Hey, Scripting Guy! Answer\" class=\"nearGraphic\" \/><a href=\"http:\/\/go.microsoft.com\/fwlink\/?linkid=68779&amp;clcid=0x409\"><img decoding=\"async\" src=\"http:\/\/img.microsoft.com\/library\/media\/1033\/technet\/images\/scriptcenter\/ad.jpg\" border=\"0\" align=\"right\" width=\"120\" alt=\"Script Center\" height=\"288\" title=\"Script Center\" class=\"farGraphic\" \/><\/a><\/p>\n<p>Hey, DW. Last night as the Scripting Guy who writes this column was on his way home he stopped at a red light. (Which is yet another thing that separates him from most Seattle-area suburbanites: very few of them bother to stop at red lights these days.) As he sat there waiting for the light to turn green, he glanced around and noticed a reader board posted in front of a local restaurant:<\/p>\n<p>CHICKEN FRIED STEAK<br \/>HERB CRUSTED TROUT<br \/>PARMESAN CARMEL LATTE<\/p>\n<p>Admittedly, the CHICKEN FRIED STEAK sounded awfully tempting. The HERB CRUSTED TROUT? Well, the Scripting Guy who writes this column doesn&rsquo;t like seafood; in fact, he refuses to eat anything that swims. Still, he could see how seafood lovers might be attracted to HERB CRUSTED TROUT.<\/p>\n<p>That just left one of the day&rsquo;s specials: PARMESAN CARMEL LATTE. Notice anything weird about that? That&rsquo;s absolutely right; carmel should actually be spelled &ldquo;caramel!&rdquo;<\/p>\n<p>Oh. And then there&rsquo;s that whole Parmesan cheese thing.<\/p>\n<p>Admittedly, the Scripting Guy who writes this column has never tried a PARMESAN CARMEL LATTE; for that matter, he&rsquo;s pretty sure he&rsquo;s never tried <i>any<\/i> kind of cheese-flavored latte. And he&rsquo;s well aware that people have no problem drinking odd-flavored lattes; root beer and grape are two of the more unusual ones that the Scripting Guy who writes this column is personally aware of.<\/p>\n<table cellpadding=\"0\" cellspacing=\"0\" id=\"EJD\" class=\"dataTable\">\n<thead><\/thead>\n<tbody>\n<tr valign=\"top\" class=\"record\">\n<td>\n<p><strong>Note<\/strong>. This has nothing to do with anything, but did you know that they now make coffee-flavored <i>vodka<\/i>? It&rsquo;s true: Double Espresso Double Caffeine Vodka. One shot of this stuff supposedly has as much caffeine as three shots of espresso, and is designed to give people that &ldquo;extra kick they need to keep going in the evening.&rdquo; (It&rsquo;s no fun when people conk out before they&rsquo;ve had a chance to order a few more vodkas.) And have we mentioned Dr. Brown&rsquo;s Cel-Ray Soda, which (we assume) is the world&rsquo;s only soda pop made from (and tasting of) celery? Well, now we have.<\/p>\n<p>And people think the <i>Scripting Guys<\/i> have too much time on their hands.<\/p>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<div class=\"dataTableBottomMargin\"><\/div>\n<p>Anyway, where were we? Oh, right: the PARMESAN CARMEL LATTE. Like we said, we haven&rsquo;t actually tried this, and until you try something you have no right to pass judgment on it.<\/p>\n<p>Except, of course, in this case. A Parmesan-flavored latte? Thanks, but no thanks.<\/p>\n<p>What&rsquo;s that? OK, sure, it&rsquo;s <i>possible<\/i> that the restaurant simply made a mistake. For example, maybe the reader board was <i>supposed<\/i> to look like this instead:<\/p>\n<p>CHICKEN FRIED STEAK<br \/>PARMESAN HERB CRUSTED TROUT<br \/>CARMEL LATTE<\/p>\n<p>But that wouldn&rsquo;t have made for a very entertaining column, would it? And <i>caramel<\/i> would still be spelled wrong.<\/p>\n<p>Speaking of columns, we should probably take a minute or two to address DW&rsquo;s question, shouldn&rsquo;t we? As it turns out, system sound information is stored in the registry; to be a little more precise, system sound information is stored in the HKEY_CURRENT_USER\\AppEvents\\Schemes\\Apps\\.Default\\Maximize\\.Current portion of the registry. Each of the system sound events found in the Windows Control Pane (Default Beep; Device Connect; Device Disconnect; etc.) have their own little place in this registry key:<\/p>\n<p><img decoding=\"async\" src=\"http:\/\/img.microsoft.com\/library\/media\/1033\/technet\/images\/scriptcenter\/qanda\/soundinfo.jpg\" border=\"0\" width=\"405\" alt=\"Spacer\" height=\"353\" \/><\/p>\n<p>So how can we randomly-assign a sound to one of these events? Let&rsquo;s start by showing you a script that assigns a specific sound to a specific event. After we explain how <i>that<\/i> code works then we&rsquo;ll see what we can do about adding in the random-assignment stuff.<\/p>\n<p>Here&rsquo;s a simple little script that assigns the sound file C:\\Windows\\Media\\Ding.wav to the Maximize sound event (in other words, Ding.wav will be played each time you maximize a window):<\/p>\n<pre class=\"codeSample\">Const HKEY_CURRENT_USER = &amp;H80000001strComputer = \".\" Set objRegistry = GetObject(\"winmgmts:\\\\\" &amp; strComputer &amp; \"\\root\\default:StdRegProv\") strKeyPath = \"AppEvents\\Schemes\\Apps\\.Default\\Maximize\\.Current\"strValueName = \"\"strValue = \"C:\\Windows\\Media\\Ding.wav\" objRegistry.SetExpandedStringValue _    HKEY_CURRENT_USER,strKeyPath,strValueName,strValue<\/pre>\n<p>As you can see, we start off by defining a constant named HKEY_CURRENT_USER and setting the value to &amp;H80000001; we&rsquo;ll use this constant to tell the script which registry key we want to work with. After we define the constant, we connect to the WMI service on the local computer.<\/p>\n<table cellpadding=\"0\" cellspacing=\"0\" id=\"E5E\" class=\"dataTable\">\n<thead><\/thead>\n<tbody>\n<tr valign=\"top\" class=\"record\">\n<td>\n<p class=\"lastInCell\"><strong>Note<\/strong>. Could we use this script to change the system sounds on a remote machine? Sure; just assign the name of that remote machine to the variable strComputer.<\/p>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<div class=\"dataTableBottomMargin\"><\/div>\n<p>Next we assign values to three variables:<\/p>\n<ul>\n<li><b><span>strKeyPath<\/span><\/b><span>&nbsp;is the path to the registry key we want to work with. In\nthis case, that&rsquo;s AppEvents\\Schemes\\Apps\\.Default\\Maximize\\.Current. If we\nwanted to change the sound used when Windows shuts down we&rsquo;d use this path:\nAppEvents\\Schemes\\Apps\\.Default\\<b>SystemExit<\/b>\\.Current. To change the sound\nused when Windows first starts up we&rsquo;d use this path:\nAppEvents\\Schemes\\Apps\\.Default\\<b>SystemStart<\/b>\\.Current. Etc.<\/span><\/li>\n<li><b><span>strValueName<\/span><\/b><span>. This is the name of the registry value we need to change. Notice\nthat we didn&rsquo;t specify a name here. Why? Because the sound events all use the&nbsp;<b>(Default)<\/b>&nbsp;value\nto store sound information. When working with a (Default) you don&rsquo;t specify a\nvalue name.<\/span><\/li>\n<li><b><span>strValue<\/span><\/b><span>. This is the local path to the sound we want played each time we\nmaximize a window. Incidentally, sound information is stored using the\nREG_EXPAND_SZ data type; that means we could also use environment variables\nsuch as %WINDIR% and %SYSTEMROOT% in our file paths. In other words, we could\nhave assigned this value to strValue: %WINDIR%\\Media\\Ding.wav.<\/span><\/li>\n<\/ul>\n<p>At that point all we have to do is call the <strong>SetExpandedStringValue<\/strong> method to update the registry:<\/p>\n<pre class=\"codeSample\">objRegistry.SetExpandedStringValue _    HKEY_CURRENT_USER,strKeyPath,strValueName,strValue<\/pre>\n<p>Best of all, this change takes place immediately; there&rsquo;s no need to logoff or shutdown. Instead, run the script and then immediately try maximizing a window. If you don&rsquo;t hear a ding, the Scripting Guy who writes this column will eat his hat.<\/p>\n<p>And maybe even drink a PAREMSAN CARMEL LATTE.<\/p>\n<p>Cool, huh? Now, what about randomly assigning a sound to a sound event? Well, this next script retrieves a collection of all the files in the folder C:\\Windows\\Media:<\/p>\n<pre class=\"codeSample\">Set colFileList = objWMIService.ExecQuery _    (\"ASSOCIATORS OF {Win32_Directory.Name='C:\\Windows\\Media'} Where \" _        &amp; \"ResultClass = CIM_DataFile\")<\/pre>\n<p>The script then retrieves the value of the <strong>Count<\/strong> property, which tells it how many files were found in the Media folder:<\/p>\n<pre class=\"codeSample\">intCount = colFileList.Count<\/pre>\n<p>From there the script uses this block of code to generate a random number between 1 and the number of files in the folder:<\/p>\n<pre class=\"codeSample\">intHighNumber = intCountintLowNumber = 1RandomizeintNumber = Int((intHighNumber - intLowNumber + 1) * Rnd + intLowNumber)<\/pre>\n<p>Got all that? OK, after assigning the value 1 to a counter variable named i we next set up a For Each loop to loop through all the files in the collection. Inside that loop we use this line of code to determine if our counter variable is equal to the random number we generated:<\/p>\n<pre class=\"codeSample\">If i = intNumber Then<\/pre>\n<p>If it is, we grab the value of the file&rsquo;s <strong>Name<\/strong> (path) property, assign it to the variable strValue, then use the <strong>Exit For<\/strong> statement to exit the For Each loop:<\/p>\n<pre class=\"codeSample\">If i = intNumber Then    strValue = objFile.Name    Exit ForEnd If<\/pre>\n<p>If it isn&rsquo;t, we increment i by 1 and try again. Sooner or later, i will be equal to intNumber and we will have assigned a file path to strValue. Once that&rsquo;s happened we can then use our original script to assign that value to the Maximize sound event.<\/p>\n<p>Here&rsquo;s what the entire script looks like:<\/p>\n<pre class=\"codeSample\">Const HKEY_CURRENT_USER = &amp;H80000001strComputer = \".\"Set objWMIService = GetObject(\"winmgmts:\\\\\" &amp; strComputer &amp; \"\\root\\cimv2\")Set colFileList = objWMIService.ExecQuery _    (\"ASSOCIATORS OF {Win32_Directory.Name='C:\\Windows\\Media'} Where \" _        &amp; \"ResultClass = CIM_DataFile\")intCount = colFileList.CountintHighNumber = intCountintLowNumber = 1RandomizeintNumber = Int((intHighNumber - intLowNumber + 1) * Rnd + intLowNumber)i = 1For Each objFile in colFileList    If i = intNumber Then        strValue = objFile.Name        Exit For    End If           i = i + 1NextSet objRegistry = GetObject(\"winmgmts:\\\\\" &amp; strComputer &amp; \"\\root\\default:StdRegProv\") strKeyPath = \"AppEvents\\Schemes\\Apps\\.Default\\Maximize\\.Current\"strValueName = \"\" objRegistry.SetExpandedStringValue _    HKEY_CURRENT_USER,strKeyPath,strValueName,strValue<\/pre>\n<p>That should get you going, DW.<\/p>\n<p>Before we go, we&rsquo;d be remiss if we signed off without noting another reader board sign that the Scripting Guy who writes this column saw a year or so ago. While driving past a &ldquo;Used Equipment and Army Surplus Store,&rdquo; he saw a reader board that read like this:<\/p>\n<p>SLEEPING BAGS AND OUTDOOR GEAR<br \/>HIKING BOOTS<br \/>HALIBUT<\/p>\n<p>The Scripting Guy who writes this column wasn&rsquo;t sure whether this was used HALIBUT or Army Surplus HALIBUT. Nor did he bother to try and find out.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hey, Scripting Guy! Is there an easy way to randomize the selection of .wav files at the startup and shutdown of windows? &#8212; DW Hey, DW. Last night as the Scripting Guy who writes this column was on his way home he stopped at a red light. (Which is yet another thing that separates him [&hellip;]<\/p>\n","protected":false},"author":595,"featured_media":87096,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[16,47,3,5],"class_list":["post-56293","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-desktop-management","tag-general-management-tasks","tag-scripting-guy","tag-vbscript"],"acf":[],"blog_post_summary":"<p>Hey, Scripting Guy! Is there an easy way to randomize the selection of .wav files at the startup and shutdown of windows? &#8212; DW Hey, DW. Last night as the Scripting Guy who writes this column was on his way home he stopped at a red light. (Which is yet another thing that separates him [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/56293","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/users\/595"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/comments?post=56293"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/56293\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media\/87096"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media?parent=56293"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=56293"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=56293"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}