Automatic images resizing sample

The MouseRobot contains all type of tools that you need to automate even the most complicated tasks such as images and data processing.

Consider ways to use loops, conditions and variables, work with files in MouseRobot in the photos resizing automation sample.

Modern cameras create images with high resolution and, accordingly, with a large file size. We often encounter the problem of reducing the size of photos, for transmission them, for example, via the Internet.

So, there is a folder with photos. It's necessary to reduce the size of photos in 2 times for each of the dimensions.

Lets divide the problem into two steps:

1. First, we will request the folder name through dialog. Since in most cases need to make a copy of photos, we will add to the dialog check-box titled "Make a copy of the photos". Lets call this step as "Collecting of an information".

2. Then we'll reduce the size of each photo using Microsoft Paint in the cycle. We use this program, because it's included to the Windows, you can replace it by any other program. Lets call this step as "Image processing".

Collecting of an information

1. Create a new task in MouseRobot.

2. We use the step "Dialog (input and display data)" to input data. You can add this step by double-clicking or dragging it from the "Toolbox" to the steps window.

Change the dialog caption to "Photo resizing" and dialog text to "Select a folder with photos".

3. Add a new "Directory selection field" to the dialog by pressing the "+ Field" button. New field properties window will open:

  • Set "Directory selection field" in the field "Type"
  • Set a caption in "Field caption", for example, "Select a folder"
  • Set "{folder}" in the "Variable". It will be variable, which will save the value of the our field

IMPORTANT NOTE! Variables should be surrounded by curving braces ("{ }").

4.Press the button "+Field" to add to check-box "Make a copy of the photos" to the dialog:

  • Set "Check box" in the field "Type"
  • Set "Make a copy of the photo" in "Field caption"
  • Set "{make_copy}" in the "Variable"
  • Set "True" in the "Default value". That means the check box will be switched on when dialog opens

IMPORTANT NOTE! The variable "{make_copy}" is set to "True" if the "Make a copy of the photo" is checked and "False" if the "Make a copy of the photo" is clear.

5. Copies of photos will be stored in the subdirectory "Resize" of selected directory. So, let's create this directory if the "Make a copy of the photo" is checked.

To do this, insert step "Condition (if.. else)" to the task.

To test if the "Make a copy of the photos" is checked, check the value of variable "{make_copy}".

In "Condition (if.. else)" step properties:

  • Select "{make_copy}" in the "First variable" field
  • Select "equal" in the "Condition"
  • Set "True" in the "Second variable" field

6. Now add a "Create Directory" snippet (located in the "Files and Directories" in the toolbox) into the block "If".

  • Set "{folder}\resize" in the "Parameters" field
  • IMPORTANT NOTE! Enclose file and directory names in double quotes (" "), as they may contain spaces. Spaces in file names are not allowed in the performance of Windows command.

  • Check the "Wait for termination"
  • IMPORTANT NOTE! For some tasks, such as deleting or copying a directory may take some time. If not checked, "Wait for termination", the specified operation will be carried out simultaneously with the task MouseRobot. This can lead to errors in execution.

7. Test created macros by pressing the "Run" button.

MouseRobot should display on the screen such dialog:

Select a folder with photos and click "OK".

If the "Make a copy of the photo" was checked, you should see a new subfolder "Resize" in selected folder.

Image processing

Now we have the directory with photos. Photos will be processed in a loop.

There are the step "Loop (repetition of actions)" and the snippets based on it in MouseRobot:

  • Loops through objects on the screen
  • Files loops - "Files loop" and "Files loop (including files in subdirectories)"

"Files loop" is best suited for our task, add it to the task by double-clicking on it.

  • Write "{folder}" instead of the directory "c: \ *.* " in "Loop through" field - "{FileList ({folder})}"
  • The name of the processed file will be kept in the "{LoopFileName}" variable

Next steps will be added into the block "Loop" ... "End Loop".

1. Open the photo file "{LoopFileName}" in the graphic editor Paint by using step, "Windows command".

Paint like the most of programs opens a file passed to it as a parameter for editing. We will pass "{LoopFileName}" as parameter.

Add step "Windows command" into the loop.

  • Write "mspaint" into the "Command object or a file" field. It's a fast start command for editor Paint
  • Write "{LoopFileName}" into the "Parameters" fields

IMPORTANT NOTE! Don't check "wait for termination", because otherwise this step will be expected Paint termination.

2. Often it takes some time to launch applications or open a dialog takes. Therefore make sure that the necessary application or window is already present on the screen to avoid premature implementation of next steps.

  • Run the Paint, for example, from the Start menu
  • Add a step "Search object on screen" into the loop and select title of Paint window by pressing "CTRL"
  • Set "Step timeout" on the "Common" tab, because the window may appear on the screen with delay (depending on the workload of your computer)
  • IMPORTANT NOTE! "Step timeout" is the maximum time during which MouseRobot will attempt to execute a step. In our case MouseRobot will try to find the object during 10 seconds. However, as soon as the object appears on the screen, the step will be executed. If window does not appear within 10 seconds, the task will be stopped.

  • Make blank the "Name", "value" and "Text" of the objects on "Basic" tab.
  • IMPORTANT NOTE! The specified window has the title "Untitled - Paint". But while opening the photos titles will be different. Make the window titles blank to not use them while looking for an object.

    Open by double-clicking on an object its properties and make blank fields "Name", "Value" and "Text" for each of the objects.

3. Now we need to resize photos.

There is a tool Resize in Paint, which can be caused by a keyboard shortcut "CTRL + w". Most of programs show hints with description of their commands and hot keys.

IMPORTANT NOTE! Using hotkeys makes macro more flexible and reduces dependence on the version of automating program. Therefore, we recommend you to use them in your tasks.

Add the step "Mouse and keyboard" into the loop and write "{CTRL}w{/ CTRL}" in the "Send input" field.

4. Previous step opens a dialog box "Resize". Similarly, with the Paint main window, a dialog may not appear immediately. Therefore check its availability before attempting to fill his field.

  • Open dialog "Resize" in Paint
  • Add step "Search object on the screen" into the loop
  • Set as an object the title of dialog Resize by pressing "CTRL"
  • Increase "Step timeout" up to 10 seconds

5. Set the horizontal scale "50" percents, the vertical scale will established automatically if the checkbox "Maintain aspect ratio" is checked. We do this by using the keyboard.

  • It's enough to press "Tab" to focus the "Horizontal" field
  • Text in the field "Horizontal" is automatically marked on focus, so enter "50" to set up a new value
  • "OK" button is the default button (it lights up differently than the other buttons), this means that the "OK" button will be pressed if you press "Enter"

Thus, to perform the necessary steps press "TAB", "50", "ENTER" in the "Resize" dialog.

Insert the step "Mouse and keyboard" to the end of the loop and write "{Tab}50{Enter}" in the "Sent input" field.

The picture size will be changed after this step.

6. Now we should save the photo. Depending on the value of variable "{make_copy}", which contains the value of the "Make a copy of the photos" check box, we should:

  • save the photo with the same file name
  • save the photo with the same file name in the folder "Resize"

Insert the "Condotion (if..else)" step to the end of the loop to check the value of "{make_copy}".

7.We will save a file in the folder "Resize" in the block "If".

  • Call dialog "Save as ..."
  • Similarly the other windows wait it appearance on the screen using the step "Search object on the screen"
  • Save the photo in the "Resize" folder

You can call dialogue "Save as ..." by pressing the "F12"

Insert step "Mouse and keyboard" into the block "If" and write "{F12}" to the "Send input"

8. Find the dialog "Save as ..." with a step "Search object on the screen" to ensure that it presents on the screen.

  • Open the dialog "Save as ..." in Paint
  • Add step "Search object on the screen" to the end of the block "If"
  • Choose the "Save as ..." dialog title by pressing "CTRL"
  • Increase "Step timeout" up to 10 seconds

9. When a dialog "Save as ..." opens input focus is set on the field "File Name", and the file name is allocated. So, we need only enter a filename with "Resize" and press "Enter".

The loop variable "{LoopFileName}" contains the full path of the file. To get out the file name from it use the built-in function "{StrFileName ({LoopFileName})}", which returns the file name.

The new filename is "Resize\{StrFileName ({LoopFileName})}".

Add step "Mouse and Keyboard" at the end of the block "If" and write to the "Send Input" field "Resize\{StrFileName({LoopFileName})}{Enter}".

10. Save the file with the same name in the block "Else". In most programs (including Paint) the key combination "CTRL+S" is used to save a file.

Add into block "Else" step "Mouse and Keyboard" and introduce "{CTRL}s{/ CTRL}".

11. Finally, after transforming and saving the file close the application Paint.

We use common Windows hot keys "ALT + F4" to close the program.

Since it takes some time for closing the program, set a small delay after pressing the keys.

  • Add "Mouse and keyboard" step to the end of the loop and type "{ALT}{F4}{/ALT}" in the "Send input" field
  • Set the "Pause after step" on the "Common" tab

Run macro

IMPORTANT NOTE! Close all open Paint windows before you run the macro.

Run the macro by pressing the "Run" button.

© AutomationBox 2005-2011