Welcome Guest! You need to login or register to make posts.



Go to last post Go to first unread
#1 Posted : Sunday, March 14, 2004 5:29:00 PM(UTC)

Rank: Advanced Member

Groups: Member, Administration
Joined: 8/2/2003(UTC)
Posts: 876

Thanks: 2 times
Was thanked: 27 time(s) in 27 post(s)
Hello everybody,

It is pretty convenient to use WSH applications for batch processing on local workstation. For example if you wish to generate thumbnails for a folder of photos or put some text on it. If you run it from command prompt and pass command-line arguments to the script, it becomes rather powerful and flexible tool. Here is posted small tutorial how to use Graphics Mill in WSH scripts.
I assume you are familiar to VBScript.

In the first sample we create batch script which generates thumbnails for specified folder.

WSH applications can be divided into ones which can run from the command prompt only (using cscript.exe) and which can run also from Windows (using wscript.exe). This script requires command prompt.

Create file batch.wsf and insert the code below. Run it from command prompt in this way:

cscript batch.wsf BatchExample [path to source files folder] [path to the destination folder]

<job id="BatchExample">
<script language="VBScript">

' Check if arguments are passed. This script expects that you pass two arguments:
' path to folder with files you want to process and path to folder where to store 
' result. Note, when we pass two arguments, Count property contains 3, because 
' the first argument contains script name.
If WScript.Arguments.Count   3 Then
	WScript.Echo "Arguments count " & WScript.Arguments.Count  & vbNewLine & "Usage: Batch.wsf BatchExample [input images folder path] [output images folder path]"
End If

Dim objFSO
Dim objInputFolder
Dim objFile

' The Arguments collection is zero-based. The second argument 
' (next to script name) contains the input path.
Set objFSO = WScript.CreateObject("Scripting.FileSystemObject")
Set objInputFolder = objFSO.GetFolder(WScript.Arguments(1))

Dim I
Dim objBitmap
Set objBitmap = WScript.CreateObject("GraphicsMill.Bitmap")

For Each objFile in objInputFolder.Files

	' As potentially the folder may contain non-images, we should disable
	' break on error.
	On Error Resume Next
	objBitmap.LoadFromFile objFile.Path

	' Error check is made in this way
	If Err = 0 Then
		objBitmap.Transforms.Resize 320
		objBitmap.SaveToFile WScript.Arguments(2) & "\" & objFile.Name
		PrintMessage "File " & objFile.Name & " is processed."
		PrintMessage "File " & objFile.Name & " loading failed: " & Err.Description
	End If 

' Due using StdOut, you can run this script from command line only (cscript.exe). 
' If you would like to customize it for wscript.exe, just modify this function
' to write the string to log file or whatever.
Sub PrintMessage(strMessage)
	WScript.StdOut.WriteLine strMessage
End Sub


#2 Posted : Sunday, March 14, 2004 5:40:00 PM(UTC)

Rank: Advanced Member

Groups: Member, Administration
Joined: 8/2/2003(UTC)
Posts: 876

Thanks: 2 times
Was thanked: 27 time(s) in 27 post(s)
The next code sample concerns the specifics of using ActiveX components in WSH scripts. The following things are reviewed here:
  1. Binding type library to the script. It allows using constants and enumeration values in your code making it much more readable and understandable
  2. Event handing.
To add reference to the type library, you should use <reference/> tag in the beginning of the file. Using attribute guid specify type library GUID. Note, it is essential to work with .wsf files to get it working. You cannot use it in .vbs or .js.

The simplest way to add event handler, is to use optional parameter of CreateObject method to specify the prefix of the handler. After this, create the function or sub which has the same prototype as the required event with appended prefix.

The following code samples generates empty image and writes a text to it. After this, it saves this image to PNG file and display message after image is saved. You can load it both from the command prompt and Windows.

<job id="Example">
<reference guid="{3CE48541-DE7A-4909-9314-9D0ED0D1CA5A}"/>
<script language="VBScript">

Dim objBitmap

' Second parameter is optional. Use it to create event handler for 
' Bitmap object.
Set objBitmap = WScript.CreateObject("GraphicsMill.Bitmap", "Bitmap_")

' As you specified GUID of type library (<reference tag>), you can use
' color constants...
objBitmap.CreateNew 400, 100, Format24bppRgb, ColorChocolate 

' ...and enumerations
objBitmap.Graphics.TextFormat.HorizontalAlignment = HorizontalAlignmentCenter
objBitmap.Graphics.TextFormat.VerticalAlignment = VerticalAlignmentCenter
objBitmap.Graphics.TextFormat.FontColor = ColorPaleGreen
objBitmap.Graphics.TextFormat.FontSize = 32

objBitmap.Graphics.DrawText "Aurigma Graphics Mill 2.0",objBitmap.Data.Width/2, objBitmap.Data.Height/2
objBitmap.SaveToFile "text.png"

' Example of event handler. Just append the prefix you specified in
' CreateObject method and provide the proper signature (see the documentation
' for Graphics Mill)
Sub Bitmap_AfterSave
	WScript.Echo "File saved successfully."
End Sub


Edited by user Monday, December 24, 2007 6:11:54 PM(UTC)  | Reason: Not specified

Users browsing this topic
Forum Jump  
You cannot post new topics in this forum.
You cannot reply to topics in this forum.
You cannot delete your posts in this forum.
You cannot edit your posts in this forum.
You cannot create polls in this forum.
You cannot vote in polls in this forum.