[파이썬] pyautogui 유저의 도움 없는 부팅시 자동 실행

Have you ever wanted to automate certain tasks on your computer as soon as you start it up, without requiring any user input? In this blog post, we will explore how to achieve this using the pyautogui library in Python.

What is pyautogui?

pyautogui is a Python library that allows you to programmatically control the mouse and keyboard to automate tasks on your computer. It provides a simple and intuitive API to simulate mouse movements, button clicks, and keystrokes. This makes it perfect for automating repetitive tasks or creating scripts that interact with GUI applications.

Auto-Starting Scripts on Boot

To execute a Python script automatically when your computer boots up, we can take advantage of the operating system’s startup folder or task scheduler.

Using the Startup Folder (Windows)

  1. Open File Explorer and navigate to C:\Users\<your_username>\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup.

  2. Create a new shortcut to your Python script by right-clicking and selecting “New” -> “Shortcut”.

  3. In the “Create Shortcut” window, click the “Browse” button and locate your Python script file.

  4. Click “Next” and then “Finish” to create the shortcut.

Now, every time you start your computer, the Python script will be executed automatically without any user intervention.

Using the Task Scheduler (Windows)

  1. Open Task Scheduler by pressing Windows + R, typing taskschd.msc, and pressing Enter.

  2. In Task Scheduler, click on “Create Basic Task” or “Create Task” on the right-hand side.

  3. Follow the prompts to configure your task, specifying the details like name, description, trigger (e.g., “At startup”), and action (e.g., “Start a program”).

  4. In the “Program/script” field, provide the path to your Python interpreter (e.g., C:\Python\python.exe) and in the “Add arguments” field, specify the path to your script file.

  5. Click “Finish” to save the task.

With these steps, your Python script will be executed automatically when your computer starts up.

Running pyautogui in Startup Script

To use pyautogui in your startup script, you need to make sure the library is installed. You can install it using pip:

pip install pyautogui

Once installed, you can import and use the pyautogui library in your Python script:

import pyautogui

# Add your pyautogui code here

pyautogui.moveTo(100, 100)
pyautogui.click()

In the above example, we import pyautogui and use two of its functions, moveTo() and click(), to move the mouse to position (100, 100) and perform a mouse click. You can add any pyautogui code to automate specific tasks according to your needs.

Remember to add any necessary delays using time.sleep() to ensure that the interface elements are fully loaded before the script tries to interact with them.

Conclusion

Automating tasks on your computer during startup can save you time and effort. By using the pyautogui library in Python, you can easily create scripts that control mouse movements, button clicks, and keystrokes. Whether it’s opening applications, filling out forms, or performing other repetitive actions, pyautogui allows you to automate these tasks and execute them as soon as your computer boots up.

Just be cautious when using automation tools and ensure that your scripts are well-tested and properly handle any error scenarios.