vbScript - Extending Application Functionality with vbScript and AutoIt

Reverend Jim 1 Tallied Votes 3K Views Share

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
        Loop

        '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

        Do 
            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
        Loop

        '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
        Loop

    End If

    'wait for the next crop action

    Wscript.Sleep 1000

Wend

Function pad (val)
    pad = Right("0000" & val,4)
End Function
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.