Skip to content

Instantly share code, notes, and snippets.

@n8henrie
Last active February 7, 2023 21:27
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save n8henrie/8567995 to your computer and use it in GitHub Desktop.
Save n8henrie/8567995 to your computer and use it in GitHub Desktop.
Script to automate clicking on a point on the screen
"""clicker.py :: Easy way to click stuff on the screen
https://pyautogui.readthedocs.io/en/latest/cheatsheet.html
"""
import argparse
from collections import namedtuple
import logging
import sys
import pyautogui
logging.basicConfig(
level=logging.INFO,
format='%(asctime)s %(name)-12s %(lineno)d %(levelname)-8s %(message)s',
datefmt='%Y-%m-%d %H:%M:%S'
)
logger_name = "{} :: {}".format(__file__, __name__)
logger = logging.getLogger(logger_name)
Point = namedtuple('Point', ['x', 'y', 'delay'])
def run(p_list):
try:
input("\n\nHit RETURN to run, ctrl-c again to cancel")
while True:
for p in p_list:
pyautogui.moveTo(p.x, p.y, duration=p.delay)
pyautogui.click()
except KeyboardInterrupt:
sys.exit(0)
def get_positions_interactive():
p_list = []
print("Ctrl-c when done inputting points")
try:
while True:
delay = input("Move mouse to next position.\nTime delay *before* "
"this click (secs, default 0): ").strip()
# Empty string == False
pos = pyautogui.position()
delay = float(delay or 0)
print(pos)
p_list.append(Point(*pos, delay))
except KeyboardInterrupt:
return p_list
def cli():
parser = argparse.ArgumentParser()
parser.add_argument('-i', '--input-file', help="File of comma separated "
"points and delays, one per line")
parser.add_argument('-o', '--output-file', help="Output points to file "
"instead of running, intended for later use with `-i`")
args = parser.parse_args()
return {k: v for k, v in vars(args).items() if v is not None}
def main(input_file=None, output_file=None):
if input_file:
with open(input_file) as f:
p_list = [Point(*(float(num) for num in line.strip().split(',')))
for line in f.readlines()]
run(p_list)
else:
p_list = get_positions_interactive()
if output_file:
with open(output_file, 'w') as f:
for p in p_list:
f.write("{},{},{}\n".format(*p))
run(p_list)
if __name__ == '__main__':
main(**cli())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment