Windows Automation Forth Programming

Windows Api Automation using the Forth Programming Language. (Win32).

This tutorial I’ll show how to automate basic Windows tasks such as moving the mouse around the screen and automating button clicks using SwiftForth.

Source code used in the video:

FUNCTION: mouse_event ( a b c d e – x )
FUNCTION: SetCursorPos ( a b – x )

1502 604 SetCursorPos

mousepos

: click
2 0 0 0 0 mouse_event drop
4 0 0 0 0 mouse_event drop ;

: test s" calc" >shell 1000 ms
1463 633 SetCursorPos 200 ms click 200 ms
1776 627 SetCursorPos 100 ms click 1000 ms
1584 645 SetCursorPos 100 ms click 700 ms
1777 687 SetCursorPos 100 ms click ;

How >SHELL works

In forth its common to see “>”. You can think of this as “to” or “out.”

>SHELL can be thought of as, “to shell” or “to the command prompt.”

>SHELL is a very useful word if you want to use the windows command prompt.

For example, in windows if you want to open the microsoft calculator, open a command prompt and type “calc.” This will open the windows calculator.

Windows Calculator

We can do the same thing in SwiftForth by typing:
s" calc" >shell

Bonus: Google source code

This is the code at the end of the video. It lets you type in a search query and will open google and show you the results.

: ggl
pad 100 erase
s" www.google.com/search?q=" pad swap move
pad zcount + 30 accept
drop pad zcount -trailing >shell ;

All we are doing is zero’ing out 100 bytes in a memory space that comes built-in with SwifthForth called, “PAD.” PAD is a scratch pad of memory. We move our strings to this location and do some basic string processing with -TRAILING and send it out to the command prompt.

If you want to keep the command prompt open, then use “cmd /k”.

s" cmd /k echo ‘hello’" >shell