Skip to content

Instantly share code, notes, and snippets.

@sleepygarden
Created June 7, 2014 20:41
Show Gist options
  • Save sleepygarden/d10ac3b69aead38bbc26 to your computer and use it in GitHub Desktop.
Save sleepygarden/d10ac3b69aead38bbc26 to your computer and use it in GitHub Desktop.
Clickgasm.py - You ever wanna click something on OSX, like, really really fast?
"""
clickgasm.py, clicks an x,y point n times on OSX realllly fast
usage: "python clickgasm.py <click x,click y> <loop count>"
sleepygarden 2014
"""
import sys
import time
import datetime
print "Importing Quartz, this might take a second..."
from Quartz.CoreGraphics import CGEventCreateMouseEvent, CGEventPost, CGEventCreate, CGEventGetLocation
from Quartz.CoreGraphics import kCGMouseButtonLeft, kCGHIDEventTap, kCGEventMouseMoved, kCGEventLeftMouseDown, kCGEventLeftMouseUp
print "Done."
def mouseEvent(eventType, posx, posy):
theEvent = CGEventCreateMouseEvent(None, eventType, (posx,posy), kCGMouseButtonLeft)
CGEventPost(kCGHIDEventTap, theEvent)
def mousemove(posx,posy):
mouseEvent(kCGEventMouseMoved, posx,posy)
def mouseclick(posx,posy):
mouseEvent(kCGEventLeftMouseDown, posx,posy)
mouseEvent(kCGEventLeftMouseUp, posx,posy)
def clickgasm(x,y,loop):
ourEvent = CGEventCreate(None)
currentpos = CGEventGetLocation(ourEvent) # Save current mouse position
print "Start:", datetime.datetime.now().time()
for i in xrange(0, loop): #click
mouseclick(x,y)
time.sleep(.01)
print "End:", datetime.datetime.now().time()
mousemove(int(currentpos.x),int(currentpos.y)); # Restore mouse position
def warn(loopCount):
captureMS = loopCount + loopCount*10 # a click event takes about 10ms + 1ms for the sleep
captureS = captureMS/1000
captureM = captureS/60
captureH = captureM/60
captureD = captureH/24
frmtD = "" if captureD == 0 else str(captureD)+"d,"
frmtH = "" if captureH == 0 else str(captureH % 24)+"h,"
frmtM = "" if captureM == 0 else str(captureM % 60)+"m,"
frmtS = "< 1s" if captureS == 0 else str(captureS % 60)+"s"
if (frmtS == ""):
frmtM.rstrip(",")
if (frmtM == ""):
frmtH.rstrip(",")
if (frmtH == ""):
frmtD.rstrip(",")
warnString = "This will capture the mouse for approx {}{}{}{}. Proceed? (Y/N)\n".format(frmtD,frmtH,frmtM,frmtS)
accept = raw_input(warnString)
accept = accept.upper()
return (accept == "Y" or accept == "YES")
def getArgs():
loop = 0
x = 0
y = 0
try:
if len(sys.argv) == 3:
coordsList = sys.argv[1].split(",")
if len(coordsList) == 2:
x = int(coordsList[0].strip())
y = int(coordsList[1].strip())
else:
print "Improper coord formatting"
return 0,0,0
loop = int(sys.argv[2])
else:
coordsList = raw_input("Enter the x,y coord, separated by a comma: ").split(",")
if len(coordsList) == 2:
x = int(coordsList[0].strip())
y = int(coordsList[1].strip())
else:
print "Improper coord formatting"
return 0,0,0
loop = int(raw_input("Click how many times?"))
except ValueError:
print "Oops, looks like you didn't use a number where you should have."
print "Make sure you're using \"python clickgasm.py <click x,click y> <loop count>\""
return 0,0,0
return (x,y,loop)
def main():
x,y,loop = getArgs()
if loop > 0:
if warn(loop):
clickgasm(x,y,loop)
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment