Jump to content


 


Register a free account to unlock additional features at BleepingComputer.com
Welcome to BleepingComputer, a free community where people like yourself come together to discuss and learn how to use their computers. Using the site is easy and fun. As a guest, you can browse and view the various discussions in the forums, but can not create a new topic or reply to an existing one unless you are logged in. Other benefits of registering an account are subscribing to topics and forums, creating a blog, and having no ads shown anywhere on the site.


Click here to Register a free account now! or read our Welcome Guide to learn how to use this site.

Photo

Script to change sound settings


  • Please log in to reply
13 replies to this topic

#1 Alamaxia

Alamaxia

  • Members
  • 25 posts
  • OFFLINE
  •  
  • Local time:10:57 PM

Posted 30 September 2009 - 11:30 PM

Hey all, have a question about writing some piece of code that would allow me to double click something on the desktop that would quickly change my sound output settings from Realtek AC97 Audio to UAC3553B, and back the other way when I click it again. I frequently switch over my audio basically from my PC speakers to my USB Stereo, and am just trying to find a faster way to do this without having to manually open my control panel settings each time. I don't have any previous scripting experience in windows (I do, however, have extensive VBA programming experience in Excel!), so any help is MUCH appreciated!

Thanks in advance!

BC AdBot (Login to Remove)

 


#2 Alamaxia

Alamaxia
  • Topic Starter

  • Members
  • 25 posts
  • OFFLINE
  •  
  • Local time:10:57 PM

Posted 01 October 2009 - 05:25 PM

Just bumping this up, hoping someone has an idea.

#3 Gammers

Gammers

  • Members
  • 44 posts
  • OFFLINE
  •  
  • Local time:09:57 PM

Posted 01 October 2009 - 06:21 PM

Hey Alamaxia,

Guess you and I were thinking along the same lines, a bit of script to get rid of a repetitive task. I was trying to figure out how I could create a bit of code (maybe a batch file?) to open the XP services and start or stop some. Hub has an iPhone and I really don't want several of the services running all the time -- just when he needs to sync.

Question, how do you do the switch of your audio? i.e. you go into the Control Panel, and then what do you click?

I was just looking at something. First I did a batch file with only this in it:

devmgmt.msc

Then I found there was a far easier way to get there :thumbsup: Just do a desktop short cut to that filename. That opens the Device Manager, but I'm not sure how you do your switch -- never did that before.

Surely someone has a code idea for this :flowers:

Gammers.

#4 Alamaxia

Alamaxia
  • Topic Starter

  • Members
  • 25 posts
  • OFFLINE
  •  
  • Local time:10:57 PM

Posted 02 October 2009 - 01:03 PM

I open control panel, double click on "Sounds and Audio Devices", go to the "Audio" tab, and use the dropbox under "Sound Playback" to switch my default audio device.

#5 Gammers

Gammers

  • Members
  • 44 posts
  • OFFLINE
  •  
  • Local time:09:57 PM

Posted 02 October 2009 - 09:56 PM

Greetings Alamaxia,

Found you a good headstart from someone who wanted the same thing as you. I'm writing up the step by steps for the following, but the other good person found the actual code line - I certainly can't take the credit for that. :thumbsup:

Okay... IN short, the following creates a desktop shortcut which will be a double click opening of the Audio tab of the "Sounds and Audio Devices Properties" dialog box. When the Audio tab opens, the "Sound Playback" setting "should be" highlighted so that you can make your speaker switch. Of course, the latter is what happens on my system, I might guess it will be the same on yours. Once at the setting, you should be able to just click the down arrow to access your speaker switch, then click Enter.

For a one click access to this setting, drag the desktop shortcut to your Quick Launch bar (lower right on the Windows XP task bar.)

====
Create the desktop shortcut:
1. Right click your desktop
2. Select "New" and then select "Shortcut"
3. In the box for "Type the location of the item" enter the following exactly as it is (you can copy and paste it):

RunDll32.exe shell32.dll,Control_RunDLL mmsys.cpl,,2

Click Next

4. In the box for "Type a name for the shortcut" you can put something like "Sound Output Change" (without the quotes), or "Speaker Change"
5. Click Finish

The desktop shortcut is made, you can now drag that shortcut to the Quick Launch bar.
=====

Okey dokey... As a brief explanation, the "mmsys.cpl" file in the above code is the one that opens the "Sounds and Audio Devices Properties" dialog box. As a side note .cpl files will open Control Panel items. You can find other .cpl files in the C:\Windows\System32 folder -- running those files opens up different CP items :flowers: Cool! I don't understand the switches in the code (,,2) but fiddled and found the following:

Of the "Sounds and Audio Devices Properties" ...

Opens the Sounds tab
RunDll32.exe shell32.dll,Control_RunDLL mmsys.cpl,,1

Opens the Audio tab
RunDll32.exe shell32.dll,Control_RunDLL mmsys.cpl,,2

Opens the Voice tab
RunDll32.exe shell32.dll,Control_RunDLL mmsys.cpl,,3

Opens the Hardware tab
RunDll32.exe shell32.dll,Control_RunDLL mmsys.cpl,,4

Opens the Volume tab
RunDll32.exe shell32.dll,Control_RunDLL mmsys.cpl,,5

If you copy/paste any of the above code lines into the Start\Run code box of Windows XP and click OK you will see the action of opening those tabs. Making a shortcut, or Quick Launch item sets you up more permanently.

Now then... There is a web page where some folks are discussing what you wanted. I didn't finish reading it, but you might want to in order to find a script that fits your exact needs:

http://www.donationcoder.com/Forums/bb/ind...c=3165.msg22210

I don't know whether the fellow got to the end of selecting the speakers with the script -- but you might. If so, let us know, eh? It might help me figure out how to do the switching I need of services :trumpet:

Hope the above helps you some.

Gammers

Edited: the people discussing on the above web page are discussing the AHK program, which I know nothing about. Surely there must be a simple .bat or shortcut or vbs script that can get you all the way to what you want. I'll continue looking, but since I don't have two sets of speakers, I can't test.

Edited by Gammers, 02 October 2009 - 09:59 PM.


#6 Gammers

Gammers

  • Members
  • 44 posts
  • OFFLINE
  •  
  • Local time:09:57 PM

Posted 02 October 2009 - 10:14 PM

Oh, Alamaxia...

Found a cool webpage, text file, with a number of code lines to access different items.

This is the webpage, Classic Visual Basic, of MS MVP Karl Peterson:

http://vb.mvps.org/tips/shellcpl.txt

Perhaps some of Karl's code samples will help you tie off what you want to do.

Gammers

#7 Alamaxia

Alamaxia
  • Topic Starter

  • Members
  • 25 posts
  • OFFLINE
  •  
  • Local time:10:57 PM

Posted 02 October 2009 - 11:50 PM

AWESOME finds Gammers, thanks! :thumbsup: :flowers: :trumpet:

I'll be sure to read through all of this and let ya know what I come up with!

#8 Gammers

Gammers

  • Members
  • 44 posts
  • OFFLINE
  •  
  • Local time:09:57 PM

Posted 03 October 2009 - 02:47 AM

Okay Alamaxia,

I think I have your ticket...but play at your own risk since this is script. I scoured around the web and cobbled some things together. Since I don't have two sets of speakers (heck, you can teach me how to do that), I had to experiment with another Control Panel setting that had a drop list with more than one choice :thumbsup: i.e. you have more than one choice for speakers, we have to change that with code, I needed something with more than one choice to experiment with this.

Prior to showing you what I propose might work for changing your speaker settings from one set of speakers to another, the following shows "why/how" I got to your possibility.

First, I played with the Power Settings in the Control panel. My Power Settings are set to "Home/Office Desk," pretty vanilla. I twibbed script samples until I could figure out how to change that setting from ""Home/Office Desk" to "Always on."

To do this, I made a text file with notepad, placed it on the desktop and then changed the file extension from .txt to .vbs I kept playing with code in a junk.vbs file, just double clicked the junk.vbs on the desktop to launch the script in between playing with the code additions/changes. MIND YOU, I can explode my system and then bail it out, if you can't do that...PLEASE think twice before piddling with this.

So...my power setting was on "Home/Office Desk" The following opened the Power Option settings in the Control Panel to the Power Schemes tab (that's the "powercfg.cpl" you see in the following), the focus was on the "Power Schemes" when it opened. The code then sent the letter "A" to the setting to select "Always on" i.e. this is the "SendKeys" bit with the letter "A" which is the first letter of "Always on" The code then tabbed 8 times to the "Apply" button, then the code used an "Enter" to press the Apply button, after that the code used "ESC" to get out of the dialog box. Bingo, the setting was changed to Always on.

I tested putting the setting back to "Home/Office Desk" by using the SendKey of "H" (the first letter of "Home/Office Desk") instead of the "A"

Code bit to change the power setting...
======= snip =======
Set Shell = CreateObject("Shell.Application")
Shell.ControlPanelItem cstr("powercfg.cpl")
Set Shell = Nothing

Set WshShell = CreateObject("WScript.Shell")
WScript.Sleep(2200)
WshShell.SendKeys "A"
WScript.Sleep(2200)
WshShell.SendKeys "{TAB 8}"
WshShell.SendKeys "{ENTER}"
WshShell.SendKeys "{ESC}"
Set WshShell = Nothing
===== snip ========

NOW THEN! Above, you will notice the "Sleep." For some reason, Windows XP wanted a little think before it moved on...that pause also gave me a chance to "eyeball" what was happening. You can test with and without that Sleep line, with more or less sleep time, or add more sleep lines in between your code bits to test.

The following is what I think will work for you BUT you must put the RIGHT Sendkey letters into your code bits. For example, if you want the "BigFat Sound Box" speakers, use the SendKey of "B" If you want the "Wimpy Tinny" speakers then use the SendKey of "W" -- get the picture? You can test this manually by going to that setting and typing B or W to see if it switches between the names of your speakers.

On the other hand :flowers: You said you had Realtek AC97 Audio and UAC3553B that you wanted to switch between, so I might assume you will need SendKeys of "R" and "U" (make one .vbs with the "R," make another .vbs with the "U")

In the following the Audio tab is opened with mmsys.cpl,,2

Proposed code bit for your need -- make sure to put the right sendkey in
======== snip =======
Set Shell = CreateObject("Shell.Application")

' The following opens the Audio tab of the Control Panel item Sounds and Audio Devices
' The cursor focus should immediately be on the Sound Playback default device
Shell.ControlPanelItem cstr("mmsys.cpl,,2")
Set Shell = Nothing

Set WshShell = CreateObject("WScript.Shell")

' The following will pause the script so that WinXP can have a rest and a think
' Use the sendkey of R to have the code select your Realtek AC97 audio speakers
' Use the sendkey of U to have the code select your UAC3553B stereo
WScript.Sleep(2200)
WshShell.SendKeys "R"

' The following pauses the script again -- you can look to see if Realtek or UAC3553B was selected
' Then the code tabs down 11 times to put the focus on the Apply button -- you may need more or less tabs to get there
' The following worked for me in WinXP SP2 Media Center edition
WScript.Sleep(2200)
WshShell.SendKeys "{TAB 11}"

' This presses the Apply button for you
WshShell.SendKeys "{ENTER}"

' This exits out of the Audio tab dialog box after having made the setting change
WshShell.SendKeys "{ESC}"

Set WshShell = Nothing
======== snip =======

The following might be the short form of the code, should it work as above:

======== snip =======
Set Shell = CreateObject("Shell.Application")
Shell.ControlPanelItem cstr("mmsys.cpl,,2")
Set Shell = Nothing

Set WshShell = CreateObject("WScript.Shell")
WScript.Sleep(1000)
WshShell.SendKeys "R"
WshShell.SendKeys "{TAB 11}"
WshShell.SendKeys "{ENTER}"
WshShell.SendKeys "{ESC}"
Set WshShell = Nothing
======== snip =======

Now then.. I suppose someone could make you a toggling code with just one VBS file, but I'm thinking make two files one named Realtek (use the sendkey of R, probably), one named UAC (use the sendkey of U, probably). Once your desktop realtek.vbs and UAC.vbs files work to do the switch, drag them to the QuickLaunch section of your task bar and you have a one click option to switch your speaker settings.

Wow, I'm proud of myself :trumpet: I'll have to figure out how to use this on something :inlove:

Gammers

PS. Jama's code, on the following webpage, really helped me alot
http://www.pctools.com/forum/showthread.php?t=19999

Let me know if this works for you, okay?

Edited by Gammers, 03 October 2009 - 02:54 AM.


#9 Gammers

Gammers

  • Members
  • 44 posts
  • OFFLINE
  •  
  • Local time:09:57 PM

Posted 03 October 2009 - 01:25 PM

Well Alamaxia?

How we cooking?

I'm looking forward to a mental/emotional perk and seeing you get this handled will give me that perk.

Gammers

#10 Alamaxia

Alamaxia
  • Topic Starter

  • Members
  • 25 posts
  • OFFLINE
  •  
  • Local time:10:57 PM

Posted 04 October 2009 - 06:13 PM

Hey Gammers,

I haven't had a chance to do anything yet - have had an overly busy weekend. I'll hopefully get around to tinkering with it this week.

Thanks MUCHO though for the finds! I'll definitely let you know when I get it going.

#11 Gammers

Gammers

  • Members
  • 44 posts
  • OFFLINE
  •  
  • Local time:09:57 PM

Posted 06 October 2009 - 06:06 PM

Greetings Alamaxia,

Well?? You aren't going to keep me in suspense forever are you now? :thumbsup:

Gammers

#12 Alamaxia

Alamaxia
  • Topic Starter

  • Members
  • 25 posts
  • OFFLINE
  •  
  • Local time:10:57 PM

Posted 12 October 2009 - 04:26 PM

Gammers, it works out well! I'm going to play with it a bit to see if I can't add an if-statement in there to automatically detect if it should switch from Realtek to UAC or from UAC to Realtek... also going to minimize those wait times so it happens faster!

THANKS MUCH! :thumbsup: :flowers: :trumpet:

#13 Alamaxia

Alamaxia
  • Topic Starter

  • Members
  • 25 posts
  • OFFLINE
  •  
  • Local time:10:57 PM

Posted 12 October 2009 - 04:33 PM

Hmm... couldn't figure out how to put in an if statement for my needs... but I was able to take the wait down to 100 instead of 2200 and it works like a gem!

#14 Gammers

Gammers

  • Members
  • 44 posts
  • OFFLINE
  •  
  • Local time:09:57 PM

Posted 12 October 2009 - 11:55 PM

Hallo Al

>>and it works like a gem! <<

YAHOOOOOO!

She shoots, she scores! :thumbsup:

Gammers




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users