PDA

View Full Version : Another for the "team that can"...


Keef
24th Feb 2013, 18:24
Several longstanding problems on the Keefpooter have been solved by the wise heads in here. I'm getting down to the little ones now...

I'm an owl, but M likes to turn in early, so the PC speakers go off at 10pm.
In the olden days, that was done with a timeswitch in the power connection to the speakers, and worked fine.

More recently, software fixes have been used. The first was a little app that kills the speakers according to a schedule. Then I found a clever little utility that will switch between speakers and headphones - it rejoices under the name of ToggleAudio. However, it needs an input when it starts, so using the task scheduler as-is won't do. I've written a little batch file that works perfectly.

BUT ... if I run that batch file under "Task Scheduler" it doesn't work. The .exe starts, and that's as far as it gets. If I look at Task Manager, I can see it in there (and stop it) but it doesn't do its job. Running the batch file from the desktop, it works fine, and closes OK so it's not sitting there in task manager.

What am I missing? Here's the batch file:

h:
cd H:\ToggleAudio\
toggle-audio-7.exe
choice /T 1 /C ync /CS /D y
taskkill /F /IM toggle-audio-7.exe
c:
exit

The "choice" line provides the .exe with an input to keep it happy.

The taskkill line is in there as an attempt to close it down when it's running from Task Scheduler (but doesn't work).

Mike-Bracknell
24th Feb 2013, 19:48
Are you running the task in the logged-in user's security profile?
Are you setting the task to operate whether the user is logged in or not?

Keef
24th Feb 2013, 20:05
Yes, running in my user-account, which is always logged on. The PC is in my study, and nobody else goes near it.

Yes, run whether logged on or not.

Also "Run with highest privileges".

mixture
24th Feb 2013, 22:21
Keef,

Its been a while since I've done batch scripting, ever since the advent of PowerShell I've found that to be much better in the rare events I find myself writing scripts on Windows.

I'm taking a stab in the dark here..... but how about replacing


h:

cd H:\ToggleAudio\


toggle-audio-7.exe


with


start "ToggleAudio" /d "H:\ToggleAudio\" /wait toggle-audio-7.exe


(you might possibly need to spec the full path for the exe rather than the prog name, I can't remember).

A quick Googoo also suggests there may be alternate means to achieve your goal if you can't get the exe method working.... Toggle set default audio device in Windows 7. - Scripts - AutoHotkey Community (http://www.autohotkey.com/board/topic/68257-toggle-set-default-audio-device-in-windows-7/) and Windows 7 Tip: How to change the default audio device with a hotkey (http://downloadsquad.switched.com/2010/06/16/windows-7-tip-how-to-change-the-default-audio-device-with-a-hot/)

Milo Minderbinder
24th Feb 2013, 22:38
Its also worth remembering that running a batch file from a network drive, or sometimes even from a removeable drive (which I presume H: is) hits a brick wall in Windows Vista / 7.......the OS just refuses to run it, even it run with admin privliges.
In the past I've got round it - sometimes - by creating a shortcut pointing to the bat file which points to the exe file........messy, but sometimes it will work.
Or you could try moving the toggleaudio exe file to a folder on the C: drive. Even then if you still have problems, run a shortcut to the bat file and run that. Don't directly run the bat file. Sounds crazy, but - in the past - its worked

Keef
25th Feb 2013, 21:06
Thanks for those.

H is the backup drive, a second SATA drive inside the case and on the motherboard. I moved the folder to the C drive. That made no difference.

Mixture's start "ToggleAudio" /d "C:\ToggleAudio\" /wait toggle-audio-7.exe worked perfectly in "batch" mode, just like my batchfile.
It also locked up in exactly the same place as the batchfile when run by task scheduler.

I put in some echo > output.txt lines to track where the batchfile got to. It stopped at the line that starts the .exe file.

A separate shortcut to run the batch file did the same thing.

The .exe on its own in task scheduler was subtly different - the programme started and the audio control panel appeared on the screen, and stopped there. It didn't toggle over, although I could operate it as if I had opened it manually. The task scheduler showed "running" and task manager showed it running.

The problem seems to be that running the .exe from task scheduler (whether via a batch file or direct) it doesn't complete.

I have the batch file on a link on the desktop, and that works flawlessly every time.

Curious! I'm not that bothered about the action (the link on the desktop is enough), but I'm puzzled about the way it doesn't work.

mixture
25th Feb 2013, 22:02
Hi Keef,

How does toggle-audio-7.exe actually behave ? Does it launch an interface of some description ?

Keef
25th Feb 2013, 22:21
I simplified the source code to this, which when compiled works OK from a CMD line or from clicking on the .exe - but doesn't work from Task Scheduler.


; This is the "on" version


$windowName = "Sound"
$altPlusKey = "s"
$numDevices = 2


; Set device position to switch to 2 = Speaker
$ItemNumber = 2

; Run the sound control applet
Run("control mmsys.cpl")

; Wait a second for it to be active before sending keystrokes
Sleep (1000)

; Put the focus on the list
Send("{TAB}{TAB}{TAB}{TAB}")

; Tab down to the device position.
For $i = 1 to $ItemNumber Step 1
Send("{DOWN}")
Next

; Press shortkey to set the selected device as the default
Send("!"&$altPlusKey)

; Close the window.
WinClose($windowName)


I tried running that via a batch file - same result!

Keef
1st Mar 2013, 23:52
FIXED IT!

The problem seems to be that Task Scheduler can't run anything that uses Control Panel items (don't ask me why!). It starts the CPL item OK, but then the CPL takes over and subsequent commands from TS aren't passed to it. If the CPL appears at all on the screen, it will be flashing and asking for attention.

Batch files called by Task Scheduler can't bypass that, because TS keeps running until the batch file exits.

Microsoft offer a free app called "Mayhem" which will do things Task Scheduler won't, such as run a program if the stock market does something. It has a raft of other toggles. One such is "if a folder contents change". Mayhem doesn't care about the Control Panel niceties that bother Task Scheduler.

So I have two little apps, one to switch from speaker to headphones, and one to go the other way (the script above).

Task scheduler runs an "audio-on" or an "audio-off" batch file, which toggles the name of a file in subdirectories called "ON" and "OFF", and TS then exits.

Mayhem sees the subdirectory contents change, and runs the appropriate script.

It's messy, but it works. I've not found a way to do it without Mayhem.