Skip to content

Instantly share code, notes, and snippets.

@iklobato
Created January 19, 2024 16:58
Show Gist options
  • Save iklobato/1a30d3f81bf60b110be47e30599013ae to your computer and use it in GitHub Desktop.
Save iklobato/1a30d3f81bf60b110be47e30599013ae to your computer and use it in GitHub Desktop.
Mouse mover
"""
Mouse Automator
This script automates mouse movements, simulating user activity by randomly moving the mouse pointer within the screen. It uses the Quartz framework for macOS.
To run this script:
1. Make sure you are using a macOS system as Quartz is specific to macOS.
2. Install the required dependencies if not already installed:
- Quartz: This is typically available by default on macOS.
3. Copy and paste the code into a Python file (e.g., mouse_automator.py).
4. Open a terminal and navigate to the directory containing the script.
5. Run the script using the following command:
python mouse_automator.py
The script creates an instance of the MouseAutomator class, which handles mouse automation. It continuously moves the mouse pointer to random positions within a predefined screen size and pauses for a random duration between movements.
MouseAutomator class:
- min_sleep: Minimum sleep time between mouse movements (default: 280 milliseconds).
- max_sleep: Maximum sleep time between mouse movements (default: 299 milliseconds).
- duration: Not currently used.
Methods:
- get_random_coordinate(max_val): Returns a random coordinate between 0 and max_val - 1.
- move_mouse_to_random_position(): Moves the mouse pointer to a random position within the screen.
- sleep_execution(): Pauses the execution for a random amount of time.
main() function:
- Creates an instance of MouseAutomator.
- Enters an infinite loop, repeatedly moving the mouse and sleeping between movements.
KeyboardInterrupt handling:
- If the user interrupts the script (Ctrl+C), it catches the KeyboardInterrupt exception, prints a message, and gracefully exits.
"""
import random
import time
from dataclasses import dataclass
import Quartz
@dataclass
class MouseAutomator:
"""Handles the automation of mouse movements."""
min_sleep: int = 280
max_sleep: int = 299
duration: int = 0
@staticmethod
def get_random_coordinate(max_val):
"""Returns a random coordinate between 0 and max_val - 1."""
return random.randint(0, max_val - 1) if max_val > 0 else 0
def move_mouse_to_random_position(self):
"""Move the mouse pointer to a random position within the screen."""
screen_width, screen_height = 360, 480
x = self.get_random_coordinate(screen_width)
y = self.get_random_coordinate(screen_height)
Quartz.CGEventPost(
Quartz.kCGHIDEventTap,
Quartz.CGEventCreateMouseEvent(None, Quartz.kCGEventMouseMoved, (x, y), 0)
)
def sleep_execution(self):
"""Pause the execution for a random amount of time."""
to_sleep = random.randint(self.min_sleep, self.max_sleep)
time.sleep(to_sleep)
def main():
automator = MouseAutomator()
while True:
automator.move_mouse_to_random_position()
automator.sleep_execution()
if __name__ == "__main__":
try:
main()
except KeyboardInterrupt as ki:
print(f'\nKeyboard Interrupt {ki}', end='\n')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment