# GUI Automation Tool (Form Filler)

This guide demonstrates how to create a script to automate repetitive GUI tasks using `pyautogui`. The example below is a "Bulk Data Entry" bot that types data into a form automatically.

**Modules Used:**
*   [[programming/python/modules/pyautogui-module|pyautogui]]: For controlling the mouse and keyboard.
*   `time`: To handle delays.

## Installation

```bash
pip install pyautogui
```

## The Code

Save this as `form_filler.py`.

```python
import pyautogui
import time

def auto_fill(data):
    # Safety feature: Drag mouse to any corner to stop the script
    pyautogui.FAILSAFE = True
    
    print("Please open your form/application.")
    print("Move your mouse over the first input field.")
    print("Script starting in 5 seconds...")
    
    # Countdown
    for i in range(5, 0, -1):
        print(f"{i}...", end=" ", flush=True)
        time.sleep(1)
    print("\nStarting!")

    # Click to focus the first field at current mouse position
    pyautogui.click()

    for person in data:
        print(f"Entering data for: {person['name']}")
        
        # Type Name
        pyautogui.write(person['name'], interval=0.1)
        pyautogui.press('tab')
        
        # Type Email
        pyautogui.write(person['email'], interval=0.1)
        pyautogui.press('tab')
        
        # Type Message
        pyautogui.write(person['message'], interval=0.1)
        pyautogui.press('tab')
        
        # Simulate submitting (e.g., pressing Enter)
        pyautogui.press('enter')
        
        # Wait for next entry to load
        time.sleep(2)

if __name__ == "__main__":
    # Dummy data
    users = [
        {"name": "Alice Smith", "email": "alice@example.com", "message": "Hello World"},
        {"name": "Bob Jones", "email": "bob@example.com", "message": "Automated entry"},
        {"name": "Charlie Day", "email": "charlie@example.com", "message": "PyAutoGUI is fun"}
    ]
    
    try:
        auto_fill(users)
        print("Done!")
    except pyautogui.FailSafeException:
        print("\nFail-safe triggered from mouse corner. Script stopped.")
    except KeyboardInterrupt:
        print("\nScript interrupted.")
```

## Usage

1.  Open a text editor or a web form (e.g., a Google Form with Name, Email, Message fields).
2.  Run the script:
    ```bash
    python form_filler.py
    ```
3.  Quickly switch to the target application and hover your mouse over the first text box.
4.  Wait for the countdown to finish.

[[programming/python/python]]