Detect Image Data on Windows Clipboard plus Save and Restore

Technical support and scripting issues

Moderators: JRL, Dorian (MJT support)

User avatar
JRL
Automation Wizard
Posts: 3532
Joined: Mon Jan 10, 2005 6:22 pm
Location: Iowa

Post by JRL » Thu Oct 12, 2006 9:47 pm

Marcus and jpuziano

This post is getting old but I was thinking about a script I want to build and was wondering how or if you have progressed with this issue. Playing around with clipbrd.exe I have found it to be a useful tool for saving and restoring the clipboard. Using it in a script I can take the users clipboard, save it to a file, use the clipboard during the script, then restore the clipboard to its pre-script condition. And I found that the clipbrd.exe program can be run nearly transparently from a script. The caveat here is "nearly transparently".

The following script can run clipbrd.exe invisibly but it cannot run the windows spawned by clipbrd.exe invisibly, they blink momentarily on the screen. This is the puzzle piece that I am curious about.

jpuziano,
Did you find a way to satisfactorily perform this task or did you stop with the ability to warn the users that continuing would kill their clipboard contents?

Does anyone know of a way to run a program invisibly and keep spawned windows invisible as well?

The following script:

- opens clipbrd.exe
- saves the current clipboard contents to a file in the users temp directory
- puts some text into the clipboard
- opens notepad.exe
- pastes the text into notepad (visual confirmation of clipboard contents)
- opens clipbrd.exe again
- retrieves clipboard contents from the file

If you check your clipboard contents before and after running the script, they should be the same.

Most of the waits in this script are there just for ease of watching with the exception being the wait just prior to the PUT> line. If that wait is removed PUT> will actually replace the clipboard contents before the file created by clipbrd.exe is saved. This will then save the phrase "This text is now in the clipboard." as the saved clipboard contents, thus defeating the whole process. There is probably a better method of waiting but I didn't try anything else.

Thanks for any help,
Dick

Code: Select all

Let>Clipfile=%TEMP_DIR%~SaveClipBoard~.clp
IfFileExists>%Clipfile%
  DeleteFile>%Clipfile%
EndIf

//Write the current clipboard contents to a file

Let>RP_WINDOWMODE=0
Run>clipbrd.exe
WaitWindowOpen>Clipbook Viewer
Press Alt
  Send>fa
Release Alt
WaitWindowOpen>Save As

send>%Clipfile%
Press enter
Press Alt
  Send>fx
Release ALT
Wait>1

//Put some text into the clipboard using the PutClipBoard function

              PUT>This text is now in the clipboard.

//Run notepad

Let>RP_WINDOWMODE=1
Run>notepad.exe
WWO>Notepad*
Wait>2

//Paste the clipboard text into notepad 
//proving the script has overwritten the clipboard.

Press ctrl
  send>v
Release ctrl
wait>2
CloseWindow>Notepad*
Wait>1
Send>n
Wait>1

//Read the saved pre-script clipboard contents back to the clipboard

Let>RP_WINDOWMODE=0
Run>clipbrd.exe
WaitWindowOpen>Clipbook Viewer
Press Alt
  Send>fo
Release Alt
WaitWindowOpen>Open
send>%Clipfile%
Press enter
WWO>Clear Clipboard
send>y
Press Alt
  Send>fx
Release ALT

User avatar
jpuziano
Automation Wizard
Posts: 1085
Joined: Sat Oct 30, 2004 12:00 am

Post by jpuziano » Fri Oct 13, 2006 11:11 pm

Hi JRL,

I stopped at just warning them as they would not be able to install something like Irfanview on their locked down machines.

However "clipbrd.exe" is part of Windows and would be on their machines so I tried your script but it wouldn't run on my PC. Does the script you posted run as-is for you? I had to make a few changes to make it run for me, perhaps the clipbrd.exe I'm running on Win XP SP1 is different than the one you're running. Anyway, here's what I ended up with:

Code: Select all

// 2006-10-13 17:28:55 Friday
// JRL's Clipboard Image Save and Restore script
// with mods for clipbrd.exe on Win XP SP1 by jpuziano

Let>SK_DELAY=20

Let>Clipfile=%TEMP_DIR%~SaveClipBoard~.clp
IfFileExists>%Clipfile%
  DeleteFile>%Clipfile%
EndIf

//Write the current clipboard contents to a file

Let>RP_WINDOWMODE=0
//Let>RP_WINDOWMODE=1

Run>clipbrd.exe
WaitWindowOpen>ClipBook Viewer*
Wait>1

//From "Window" main menu, choose "1" to view clipboard instead of clipbook
Press Alt
Send>w
Release Alt
Send>1
Wait>2

//save existing clipboard contents
Press Alt
//  Send>fa
  Send>f
Release Alt
Send>a
WaitWindowOpen>Save As
Wait>1

send>%Clipfile%
Press enter

//Close clipbrd.exe app 
Press Alt
//  Send>fx
  Send>f
Release ALT
  Send>x
Wait>1

//Put some text into the clipboard using the PutClipBoard function

              PUT>This text is now in the clipboard.

//Run notepad

Let>RP_WINDOWMODE=1
Run>notepad.exe
WWO>Notepad*
Wait>2

//Paste the clipboard text into notepad 
//proving the script has overwritten the clipboard.

Press ctrl
  send>v
Release ctrl
wait>2
CloseWindow>Notepad*
Wait>1
Send>n
Wait>1

//Read the saved pre-script clipboard contents back to the clipboard

Let>RP_WINDOWMODE=0
//Let>RP_WINDOWMODE=1
Run>clipbrd.exe
WaitWindowOpen>ClipBook Viewer*
Wait>1

//From "Window" main menu, choose "1" to view clipboard instead of clipbook
Press Alt
Send>w
Release Alt
Send>1
Wait>2

Press Alt
//  Send>fo
  Send>f
Release Alt
  Send>o
WaitWindowOpen>Open
send>%Clipfile%
Press enter
WWO>Clear Clipboard
send>y
Press Alt
//  Send>fx
  Send>f
Release ALT
  Send>x
The following two lines appear in two places. Anyone trying out the above script, I recommend you run it first with...

//Let>RP_WINDOWMODE=0
Let>RP_WINDOWMODE=1

as this will let you see the clipbrd.exe main window. Then uncomment one and comment the other line like this...

Let>RP_WINDOWMODE=0
//Let>RP_WINDOWMODE=1

to see it in "stealth-mode", clipbrd.exe main window is hidden.

Yes JRL, it looks wierd, erie even, to see the Save As dialog pop out of nowhere and menu choices popping out of nowhere... definately not a macro I could give to users. I don't know if child windows (and menu choices) could be made "hidden" as well... anybody else out here know the answer to that one?

I did find another avenue for you to try though... and it should be (if it works) completely behind-the-scenes. Check out ClipboardToFile.exe and FileToClipboard.exe available here (bottom of page). There is also some good info there on how the clipboard works.

The executables there won't work on my XP SP1 machine, gives this error:
This application has failed to start because the application configuration is incorrect. Reinstalling the application may fix this problem.
After Googling for info on that error, this page leads me to believe that they may work fine on Win XP SP2 or other flavors of Windows. Give it a try and let us know what you come up with.
Last edited by jpuziano on Sun Oct 15, 2006 8:23 am, edited 2 times in total.
jpuziano

Note: If anyone else on the planet would find the following useful...
[Open] PlayWav command that plays from embedded script data
...then please add your thoughts/support at the above post - :-)

User avatar
JRL
Automation Wizard
Posts: 3532
Joined: Mon Jan 10, 2005 6:22 pm
Location: Iowa

Post by JRL » Sun Oct 15, 2006 2:14 am

jpuziano,
jpuziano wrote:However "clipbrd.exe" is part of Windows and would be on their machines so I tried your script but it wouldn't run on my PC. Does the script you posted run as-is for you? I had to make a few changes to make it run for me, perhaps the clipbrd.exe I'm running on Win XP SP1 is different than the one you're running. Anyway, here's what I ended up with:
Yes, the script I posted runs fine for me on my XP SP1 computer. I wouldn't have posted it if it didn't. Oddly I did start out with the additions you made and discovered that I didn't need them. I found there can only be one Clipboard window open in the Clipbook Viewer application and selecting save or open saved or opened the one Clipboard whether it was minimized or not.
jpuziano wrote:Yes JRL, it looks wierd, erie even, to see the Save As dialog pop out of nowhere and menu choices popping out of nowhere... definately not a macro I could give to users. I don't know if child windows (and menu choices) could be made "hidden" as well... anybody else out here know the answer to that one?
I think if this issue could be resolved the clipbrd application would be the perfect portable solution for saving and retrieving clipboard information. I'm not sure of the precise functionality but clipbrd.exe has been around since Windows 3.0.

I tried making the child windows stay invisible by changing the WF_TYPE settings,I didn't have any luck with that. Perhaps there is a VB solution for this?

I'm curious as to what effect is taking place with apps when we run them using RP_WINDOWMODE=0. If you insert the lines:
GetActiveWindow>Clipbook Viewer,X,Y
MDL>%X%,%Y%

right after the app has been started using RP_WINDOWMODE=0. You'll see that the system thinks the window is right there in view. I was suspecting that the window would be open and would be sitting at a location something like -10000,-10000. but that's not the case.
jpuziano wrote:Check out ClipboardToFile.exe and FileToClipboard.exe
I could but would rather not use these two apps. For me on my computer I can live with the flashing windows. If they aren't working for you on your Win XP SP1 computer, that is a bright red blinking warning beacon in my mind.

Later,
Dick

User avatar
jpuziano
Automation Wizard
Posts: 1085
Joined: Sat Oct 30, 2004 12:00 am

Post by jpuziano » Sun Oct 15, 2006 8:21 am

JRL wrote:
jpuziano wrote:Check out ClipboardToFile.exe and FileToClipboard.exe
I could but would rather not use these two apps. For me on my computer I can live with the flashing windows. If they aren't working for you on your Win XP SP1 computer, that is a bright red blinking warning beacon in my mind.
Hi JRL,

As it happens, the red blinking beacon, once I figured it out, was actually saying... "Install the free MS Visual C++ Runtime available here" and once I did, ClipboardToFile.exe and FileToClipboard.exe worked perfectly.

The Microsoft Visual C++ 2005 Redistributable Package (x86) installs runtime components of Visual C++ Libraries required to run applications developed with Visual C++ on a computer that does not have Visual C++ 2005 installed.

Note that the C++ source code for these is also freely available. :wink:

The advantages here are speed and no blinking or flashing of any kind. Here's what I came up with...

Code: Select all

//2006-10-15 00:46:16 Sunday jpuziano
//Clipboard Image Save/Restore via ClipboardToFile.exe and FileToClipboard.exe

IfDirExists>%TEMP_DIR%ClipBoardData,DirExists
CreateDir>%TEMP_DIR%ClipBoardData

Label>DirExists
//Delete all saved clipboard files from last run
DeleteFile>%TEMP_DIR%ClipBoardData\*.*

//Save clipboard data
Let>RP_WAIT=1
Let>RP_WINDOWMODE=0
Run>e:\ClipboardToFile.exe %TEMP_DIR%ClipBoardData\
Let>RP_WAIT=0
Let>RP_WINDOWMODE=1

//Put some text into the clipboard using the PutClipBoard function
PUT>This text is now in the clipboard.

Run>notepad.exe
WaitWindowOpen>Untitled - Notepad
Wait>1

//Paste the clipboard text into notepad
//proving the script has overwritten the clipboard.
Press ctrl
send>v
Release ctrl
wait>2
CloseWindow>Untitled - Notepad
Wait>1
Send>n
Wait>1

//Restore Clipboard Data - Only restoring 5 data formats below.
//If you need to restore more, add another section for each additional format.
//To discover the format names, look in the %TEMP_DIR%ClipBoardData\ directory
//for all formats saved from the clipboard when you last ran this macro.

Let>RP_WAIT=1
Let>RP_WINDOWMODE=0

IfFileExists>%TEMP_DIR%ClipBoardData\CF_DIBV5
   Run>e:\FileToClipboard.exe %TEMP_DIR%ClipBoardData\CF_DIBV5 CF_DIBV5
Else
Endif

IfFileExists>%TEMP_DIR%ClipBoardData\CF_DIB
   Run>e:\FileToClipboard.exe %TEMP_DIR%ClipBoardData\CF_DIB CF_DIB
Else
Endif

IfFileExists>%TEMP_DIR%ClipBoardData\CF_BITMAP
   Run>e:\FileToClipboard.exe %TEMP_DIR%ClipBoardData\CF_BITMAP CF_BITMAP
Else
Endif

IfFileExists>%TEMP_DIR%ClipBoardData\CF_TEXT
   Run>e:\FileToClipboard.exe %TEMP_DIR%ClipBoardData\CF_TEXT CF_TEXT
Else
Endif

IfFileExists>%TEMP_DIR%ClipBoardData\CF_OEMTEXT
   Run>e:\FileToClipboard.exe %TEMP_DIR%ClipBoardData\CF_OEMTEXT CF_OEMTEXT
Else
Endif
Unfortunately, for deploying to a large number of users, they'd have to get that C++ runtime installed and they are still locked down... so I took another look at your original script and tried to eliminate as much of the flashing as possible and here's the result of that effort (not perfect but I think I reduced flashing quite a bit)...

Code: Select all

//2006-10-15 02:29:32 Sunday
//JRL's Clipboard Image Save and Restore script with mods by jpuziano

Let>SK_DELAY=10

Let>Clipfile=%TEMP_DIR%~SaveClipBoard~.clp
IfFileExists>%Clipfile%
DeleteFile>%Clipfile%
EndIf

//Write the current clipboard contents to a file

Let>RP_WINDOWMODE=0
//Let>RP_WINDOWMODE=1

Run>clipbrd.exe
Let>WIN_USEHANDLE=0
WaitWindowOpen>ClipBook Viewer*
MoveWindow>ClipBook Viewer*,2000,1000
SetFocus>ClipBook Viewer*
Wait>1

//From "Window" main menu, choose "1" to view clipboard instead of clipbook
Press Alt
Send>w
Release Alt
Send>1
Wait>0.5

//save existing clipboard contents
Press Alt
// Send>fa
Send>f
Release Alt
Send>a
WaitWindowOpen>Save As
Let>WIN_USEHANDLE=1
FindWindowWithText>NT Clipboard File (*.CLP),1,window_handle
MoveWindow>window_handle,2000,1000
Wait>0.5

send>%Clipfile%
Press enter

//Close clipbrd.exe app
Press Alt
// Send>fx
Send>f
Release ALT
Send>x
Wait>1

//Put some text into the clipboard using the PutClipBoard function

PUT>This text is now in the clipboard.

Let>RP_WINDOWMODE=1
Run>notepad.exe

Let>WIN_USEHANDLE=0
WWO>Untitled - Notepad
Wait>1

//Paste the clipboard text into notepad
//proving the script has overwritten the clipboard.

Press ctrl
send>v
Release ctrl
wait>2
CloseWindow>Untitled - Notepad
Wait>1
Send>n
Wait>1


//Read the saved pre-script clipboard contents back to the clipboard

Let>RP_WINDOWMODE=0
//Let>RP_WINDOWMODE=1

Run>clipbrd.exe
Let>WIN_USEHANDLE=0
WaitWindowOpen>ClipBook Viewer*
MoveWindow>ClipBook Viewer*,2000,1000
SetFocus>ClipBook Viewer*
Wait>1

//From "Window" main menu, choose "1" to view clipboard instead of clipbook
Press Alt
Send>w
Release Alt
Send>1
Wait>2

Press Alt
// Send>fo
Send>f
Release Alt
Send>o

Let>WIN_USEHANDLE=0
WaitWindowOpen>Open
Let>WIN_USEHANDLE=1
FindWindowWithText>NT Clipboard File (*.CLP),1,window_handle
MoveWindow>window_handle,2000,1000

send>%Clipfile%
Press enter
Let>WIN_USEHANDLE=0
WaitWindowOpen>Clear Clipboard
//Let>WIN_USEHANDLE=1
//FindWindowWithText>Do you want to clear the contents of the Clipboard?,1,window_handle
//MoveWindow>window_handle,2000,1000
send>y
Press Alt
// Send>fx
Send>f
Release ALT
Send>x
Menu dropdown flashing was eliminated by re-positioning the main window way off-screen. The Open and Save As dialogs were a problem as even though the main window was off-screen, the dialogs always came up where you could see them. I used FindWindowWithText> to get the handle of the dialogs and repositioned them off screen as well. For the Clear Clipboard dialog, it turned out to be less flashing by just quickly sending a "y".

If anyone knows of any VB script or Win32 API calls to make all child windows of a main window appear hidden as well, please let us know.

Beyond the above two methods, I'd still like to see (one day) commands added to Macro Scheduler that could accomplish this, something like this perhaps:
jpuziano wrote:TakeClipBoardSnapShot>
- writes to a structure in memory (or disk?) created by Macro Scheduler
- must be called before RestoreClipBoardfromSnapShot

RestoreClipBoardfromSnapShot>
- reads from a structure in memory (or disk?) created by Macro Scheduler
...but I know the Windows Clipboard is a complex thing and there are many more mainstream things to work on with the beta etc., so I know something like this might be a long time coming. Until then, at least we have the methods worked out so far.

If anyone has any more ideas on this, please jump in.

Hope this was of some help JRL, always enjoy your posts, take care...
jpuziano

Note: If anyone else on the planet would find the following useful...
[Open] PlayWav command that plays from embedded script data
...then please add your thoughts/support at the above post - :-)

Post Reply
Sign up to our newsletter for free automation tips, tricks & discounts