Saturday, February 13, 2010

Taking Screenshots Using VBScript & Hotkey Combination

Make sure you save the code file with .vbs extension. I saved it as ss.vbs on the c:\ drive.

Also make sure that the application (like browser, any file etc.) whose screenshot you want to take is open.

Now go to the command prompt and run the file (.vbs file) by typing its name on the command prompt and hitting enter.

As soon as you hit enter to run this code, the below message box will show up and all the opened applications will be minimized.

Just click on Ok for this message box and immediately maximize the application whose screenshot you want to take. Because when you click Ok it waits for few seconds for you to maximize your application. It then takes the screenshot and saves it on the C:\ drive. After saving the screenshot it will show you the below message box.

It will repeat the above steps once more (I have configured the below code to run 2 times using For loop). You can always modify the script to run it only once or some more repetitions as per your needs.

We will also see how to run this script each time with hotkey combination.

WSH_OBJ Shell Object - Provides access to the native Windows shell. You create a WSH_OBJ Shell object whenever you want to run a program locally, manipulate the contents of the registry, create a shortcut, or access a system folder. The WSH_OBJ Shell object provides the Environment collection.

set WSH_OBJ = CreateObject("WScript.Shell")

for i= 1 to 2

below we are creating a shell object. You can use these objects to access many of the Shell's features and dialog boxes. For example, you can access the file system, launch programs, and change system settings. We are minimizing all open windows.

set a_shll=createobject("shell.application")

msgbox "Select application whose screenshot you want to take "

below 2000 indicates the interval (in milliseconds) you want the script process to be inactive. In this below time you can just activate your application whose screenshot you want to take.

WScript.Sleep 2000

Through object linking and embedding (OLE) Automation, Word provides another application (called the "container" application) with a programmable object . Word supports a single object called "Basic" for OLE Automation. You use the "Basic" object to send WordBasic instructions to Word. WordBasic instructions can return numbers or strings directly to the container application.

Set OBJ_WB = CreateObject("Word.Basic")

OBJ_WB.SendKeys "{prtsc}"
OBJ_WB.AppClose "Microsoft Word"
Set OBJ_WB = Nothing
Set a_shll=Nothing

Open MSPaint in maximize mode
WSH_OBJ.Run "mspaint",3
WScript.Sleep 500

Activate MSPaint
WSH_OBJ.AppActivate "untitled - Paint"
WScript.Sleep 1000

Paste the Screenshot
WSH_OBJ.SendKeys "^v"
WScript.Sleep 200

Save MSPaint file
WSH_OBJ.SendKeys "^s"

WSH_OBJ.SendKeys "c:\test" & i & ".bmp"

WScript.Sleep 500
WSH_OBJ.SendKeys "{ENTER}"
msgbox "Document " & i & " saved"


Set WSH_OBJ=Nothing

Part 2 (assigning HotKey combination to the above script)

Now for example let's say that the above code file is named as ss.vbs.
We will create another vbs file named let's say a.vbs and type the below code in it:

Set sh = CreateObject("WScript.Shell")
str_dsk_top = sh.SpecialFolders("Desktop")
Set shortcut = sh.CreateShortcut(str_dsk_top & "\shortcut.lnk")
shortcut.TargetPath = "C:\ss.vbs"
shortcut.hotkey = "CTRL+SHIFT+V"

Now go to command prompt and type:

Cscript a.vbs

It will make a shortcut of ss.vbs file on the desktop and now you will be able to take screenshots by just pressing the CTRL+SHIFT+V as we passed it to the hotkey in our code and run the print-screen script with shortcut key. You can use any other hotkey combination also.

Update: The above script is saving the screenshots on the c:\ drive. By combining this script with the above scripts, you can make sure that by pressing a simple Hotkey combination you can save your screenshots in a word file itself.