RPA / Robocorp

Mouse / Move

Get coordinates

tasks.py
Copied!

from robocorp.tasks import task
import time
import pyautogui

@task
def get_mouse_coordinates():
  # Get the screen resolution using pyautogui
  screen_width, screen_height = pyautogui.size()
  print(f"Screen resolution: {screen_width} x {screen_height}")

  try:
    print("Press Ctrl+C to stop...")
    while True:
      # Get the current mouse position (x, y coordinates) using pyautogui
      x, y = pyautogui.position()
      print(f"Mouse coordinates: X={x}, Y={y} (out of {screen_width} x {screen_height})", flush=True)
      time.sleep(0.5) # Adjust the delay if necessary
  except KeyboardInterrupt:
    print("\nStopped showing mouse coordinates.")

Set coordinates

tasks.py
Copied!

from robocorp.tasks import task
import pyautogui
import time

@task
def set_mouse_coordinates():
  # Moves mouse to X of 100, Y of 200.
  pyautogui.moveTo(100, 200, duration=2)
  time.sleep(2)

  # Moves mouse to Y of 500 from current position. X is not changed.
  pyautogui.move(None, 500, duration=2)
  time.sleep(2)

  # Moves mouse to X of 600. Y is not changed.
  pyautogui.moveTo(600, None, duration=2)

Drag

tasks.py
Copied!

from robocorp.tasks import task
import time
import pyautogui

@task
def set_mouse_coordinates():
  # Move the cursor to the files area, then drag to select them
  pyautogui.moveTo(30, 370, duration=2)
  pyautogui.dragTo(110, 770, duration=2)

  # Move the cursor inside the selected area, then drag files to another coordinates
  pyautogui.moveTo(65, 650, duration=1)
  pyautogui.dragTo(1500, 800, 2, button='left')