Click to See Complete Forum and Search --> : Could use some help with App.Activate and setTimeout


bshul
April 11th, 2008, 11:33 AM
Hello,

I am trying to finish some javascript, which will copy to clipboard, a highlighted phone number on a IE6 webpage,
then execute the Win98 phone dialer program,
then paste the number into the input field of the program
then press the Dial button
then wait 5 seconds
then terminate the running program by closing the 2 program windows that were generated.
I have so far accomplished
the copy to clipboard
the execute Dialer.exe program
and the activation of the Dial button.
There are a few problems presently.
I can only paste to the Dialer.exe program, if I first execute another program. This I believe is because I need a time delay between calling the Dialer.exe program and issuing the paste command, to allow the program to start running. I have not been successful using setTimeout("wsh.Sendkeys ('^V')",1000); to accomplish the delay before pasting. Instead I call notebook.exe which seems to allows the dialer.exe to receive the paste command. Is this a timing or program focus problem?
I also need the javascript to wait 5 seconds before sending 2 ALT F4 commands
setTimeout("wsh.Sendkeys ('%{F4}')",5000);
setTimeout("wsh.Sendkeys ('%{F4}')",5100);
Here is the code, with the non working code remarked out.
I am not a programmer, so please help me with code, versus general javascript theory.
I am running Wscript 5.6. Thank you all so much.

<script language="javascript"type="text/javascript">
{var SelectedText =window.external.menuArguments.document.selection.createRange().text;
window.clipboardData.setData("Text",SelectedText);}
var wsh = new ActiveXObject('WScript.Shell');
//setTimeout("wsh.Sendkeys ('^V')",1000);
wsh.Run("C:\\WINDOWS\\notepad.exe");
wsh.Run("C:\\WINDOWS\\Dialer.exe");
wsh.Sendkeys ("^V");
wsh.SendKeys ("{ENTER}");
//setTimeout("wsh.Sendkeys ('%{F4}')",5000);
//setTimeout("wsh.Sendkeys ('%{F4}')",5100);
</SCRIPT>

PeejAvery
April 11th, 2008, 01:03 PM
The setTimeout function calls a function. You will need to pass a JavaScript function as the parameter.

function sendKeys(keys) {
var wsh = new ActiveXObject('WScript.Shell');
wsh.Sendkeys (keys);
}

setTimeout("sendKeys ('%{F4}')", 5000);

However, note that you are using a delay to ensure that the phone window has already opened. What if the computer is highly processing? Then the window might not be open when the sendkeys are called. Or some other program might get activated and steal the focus causing the keystrokes to be sent to a different application.

If you want this to be seamless, you should write your own ActiveX. Then you could easily just grab the window handle of the phone program. Having the window handle would allow you to send the keystrokes directly to the program.

bshul
November 18th, 2011, 02:44 PM
I know very little about scripting. I copied and pasted your code into my code and it generated errors.
Interestingly, the my original code ran fine contained in a hidden htm file.

Luckily, I was able to write this code which is usable on any windows with WSH to accomplish phone dialing in this case, but is also a template for any key macro you want. It can be started by creating a shortcut to the saved .vbs file and putting the shortcut on the desktop, modifying the properties of the shortcut to respond to a hotkey.
In this case, just highlight any phone number on a webpage and hit the hotkeys. Phone dialer will dial the number and then close in 10 secs.

Set WshShell = WScript.CreateObject("WScript.Shell")
WshShell.SendKeys "^c"
WshShell.Run "dialer.exe"
WScript.Sleep 500
WshShell.SendKeys "^v"
WshShell.SendKeys "{ENTER}"
WScript.Sleep 10000
WshShell.SendKeys "%{F4}"
WScript.Sleep 100
WshShell.SendKeys "%{F4}"