vbScript - Extending Application Functionality with vbScript and AutoIt

Sometimes you'll find that one of your favourite applications is missing some useful functionality. For me, it was FastStone Image Viewer. I've had to scan a large number of family photos to convert everything to digital. Rather than scan one photo at a time, I found it faster to fill the scanner and scan an entire page at once. That reduces the overall scan time but it means that I have to crop and save each individual photo from the gang scans.

FastStone has a crop board where I can select an area and Lossless Crop To File. Unfortunately, it autogenerates the file name by appending _cr to the current file name, so each succeeding crop to file overwrites the previous.

Using a free third party package, AutoIt and vbScript I can add the functionality.

The first step is to run the included tool, AutoIt Windows Info. Once this is running I then run FastStone, invoke the crop board, select an area, and choose Lossless Crop to File. I then select the control which contains the file name. The AutoIt tool tells me that the control name is Edit1.

Now, using the AutoItX component of AutoIt I can create a vbScript module that will run continuously in the background (but only when I need to do a lot of cropping). This script will continuously loop and watch for the creation of a window with the title Lossless Crop to File. It will then calculate a new file name for the cropped portion and save it. The script will then go into a pseudo wait state (using sleeps) until the next crop request appears.

This is not nearly as elegant as coding with REXX ports on the old Amiga (they were awesome) but it is still highly useful.

Note that sleep states and loops may be required to wait for controls to respond to commands.

Using the same technique I was able to automate the optimum pension split calculation in the program I use to file my income tax, a very tedious task if done manually.

folder = "d:\my\Pictures\.Scans\newscans\" 'keep this as the first line in the file

''#region Persistent fold region                                                        '
''                                                                                      '
''  Name:                                                                               '
''                                                                                      '
''      Autocrop.vbs                                                                    '
''                                                                                      '
''  Description:                                                                        '
''                                                                                      '
''      Run this script if you have to do repeated FastStone "Lossless Crop to File"    '
''      operations. It will automatically generate the next available file-####.jpg     '
''      file name and save the file.                                                    '
''                                                                                      '
''  Requires:                                                                           '
''                                                                                      '
''      AutoItX from https://www.autoitscript.com/site/autoit/                          '
''                                                                                      '
''  Audit:                                                                              '
''                                                                                      '
''      2019-04-03  rj  original code                                                   '
''                                                                                      '
''#endregion                                                                            '
title   = "Lossless Crop to File"       'title of the file save dialog in FastStone     '
control = "Edit1"                       'ID of the file name control in the dialog box  '

'AutoItX consts for WinTitleMatchMode option

Const MatchFromStart    = 1
Const MatchAnySubstring = 2
Const MatchExactString  = 3
Const AdvancedMode      = 4
Const TemporaryFolder   = 2
Const OverWriteFile     = 2

Set fso = CreateObject("Scripting.FileSystemObject")
Set aut = CreateObject("AutoItX3.Control")

aut.Opt "WinTitleMatchMode", MatchExactString

Wscript.Echo "Waiting for",title,"dialog"

While True

    If aut.WinExists(title) Then
        Wscript.Echo vbcrlf & "Found",title
        'wait until the file name has been copied to the filename control
        Do While aut.ControlGetText(title,"",control) = ""
            aut.Sleep 100

        '1 Get the default crop file name
        '2 Get the type of file from the file extension
        '3 Strip off everything but the base file name

        file = aut.ControlGetText(title,"",control)
        extn = fso.GetExtensionName(file) 
        base = Left(file,InstrRev(file,"_cr.")-1)

        'Look for the first available file-#### file in the current folder and save

        indx = 0

            indx = indx + 1
            file = base & "-" & Pad(indx) & "." & extn
        Loop Until Not fso.FileExists(file)

        'Copy the new name to the control on the dialog box

        Do Until aut.ControlGetText(title,"",control) = file
            aut.ControlSetText title, "", control, file
            aut.Sleep 1000

        'Save the image

        Wscript.Echo "Saving",file
        aut.ControlSend title, "", control, "{ENTER}"
        'wait for the file save dialog to close    
        Do While aut.WinExists(title)
            aut.Sleep 1000

    End If

    'wait for the next crop action

    Wscript.Sleep 1000


Function pad (val)
    pad = Right("0000" & val,4)
End Function
About the Author

I completed my Computer Science degree at the University of Manitoba in 1976. I did two and a half years of programming in medical research followed by twenty-nine years at Manitoba Hydro (electric utility). Most of that was spent on doing development and maintenance on an AGC/SCADA (real-time programming/process control) system. The last ten years of that was spent doing application and infrastructure support and development. I have programmed in FORTRAN (mostly), APL, PL/1, COBOL, Lisp, SNOBOL, ALGOL, Assembler (several flavours), C, C++, Paradox, VB, vbScript and more recently, Python. I am married with two grown children of whom I am very proud, and a most beautiful wife. I am currently retired (and loving it).