Monday, December 28, 2009

Easy ScreenShot using Java and VBS

This Script will take the screen shots and will save it in a folder named with System Date. It will also amend the screen shot to a Word File named as System Date.

Procedure::

Step 1:
Save the following Java code in ScreenImage.java file:

/***Java Codes Starts Here+++ */
import java.awt.image.BufferedImage;
import java.awt.AWTException;
import java.io.IOException;
import java.awt.Dimension;
import java.awt.Toolkit;
import java.awt.Rectangle;
import javax.imageio.ImageIO;
import java.awt.Robot;
import java.io.File;
import java.text.DateFormat;
import java.util.Date;
import java.text.SimpleDateFormat;

public class ScreenImage
{
     /**
     *  Convenience method to create a BufferedImage of the desktop
     *
     *  @param     fileName name of file to be created or null
     *  @return    image the image for the given region
     *  @exception AWTException see Robot class constructors
     *  @exception IOException if an error occurs during writing
     */
    public static BufferedImage createDesktopImage(String fileName)
        throws AWTException, IOException
    {
        Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
        Rectangle region = new Rectangle(0, 0, d.width, d.height);
        return ScreenImage.createImage(region, fileName);
    }

    /**
     *  Create a BufferedImage from a rectangular region on the screen.
     *
     *  @param     region region on the screen to create image from
     *  @param     fileName name of file to be created or null
     *  @return    image the image for the given region
     *  @exception AWTException see Robot class constructors
     *  @exception IOException if an error occurs during writing
     */
    public static BufferedImage createImage(Rectangle region, String fileName)
        throws AWTException, IOException
    {
        BufferedImage image = new Robot().createScreenCapture( region );
        ScreenImage.writeImage(image, fileName);
        return image;
    }

    /**
     *  Write a BufferedImage to a File.
     *
     *  @param     image image to be written
     *  @param     fileName name of file to be created
     *  @exception IOException if an error occurs during writing
    */
    public static void writeImage(BufferedImage image, String fileName)
        throws IOException
    {
        if (fileName == null) return;

        int offset = fileName.lastIndexOf( "." );
        String type = offset == -1 ? "jpg" : fileName.substring(offset + 1);

        ImageIO.write(image, type, new File( fileName ));
    }
  
    public static void main(String args[])
        throws Exception
    {
        String path =new java.io.File(".").getCanonicalPath();
        DateFormat dateFormat = new SimpleDateFormat("dd-MM-yyyy");
        Date date = new Date();
        String folderName = dateFormat.format(date);
        File folder = new File(path + "\\" + folderName);
        if (!folder.exists())
        {
            folder.mkdir();
        }
      
        path = path + "\\" + folderName;
        folder = new File(path);
        File[] listOfFiles = folder.listFiles();
  
        int imageNo = listOfFiles.length + 1;
        ScreenImage.createDesktopImage( path + "\\" + imageNo + ".jpg" );
    }
}

/***Java Code Ends Here--- */

Step 2:
Compile the Java Code in the same directory as ScreenImage.java file.

Step 3:
Save the following code as Take Screenshot.vbs in the same directory of the class file:

***VBS Code Starts Here+++
Const END_OF_STORY = 6
Const MOVE_SELECTION = 0

dd = DatePart("d",Date)
mm = DatePart("m",Date)
yy = DatePart("YYYY",Date)

If dd < 10 Then
    dd = "0" & dd
End If

If mm < 10 Then
    mm = "0" & mm
End If

today = dd & "-" & mm & "-" & yy

Set fso = CreateObject("Scripting.FileSystemObject")
Path= fso.GetParentFolderName(wscript.ScriptFullName)

Set WshShell = CreateObject("WScript.Shell")
Set WshSystemEnv = WshShell.Environment("USER")
WshSystemEnv("CLASSPATH") = Path

set objShell = wscript.createObject("wscript.shell")

'objShell.Run "cmd /c cd " & Path & " & javac ScreenImage.java", 0 , True

objShell.Run "cmd /C java ScreenImage",0,True

docFile = Path & "\" & today & ".doc"

imagePath = Path & "\" & today & "\"

fileCount=0

For Each file In fso.GetFolder(imagePath).Files
    fileCount=fileCount+1
Next

image = imagePath & fileCount & ".jpg"

If fso.FileExists (docFile) Then
    Set objWord = CreateObject("Word.Application")
    objWord.Visible = False
    Set objDoc = objword.Documents.Open(docFile)
    Set objSelection = objWord.Selection
    objSelection.EndKey END_OF_STORY, MOVE_SELECTION
    Set colShapes = objDoc.Shapes
    Set objShape = objSelection.InlineShapes.AddPicture(image)
    objDoc.Save
Else
    Set objWord = CreateObject("Word.Application")
    objWord.Visible = False
    Set objDoc = objWord.Documents.Add()
    Set objSelection = objWord.Selection
    objSelection.EndKey END_OF_STORY, MOVE_SELECTION
    Set colShapes = objDoc.Shapes
    Set objShape = objSelection.InlineShapes.AddPicture(image)
    objDoc.SaveAs(docFile)
End If

objWord.Quit

Msgbox "Done !!!"
***VBS Code Ends Here---

Step 4:
Add the Take Screenshot.vbs shortcut to Quick Launch toolbar of Windows and Start Clicking....

No comments:

Post a Comment