experchange > scripting.wsh

MummerX (01-31-11, 03:29 PM)
Hi,
I must add my initials and date to many different types of doc
(Outlook msg, text file, Word doc, HTML form, ACCESS record
field, ...) and for many different reasons (authorisation,
acknowledgement, progress checkpoint, …). I have to do this many times
daily, and I am getting pretty tired of all this repetitious typing.

TBH, I am a total newb regarding WSH. However, I have created a WSH
script to generate a string of my initials and date, simply using
SendKeys. It is based on the ubiquitous example of "run notepad, then
send these characters". It works fine, for notepad. Now how do I get
it to run on any application (specifically: the current window,
whatever it is) ?

I want to have this script activated when I place the cursor in the
correct place (in whatever doc I am working on) and then hit a magic
key combination ... but I am unable to work out how to achieve this.
There seems to be no relevant code examples "out there" that do what I
want.

Please can anyone help?
Note: My paranoid IT dept will not let me download _any_ utility s/w,
so it has to be “home coded”.
Grateful for any help.
Tom Lavedas (01-31-11, 05:45 PM)
On Jan 31, 8:29 am, MummerX <mumm> wrote:
[..]
> Note: My paranoid IT dept will not let me download _any_ utility s/w,
> so it has to be “home coded”.
> Grateful for any help.


Remove the part that opens the application (or AppActivates it) and
then assign it to a desktop shortcut having a shortcut key specified,
such as Ctrl-Alt-I (for initials). Then position the cursor in your
document where you want the initials to appear and press the shortcut
key. Be prepared to wait a bit for the script to load and function.
This will work as long as no other application steals the focus
between the time you press the shortcut key and the time the script
issues the SendKeys string.

PS. My signature was added to this message using the following script
in this way ...

sSignature = "{Enter}_____________________" _
& "{Enter}Tom Lavedas"
createobject("Wscript.Shell").sendkeys sSignature
_____________________
Tom Lavedas
MummerX (02-01-11, 08:20 PM)
@ Tom Lavedas

Thanks. That was my approach entirely. My script (named Initials.vbs)
is very similar to yours.
Code:
Set wobj = WScript.CreateObject("WScript.Shell")
wobj.SendKeys " DWB:2011-01-31 "
WScript.Echo "Done!!!"

With (say) notepad as the current active window, I get the "Done!!!"
pop up box, but the keystrokes simply disappear into the aether. They
do not appear in the notepad window at the cursor postion, or
anywhere.
* Note: Notepad *always* loses focus during the execution of the
script (& no other window gains it). That seems somehow significant.

My first guess was that I needed some code to register the script as a
valid input source for the current active window (it's always bound to
be more difficult than one expects!). But your reply seems to
contradict that. How does this script work for you and not for me
(what have I missed)? Are you perhaps using a different version of
Windows (assuming that matters)?

BTW. Sorry everyone if this is a really basic beginner's question. If
you know of a tutorial which will help me with this issue, I'd be
grateful for the pointer. I've just not found it yet.
Grateful thanks in advance.
Dr J R Stockton (02-01-11, 09:47 PM)
In microsoft.public.scripting.wsh message <5fbd0bc9-6e14-4dfb-b81b-
0bd67e03cf88>, Mon, 31 Jan 2011 05:29:09,
MummerX <mummerx> posted:

>TBH, I am a total newb regarding WSH. However, I have created a WSH
>script to generate a string of my initials and date, simply using
>SendKeys. It is based on the ubiquitous example of "run notepad, then
>send these characters". It works fine, for notepad. Now how do I get
>it to run on any application (specifically: the current window,
>whatever it is) ?


It might be simpler to write your details in any form that can be shown
in a normal readonly window, and keep that part of that window open in a
corner of the screen. Then you can pick the details with a mouse-wipe
and a couple of clicks, and paste where required. Perhaps not quite as
convenient, but works into any application without further effort, and
you can have several sets and subsets of text to choose from.

A browser will display a *.txt file suitably, for example.

You could of course do both.
Todd Vargo (02-02-11, 12:29 AM)
"MummerX" <mummerx> wrote in message
news:1b7f
[..]
> you know of a tutorial which will help me with this issue, I'd be
> grateful for the pointer. I've just not found it yet.
> Grateful thanks in advance.


SendKeys is not reliable, however, you increase your chances if you wrap it
in suitable AppActivate and Sleep statements. Note, if multiple Notepad
windows are open, you are asking for trouble.

Set wobj = WScript.CreateObject("WScript.Shell")
If wobj.AppActivate("Notepad") Then
WScript.Sleep 100
wobj.SendKeys " DWB:2011-01-31 "
WScript.Echo "Done!!!"
Else
WScript.Echo "Failed to find Notepad."
End If
MummerX (02-02-11, 01:17 PM)
@Todd
> you increase your chances if you wrap it in suitable AppActivate and Sleep statements

Thanks; so I've read. I can certainly try the "sleep", but AppActivate
is too app-specific. Using notepad was just an example (see original
posting). I want to be able to insert my initials & date into a
variety of different doc types (HTML forms, Word doc, Access db
field, ...) without having to rewrite the script each time. I can't
believe this has to be so difficult.
Update: Added the sleep - no difference. Notepad (or whatever app)
loses focus and does not receive the sendkeys() string.
Script now ...
Set WshShell = WScript.CreateObject("WScript.Shell")
WScript.Sleep 100
WshShell.SendKeys " DWB 2011 02 02 "
WScript.Echo "Done!!!"
(Activated by keystroke: Ctrl+Alt+F12)

@Dr Stockton
> Then ... pick the details with a mouse-wipe and a couple of clicks, and paste where required

Thanks. Great minds think alike ... or a pair of half-wits with only a
single thought? :o)
I have been copy+pasting for months, using a Win 7 sticky note to hold
the initials+date text.
Being greedy, or rather because I have to e-sign so often (up to 100
times a day, at peak, across 3-4 doc types), even this is tiring &
irritating. I want something much quicker & slicker. :o)

@Tom
Not heard back from you. Curious to find out if you've had success
with more apps than just notepad. Also what version of Windows are you
using?

Thanks chaps, for your assistance so far. Very grateful.
Tom Lavedas (02-02-11, 03:57 PM)
On Feb 1, 1:20 pm, MummerX <mumm> wrote:
[..]
> you know of a tutorial which will help me with this issue, I'd be
> grateful for the pointer. I've just not found it yet.
> Grateful thanks in advance.


The focus is being taken by the WSript.Echo statement before the
keystrokes are able to reach Notepad. It just proves that SendKeys is
run asynchronously from the script - which is understandable, if you
think about it. Try this and see if it doesn't fix you problem ...

Set wobj = CreateObject("WScript.Shell")
wsh.sleep 100
wobj.SendKeys " DWB:2011-01-31 "
wsh.sleep 200
WScript.Echo "Done!!!"

The sleep before seems to be needed sometime to allow the focus to be
restored from the desktop shortcut back to the initial window before
sending the keystrokes and the one after to allow restoration to the
window after the script takes the focus to send the keystrokes.

I agree with Todd that it is usually best to wrap the attempt in a
closed AppActivate loop. But under the assumption that this is to be
used with multiple, arbitrary application, I don't see how to make
that work.
_____________________
Tom Lavedas
MummerX (02-02-11, 05:32 PM)
@Tom
> Try this and see if it doesn't fix you problem

I did. No difference. The current active window (into which I want to
add my initials & date) still loses focus, and the text goes nowhere.
It does not matter what the window (a.k.a. doc type) is.

> The focus is being taken by the WSript.Echo statement before the
> keystrokes are able to reach Notepad.

Good point! I tried commenting out the Echo. It was only in there to
convince myself the script was running. Still no difference: the
window loses focus ... etc ...

> The sleep before seems to be needed sometime to allow the focus to be
> restored from the desktop shortcut back to the initial window before
> sending the keystrokes and the one after to allow restoration to the
> window after the script takes the focus to send the keystrokes.

I increased both to 500. No difference, except it now takes longer of
course.

My final script (still not working, on any window) is:
Set wobj = CreateObject("WScript.Shell")
wsh.sleep 500
wobj.SendKeys " DWB:2011-02-02 "
wsh.sleep 500

I remain very curious as to why it works for you and not for me.
Please confirm it does work for you, and what version of Windows you
are using? (Sorry to keep asking this)

I started wondering whether perhaps merely starting the script is what
takes the focus away. Well ... probably. I commented out all the lines
of the script (silly, I know), but when I ran it by issuing the
required keystroke, sure enough ... THE CURRENT ACTIVE WINDOW LOST THE
FOCUS. This is with a script with no active commands in it.

So, it has to use AppActivate; no choice. Ah well.

I had hoped that it is possible to use WSH to identify the current
active window, and then use the result from that to AppActivate that
particular window.
But now that I know just starting the script defocusses the current
active windw, I'll lay odds even this idea won't work.

Unless anyone cares to regard that as a challenge? :o) I bet it's
not simple, so I'd need help from an expert as I know nothing about
WSH.

Any thoughts welcome.
I am extremely grateful for the thoughts and comments so far. The
contributors to this group are most helpful!
Tom Lavedas (02-02-11, 07:06 PM)
On Feb 2, 10:32 am, MummerX <mumm> wrote:
[..]
> Any thoughts welcome.
> I am extremely grateful for the thoughts and comments so far. The
> contributors to this group are most helpful!


If you don't use a shortcut with a defined hotkey, this approach
CANNOT be made to work with unnamed applications. It must be a
shortcut on the desktop. Clicking on the shortcut on the desktop or
the script in explorer will change the focus.

And, Yes, it works for me in XPSP3.
_____________________
Tom Lavedas

PS, I just used the approach to add my signature here in Firefox.
MummerX (02-03-11, 05:25 PM)
@Tom
Grateful thanks for your info. Very interesting.
Yes, I am (& have always been) using a shortcut on the desktop to link
the keystroke to the script. Wouldn't it be rather silly to double-
click on the script and expect the current active window to retain
focus? :o)
The script itself is on a network drive, currently, but it made no
difference when I temporarily moved it to my local hard disk (C:).
I even tried different keystroke combinations (in case I had picked a
reserved combo).
I've tried all sorts of straws. Nothing makes any difference.
I keep comingbaxck to this:
* The current active window *always* loses focus
* ... *immediately* after I hit the required keys
* ... even with all commands commented out in the script
* ... even with a totally empty script.
That, and the fact it just works for you on XP SP3, implies something
is broken on my office PC or in Win 7 Ent (both new in the last couple
of months).

@all
I have access to both Win XP SP3 and Win 7 Ult at home. When I get
some time (ha!) I will try out both. If anything interesting arises
I’ll try to remember to come back & log it here; it won't be for a
while though.
I feel that's about as far as we can go on this. I think I'll just
have to accept it as yet another conceptually simple task I can't do
on Windows (not for the first time) and move on.
Anyway, thanks everyone for your help. You've been a great example of
community support at its best (pat yourselves on the back), and I am
really grateful.
Bye for now.
Al Dunbar (02-04-11, 02:00 AM)
"MummerX" <mummerx> wrote in message
news:2128
[..]
> That, and the fact it just works for you on XP SP3, implies something
> is broken on my office PC or in Win 7 Ent (both new in the last couple
> of months).


as others have mentioned, sendkeys is notoriously unreliable. Hey, even when
I "send" "keys" to an app by typing them, I sometimes find that other
windows steal the focus on me.

But, if the current active window has lost focus, perhaps you can prefix the
characters you want to send with the equivalent of alt-tab to give it back.

/Al
Todd Vargo (02-04-11, 07:45 AM)
MummerX wrote:
[..]
> WshShell.SendKeys " DWB 2011 02 02 "
> WScript.Echo "Done!!!"
> (Activated by keystroke: Ctrl+Alt+F12)


Try playing with longer sleep times to see if your apps get the focus back
before you SendKeys. I have one script using a 2 second delay. Sometimes the
app gets the keys on first try, and sometimes it takes multiple tries. The
problem seems to be worse on XP computers than it was for me on 98. Good
luck with it.
phil.kiefer (11-18-13, 06:33 PM)
I think I may have found the answer to your problem - was stuck with it for a while, too. Took me the best part of an hour to figure it out. Try this (insert a sleep before the second line if any problems):

set WshShell = WScript.CreateObject("WScript.Shell")
WshShell.SendKeys "%{Esc}Here's your sig!"

Enjoy!
alexandre.santaroza (02-16-18, 12:49 PM)
On Monday, November 18, 2013 at 2:33:02 PM UTC-2, phil....@gmail.com wrote:
> I think I may have found the answer to your problem - was stuck with it for a while, too. Took me the best part of an hour to figure it out. Try this (insert a sleep before the second line if any problems):
> set WshShell = WScript.CreateObject("WScript.Shell")
> WshShell.SendKeys "%{Esc}Here's your sig!"
> Enjoy!


Thank you very much! it worked out!
Similar Threads