Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Copy selection in any application

229 views
Skip to first unread message

Larry

unread,
Sep 3, 2006, 7:19:38 AM9/3/06
to
Is there a way to use WSH to copy the selection regardless of what application happens to be active?

In other words, first I manually select text, it might be in Internet Explorer, it might be in an e-mail, or in any application, and then I run a .vbs script the first step of which is to copy the selection regardless of what application happens to be active. Is that possible?

Thanks,
Larry

Dave Patrick

unread,
Sep 3, 2006, 10:32:57 AM9/3/06
to
Maybe something here.

http://www.autoitscript.com/autoit3/index.php

--

Regards,

Dave Patrick ....Please no email replies - reply in newsgroup.
Microsoft Certified Professional
Microsoft MVP [Windows]
http://www.microsoft.com/protect

McKirahan

unread,
Sep 3, 2006, 1:01:33 PM9/3/06
to

"Larry" <larry32...@att.net> wrote in message
news:OAWbwr0z...@TK2MSFTNGP02.phx.gbl...


Will this help? Watch for word-wrap.

<html>
<head>
<title>clips.htm</title>
<script type="text/javascript">
function Clip() {
var obj = new ActiveXObject("htmlfile")
var str = obj.parentWindow.clipboardData.getData("text");
alert(str);
}
</script>
</head>
<body>
<input type="button" value="Clipboard" onclick="Clip()">
</body>
</html>

It only works under IE.


Larry

unread,
Sep 3, 2006, 8:00:07 PM9/3/06
to
I have the AutoIt on my computer, but in its long list of capabilities, I don't see one for Copy.


"Dave Patrick" <DSPa...@nospam.gmail.com> wrote in message news:us6zaX2...@TK2MSFTNGP06.phx.gbl...

Larry

unread,
Sep 3, 2006, 7:59:09 PM9/3/06
to
Thanks, but this looks like a script that would be run directly from a htm file (that is, by creating a button in the IE window and running the htm file with that button--I have a bunch of those). That's not what I'm doing here. I'm working with a .vbs file. I'm just trying to add the additional step of Copy so as to automate as much of it as I can.

Also, I want this to work in any application, not just IE.

It seems funny that WSH wouldn't have a simple way of replicating the utterly simple step of pressing Ctrl+C.

Larry

"McKirahan" <Ne...@McKirahan.com> wrote in message news:-96dnSc2rdjBl2bZ...@comcast.com...

mayayana

unread,
Sep 4, 2006, 12:08:57 AM9/4/06
to
I don't think you'll find that. Programs run in
their own space. Microsoft has an Active
Accessibility API for use in screen readers
for blind people that can sort of do what you want,
but even that is limited, very complex, and can't
be done via script. MS has never actually developed
an API that *really* provides the access that you
want.

----------------

Walter Zackery

unread,
Sep 4, 2006, 6:29:08 AM9/4/06
to

"Larry" <larry32...@att.net> wrote in message
news:OAWbwr0z...@TK2MSFTNGP02.phx.gbl...

Thanks,
Larry

Try this function:

Function GetClipData()
GetClipData =
CreateObject("htmlfile").parentWindow.clipboardData.getData("text")
End Function


mayayana

unread,
Sep 4, 2006, 8:40:36 AM9/4/06
to
McKirahan gave him that yesterday,
but he's not in IE. He wants it system-wide.


Walter Zackery

unread,
Sep 4, 2006, 10:01:54 AM9/4/06
to

"mayayana" <mayaXX...@mindXXspring.com> wrote in message
news:89VKg.5914$xQ1....@newsread3.news.pas.earthlink.net...
: McKirahan gave him that yesterday,

: but he's not in IE. He wants it system-wide.
:
:
Matbe I'm missing something here, but you don't need to be in IE for the
function to work, as long as IE is installed on the system.. It uses IE (or
more accurately the IE DLL's) , but it will work from a vbs or js file. As a
matter of fact, it absolutely makes no sense to use it in IE because you
already have a clipboardData object running.


McKirahan

unread,
Sep 4, 2006, 2:21:49 PM9/4/06
to
"Larry" <larry32...@att.net> wrote in message
news:uKIrSW7z...@TK2MSFTNGP06.phx.gbl...

Thanks, but this looks like a script that would be run directly from a htm
file (that is, by creating a button in the IE window and running the htm
file with that button--I have a bunch of those). That's not what I'm doing
here. I'm working with a .vbs file. I'm just trying to add the additional
step of Copy so as to automate as much of it as I can.

Also, I want this to work in any application, not just IE.

It seems funny that WSH wouldn't have a simple way of replicating the
utterly simple step of pressing Ctrl+C.

[snip]

Try this:

WScript.Echo
CreateObject("htmlfile").ParentWindow.ClipboardData.GetData("text")


mayayana

unread,
Sep 4, 2006, 7:12:23 PM9/4/06
to
Oh, sorry. I misunderstood. That's an odd
function. It looked like it was meant to get
an IE selection. But it works just as you
say on my system with IE 5.00.

mayayana

unread,
Sep 5, 2006, 10:44:26 PM9/5/06
to
I'm still trying to figure this one out. First, it
sounded like the OP wanted to copy any selection,
not send it to the clipboard.

But beyond that, I'm confused about
what the code is supposed to do. I thought I
had it working but GetClipData doesn't return
text for me no matter what I do. Even if I first try
using SetData, that returns false, nothing has
changed on the Clipboard, and GetData still
returns nothing.

From this MSDN page:
http://msdn.microsoft.com/workshop/author/dhtml/reference/methods/getdata.as
p

it appears the GetData is used only within
the DHTML of a specific webpage, to capture
a selection. Thinking about it, that makes
sense. I can't see even Microsoft being so reckless
as to let IE read *any* selection or have unrestricted
access to Clipboard data. But several people seem
to think that GetData does *something* useful. So
I'm also wondering whether it varies with IE versions.
I've got IE 5.00 in 98SE, but MSDN says that's sufficient.

Fosco

unread,
Sep 5, 2006, 11:45:23 PM9/5/06
to
"mayayana"

> Even if I first try
> using SetData, that returns false, nothing has
> changed on the Clipboard, and GetData still
> returns nothing.

I think it's by your IE protections settings
IE >> clipboard works ONLY with low protection settings

Set objIE = CreateObject("InternetExplorer.application")

' IE security settings MUST be set on LOW

objIE.Visible = True
objIE.Navigate "http://snipurl.com/index.php"
' objIE.document.parentwindow.clipboardData.SetData"text",""
'objIE.document.parentwindow.clipboardData.SetData"text","http://www.google.it/search?hl=it&q=internetexplorer.applicati
on%2Bclick&btnG=Cerca+con+Google&meta="
WScript.Sleep 2000
Do While objIE.Busy = True
WScript.Sleep 50
Loop


objIE.document.forms.item(0).link.Value="http://www.google.it/search?hl=it&q=internetexplorer.application%2Bclick&btnG=C
erca+con+Google&meta="

objIE.Document.all.item(106).click

WScript.Sleep 2000
GetText = objIE.document.parentwindow.clipboardData.GetData("text")
msgbox GetText
'objIE.document.parentwindow.clipboardData.SetData"text",TextFromHTML
'TextFromHTML = objIE.document.parentwindow.clipboardData.GetData("text")
'msgbox TextFromHTML
' Nella clip adesso hai il testo della pagina voluta e l'avrai anche in google.txt
' objie.quit

--
Fosco

mayayana

unread,
Sep 6, 2006, 8:48:36 AM9/6/06
to
> > Even if I first try
> > using SetData, that returns false, nothing has
> > changed on the Clipboard, and GetData still
> > returns nothing.
>
> I think it's by your IE protections settings
> IE >> clipboard works ONLY with low protection settings
>

The MSDN page I linked seems to be
saying that, as well: That you can't copy
from a higher security webpage, from
Word, etc. And your example is getting
text from IE that's run within the same script.
The other posts seemed to be suggesting
that GetData would copy the selection of
any active window, as the OP was asking
for. That seemed far fetched but I wanted to
make sure that I'm not missing something. :)

Walter Zackery

unread,
Sep 6, 2006, 2:11:30 PM9/6/06
to

"mayayana" <mayaXX...@mindXXspring.com> wrote in message
news:EszLg.15794$Qf....@newsread2.news.pas.earthlink.net...
:> > Even if I first try
:
:
I probably misunderstood the OP. I assumed that he wanted to copy text from
the clipboard placed there by any app, but if he wants to copy the current
selection from the active app via script then I agree that is impossible.
You couldn't even use SendKeys (if you're that desperate) because you have
no way of determining the active app via script.


Larry

unread,
Sep 6, 2006, 9:41:02 PM9/6/06
to
> WScript.Echo
> CreateObject("htmlfile").ParentWindow.ClipboardData.GetData("text")

Thanks, but this doesn't make anything happen (except make an empty message box appear).

Walter's version is the same.

I don't know what to do to make it low security settings in IE, or if that's a good idea.

Thanks, all, for your interest.

Larry

"McKirahan" <Ne...@McKirahan.com> wrote in message news:0PedneQK7dcu82HZ...@comcast.com...

Fosco

unread,
Sep 6, 2006, 9:59:59 PM9/6/06
to
"Larry"

I don't know what to do to make it low security settings in IE, or if that's a good idea.

It's not a good idea IMHO

use autoit's clip instead
http://www.hiddensoft.com/autoit3/

$bak = ClipGet()
MsgBox(0, "Clipboard contains:", $bak)
ClipPut($bak & "additional text")
MsgBox(0, "Clipboard contains:", ClipGet())

; Autoit has also HotKeySet maybe useful for your job

HotKeySet
--------------------------------------------------------------------------------

Sets a hotkey that calls a user function.


HotKeySet ( "key" [, "function"] )


Parameters

key The key(s) to use as the hotkey. Same format as Send().
function [optional] The name of the function to call when the key is pressed. Leave blank to unset a previous hotkey.

Return Value

Success: Returns 1.
Failure: Returns 0.

Remarks

Up to 64 simultaneous hotkeys per script may be registered.

If two AutoIt scripts set the same HotKeys, you should avoid running those scripts simultaneously. (The second script
cannot capture the hotkey unless the first script terminates or unregisters the key prior to the second script setting
the hotkey.)

A hotkey-press *typically* interrupts the active AutoIt function/statement and runs its user function until it completes
or is interrupted. Exceptions are as follows:
1) If the current function is a "blocking" function, then the key-presses are buffered and execute as soon as the
blocking function completes. MsgBox and FileSelectFolder are examples of blocking functions. Try the behavior of
Shift-Alt-d in the Example.
2) If you have paused the script by clicking on the AutoIt Tray icon, any hotkeys pressed during this paused state are
ignored.

The following hotkeys cannot be set:

Ctrl+Alt+Delete It is reserved by Windows
F12 It is also reserved by Windows, according to its API.
NumPad's Enter Key Instead, use {Enter} which captures both Enter keys on the keyboard.
Win+B,D,E,F,L,M,R,U; and Win+Shift+M These are built-in Windows shortcuts. Note: Win+B and Win+L might only be reserved
on Windows XP and above.
Alt, Ctrl, Shift, Win These are the modifier keys themselves!
Other Any global hotkeys a user has defined using third-party software, any combos of two or more "base keys" such as
'{F1}{F2}', and any keys of the form '{LALT}' or '{ALTDOWN}'.

When you set a hotkey, AutoIt captures the key-press and does not pass it on to the active application, with one
exception: the Lock keys (NumLock, CapsLock, and ScrollLock) still toggle their respective state!
To Send() a key combination which will trigger a HotKeySet() event, either use ControlSend() or unregister the
HotKeySet() event, otherwise, the Send() event may trigger an infinite loop.

; capture and pass along a keypress
HotKeySet("{Esc}", "captureEsc")
Func captureEsc()
; ... can do stuff here
HotKeySet("{Esc}")
Send("{Esc}")
HotKeySet("{Esc}", "captureEsc")
EndFunc


The called function can not be given parameters. They will be ignored.

@HotKeyPressed macro can be used inside the function to handle several keys in the same function.


Related

Send, ControlSend


Example


; Press Esc to terminate script, Pause/Break to "pause"

Global $Paused
HotKeySet("{PAUSE}", "TogglePause")
HotKeySet("{ESC}", "Terminate")
HotKeySet("+!d", "ShowMessage") ;Shift-Alt-d

;;;; Body of program would go here ;;;;
While 1
Sleep(100)
WEnd
;;;;;;;;

Func TogglePause()
$Paused = NOT $Paused
While $Paused
sleep(100)
ToolTip('Script is "Paused"',0,0)
WEnd
ToolTip("")
EndFunc

Func Terminate()
Exit 0
EndFunc

Func ShowMessage()
MsgBox(4096,"","This is a message.")
EndFunc

--
Fosco


Larry

unread,
Sep 6, 2006, 10:11:24 PM9/6/06
to

There's a lot here, I'll try to figure this out. Thanks.

Larry


"Fosco" <fa...@fake.invalid> wrote in message news:z2LLg.3003$K8....@news.edisontel.com...

Fosco

unread,
Sep 6, 2006, 10:19:40 PM9/6/06
to
"Fosco"
> "Larry"

> use autoit's clip instead
> http://www.hiddensoft.com/autoit3/

Or "very basic"[1] the Windows default keys :

;[1] NOT all programs use Control+c & Control+v

; highlight the text press ESC to copy and F1 to paste


HotKeySet("{Esc}", "Copy")
HotKeySet("{F1}", "Paste")

While 1
Sleep(100)
WEnd

Func Copy()
send("^c")
EndFunc
Func Paste()
send("^v")
EndFunc

--
Fosco


Fosco

unread,
Sep 6, 2006, 10:22:05 PM9/6/06
to
"Fosco" <fa...@fake.invalid>
> "Fosco"
> > "Larry"

> Or "very basic"[1] the Windows default keys :
>
> ;[1] NOT all programs use Control+c & Control+v

But you can set/use multiple Hotkeyset (copy1 >> copy2 ecc)

--
Fosco


McKirahan

unread,
Sep 7, 2006, 12:25:35 AM9/7/06
to
"Larry" <larry32...@att.net> wrote in message
news:Oq2EX9h0...@TK2MSFTNGP04.phx.gbl...

> WScript.Echo
> CreateObject("htmlfile").ParentWindow.ClipboardData.GetData("text")

Thanks, but this doesn't make anything happen (except make an empty message
box appear).

Did you allow for word-wrap? It should all be on ONE line.

Here's another way to test it:
open an IE browser and cut-and-paste the following ONE line into the
Address:

vbscript:Alert(CreateObject("htmlfile").ParentWindow.ClipboardData.GetData("
text"))

It should echo back what you last "cut"; i.e. copied to the clipboard.

Of course, you don't need IE to run it in a .vbs script file.

Just save the original ONE line in a .VBS file and run it to echo
"the selection regardless of what application happens to be active.".

[snip]

Larry

unread,
Sep 8, 2006, 9:28:11 PM9/8/06
to

I don't think this is going to help. This is all about assigning hotkeys. But all I want is to have the Copy command performed as one of the steps in a script, in whatever window happens to be active. Since Copy (Ctrl+C) is a simple command that works everywhere in the Windows environment, I'm surprised that it would be so difficult or impossible to get a vbs script or an au3 script to run the Copy command. But it seems to be.

Thanks all.

Larry


This is all bout
"Larry" <larry32...@att.net> wrote in message news:uvaCxOi0...@TK2MSFTNGP03.phx.gbl...

McKirahan

unread,
Sep 8, 2006, 10:49:29 PM9/8/06
to
"Larry" <larry32...@att.net> wrote in message
news:#vSmx860...@TK2MSFTNGP06.phx.gbl...

[snip]

Did you see my post of 09/06/2006 11:25 PM?

Try this more verbose version:

Option Explicit
Dim objCBD
Set objCBD = CreateObject("htmlfile")
Dim strCBD
strCBD = obj.ParentWindow.ClipboardData.GetData("text")
WScript.Echo strCBD

mayayana

unread,
Sep 9, 2006, 12:23:36 AM9/9/06
to
>>
But all I want is to have the Copy command performed as one of the steps in
a script, in whatever window happens to be active. Since Copy (Ctrl+C) is a
simple command that works everywhere in the Windows environment, I'm
surprised that it would be so difficult or impossible to get a vbs script or
an au3 script to run the Copy command. But it seems to be.
>>

That actually seems to work on my machine.
At least it worked in Notepad. It can only
work where CTRL + C is a copy hotkey, and there
may be security issues in XP, but you can give
it a try. I wrote this with a pause of 5 seconds so
that you have a chance to click a window to make it
active before the SendKeys runs:

Dim sh
WScript.sleep 5000
Set sh = CreateObject("WScript.Shell")
sh.SendKeys "^C"

This seems a bit dubious, though, unless it's
exactly what you need for something specific.
In most cases I'd consider it a very badly
behaved script if it erases my clipboard
when it runs.

Fosco

unread,
Sep 9, 2006, 12:53:55 AM9/9/06
to
"McKirahan"

> strCBD = obj.ParentWindow.ClipboardData.GetData("text")

Option Explicit
Dim objCBD
Set objCBD = CreateObject("htmlfile")
Dim strCBD

strCBD = objCBD.ParentWindow.ClipboardData.GetData("text")
WScript.Echo strCBD

--
Fosco


Fosco

unread,
Sep 9, 2006, 12:57:55 AM9/9/06
to
"Fosco"

> "McKirahan"
> > strCBD = obj.ParentWindow.ClipboardData.GetData("text")
>


But it gives anyway ONLY the actual clip buffer
he wants to copy selected test

--
Fosco


Fosco

unread,
Sep 9, 2006, 1:01:01 AM9/9/06
to
"Fosco"
> "Fosco"
> > "McKirahan"

> But it gives anyway ONLY the actual clip buffer
> he wants to copy selected test

If i recall he will only highlight the text the rest made by script
and this in ANY app that has the focus

--
Fosco


McKirahan

unread,
Sep 9, 2006, 8:34:05 AM9/9/06
to
"Fosco" <fa...@fake.invalid> wrote in message
news:jUrMg.3298$K8....@news.edisontel.com...

Thanks for your thoughts.

I made a distinction between your "highlight" and his "select".

The original post asked:

"first I manually select text, it might be in Internet Explorer,
it might be in an e-mail, or in any application, and then I run
a .vbs script the first step of which is to copy the selection
regardless of what application happens to be active. "

My thought was that "manually select text" was more than
just highlighting it; that it included copying it to the clipboard.

The pronoun "I" was used rather than my clients/users/etc.
so I took this to mean that only "Larry" needed to know how
the process would work -- which included pressing Ctrl+C.

Basically, I only addressed the issue of retrieving clipboard
text into his WSH application.


McKirahan

unread,
Sep 9, 2006, 8:40:12 AM9/9/06
to
"McKirahan" <Ne...@McKirahan.com> wrote in message
news:mfedndGJQ7UsKZ_Y...@comcast.com...

An additional clarification:

The original post asked:

"first I manually select text, it might be in Internet Explorer,
it might be in an e-mail, or in any application, and then I run
a .vbs script the first step of which is to copy the selection
regardless of what application happens to be active. "

Yes, he said "copy the selection" but I took this to mean
"copy the selection" was already copied to the clipboard.

Since multiple applications could be open and each could
have some text "highlighted" I didn't think that copying *to*
the clipboard was the issue. I thought that "Larry" wanted
help copying it *from* the clipboard into his WSH application.


0 new messages