Monday, January 05, 2009

WSH and clipboard access

I did some Windows Script Host programming recently and I was pleasantly surprised by its power, features and flexibility. One thing that I couldn't accomplish was accessing the clipboard from WSH. Digging the internet I found some solutions like this one based on Internet Explorer Automation. There are several problems with this approach as you can read in my article about Internet Explorer Automation: What's wrong with Internet Explorer Automation?

My solution for scripting the clipboard content in WSH is a regular COM object created with VC++ and ATL.

Download full source code and compiled DLL: WSH_clipboard.zip
To install the COM object run register.bat

I found scripting the clipboard useful enough to add this feature to the next release of Twebst Web Automation Library.

8 comments:

Ben Larson said...
This comment has been removed by the author.
Anonymous said...

John G
(Russia, Ekaterinburg)

Excellent Object. Thanks

CodeCentrix said...

You are welcome!

TraumaHound said...

Since the code highlighting isn't supported here, I thought I would link to a post I made at your codeproject.com entry:

http://www.codeproject.com/KB/clipboard/wshclipbrd.aspx?msg=3377847#xx3377847xx

Any advice or perhaps I'm doing something wrong (I'm new at coding so I wouldn't be surprised.) ;)

Cheers,

Anonymous said...

HI CC. thank you for sharing this. I am super-newbie at C(++/#) so you can quickly tell me... does your clipboard handle only teext, or can it handle any clipboard objects, such as a filesystem object (a file, folder, etc)?
Cheers,
TwoHawks

Anonymous said...

To Clarify... my intention is to create a simple script allows me to right-click on a file or folder "shortcut" and copy the target (not the .lnk). Then when the user navigates to the desired directory its a simple native-system right-click paste to copy the shortcut target object to there.
cheers, TwoHawks

CodeCentrix said...

The COM clipboard object I presented here can only handle text. In C++/C# you have specific API to work with clipboard.

http://msdn.microsoft.com/en-us/library/system.windows.forms.clipboard.aspx

twohawks said...

Thank you.