Smiles2212

Q: How do you make an applescript mouse clicker?

I need an applescript script that can click on certain points over and over again. I'm sorry, but i'm a complete noob at any scripting, so you migt have to hold my hand and walk me through it.

MacBook Pro

Posted on Feb 3, 2012 6:29 PM

Close

Q: How do you make an applescript mouse clicker?

  • All replies
  • Helpful answers

Page 1 Next
  • by red_menace,

    red_menace red_menace Feb 3, 2012 6:40 PM in response to Smiles2212
    Level 6 (15,526 points)
    Desktops
    Feb 3, 2012 6:40 PM in response to Smiles2212

    If you are talking about some random point on the screen, AppleScript desn't have anything to do that - take a look at MouseTools.

  • by twtwtw,

    twtwtw twtwtw Feb 3, 2012 9:11 PM in response to red_menace
    Level 5 (4,935 points)
    Feb 3, 2012 9:11 PM in response to red_menace

    Actually, Red, that's not true.  the following code will supposedly click at x=100 y=200 in a given app:

     

    tell application "System Events"

              tell process "X"

                        click at {100, 200}

              end tell

    end tell

     

    I've never actually used this feature - it says it will click at {x,y} in global (assumedly desktop) coordinates, but it might take some poking around to to get it clicking in the right place.

  • by red_menace,

    red_menace red_menace Feb 3, 2012 11:13 PM in response to twtwtw
    Level 6 (15,526 points)
    Desktops
    Feb 3, 2012 11:13 PM in response to twtwtw

    You are correct in that the click will send the {x,y} location to a process object (UI element), but I haven't had much luck using System Events to just click at some random location (especially if you want to use something like a right-click) - hence my qualifier.

  • by twtwtw,

    twtwtw twtwtw Feb 4, 2012 12:03 AM in response to red_menace
    Level 5 (4,935 points)
    Feb 4, 2012 12:03 AM in response to red_menace

    Well, the OP hasn't really clarified what it is he wants clicked, so we'll have to wait for details.

  • by Smiles2212,

    Smiles2212 Smiles2212 Feb 7, 2012 6:38 PM in response to twtwtw
    Level 1 (0 points)
    Feb 7, 2012 6:38 PM in response to twtwtw

    if i use this script, I get the error "System Events got an error: Can’t get process "X". I'm a complete noob at applescript, so i'm sorry if i'm being stupid to you.

  • by red_menace,

    red_menace red_menace Feb 7, 2012 8:12 PM in response to Smiles2212
    Level 6 (15,526 points)
    Desktops
    Feb 7, 2012 8:12 PM in response to Smiles2212

    In this context System Events is used to script an application's user interface, so "X" would be the application that you tell to click the mouse - change it as needed (the application also needs to be activated to bring it to the front).

     

    It might help to know exactly what you are wanting to do - trying to do something like making a hack for World of Warcraft isn't quite what AppleScript is good at.

  • by Smiles2212,

    Smiles2212 Smiles2212 Feb 8, 2012 4:09 AM in response to red_menace
    Level 1 (0 points)
    Feb 8, 2012 4:09 AM in response to red_menace

    lol. i don't play world of warcraft, but thank you for the info

  • by Hate when you ask for personal details,

    Hate when you ask for personal details Hate when you ask for personal details Apr 10, 2014 2:23 AM in response to red_menace
    Level 1 (0 points)
    Apr 10, 2014 2:23 AM in response to red_menace

    When I run this code snippet I see the following error message

     

    System Events got an error: Can’t make {699, 242} into type list.

     

     

    Trying to send a click to Chrome.

  • by Tony T1,

    Tony T1 Tony T1 Apr 10, 2014 5:59 AM in response to Smiles2212
    Level 6 (9,232 points)
    Mac OS X
    Apr 10, 2014 5:59 AM in response to Smiles2212

    You can use python wrapped in Applescript:

     

    set x to 30

    set y to 5

     

    do shell script "

    /usr/bin/python <<END

    import sys

    import time

    from Quartz.CoreGraphics import *

    def mouseEvent(type, posx, posy):

              theEvent = CGEventCreateMouseEvent(None, type, (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);

    ourEvent = CGEventCreate(None);

    currentpos=CGEventGetLocation(ourEvent);             # Save current mouse position

    mouseclick(" & x & "," & y & ");

    mousemove(int(currentpos.x),int(currentpos.y));      # Restore mouse position

    END"

  • by Tony T1,

    Tony T1 Tony T1 Apr 10, 2014 7:07 AM in response to Tony T1
    Level 6 (9,232 points)
    Mac OS X
    Apr 10, 2014 7:07 AM in response to Tony T1

    This will the same location 50 times:

     

    set x to 30

    set y to 5

    set l to 50 -- click same location 50 times

     

    do shell script "

    /usr/bin/python <<END

    import sys

    import time

    from Quartz.CoreGraphics import *

    def mouseEvent(type, posx, posy):

              theEvent = CGEventCreateMouseEvent(None, type, (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);

    ourEvent = CGEventCreate(None);

    currentpos=CGEventGetLocation(ourEvent);             # Save current mouse position

    for x in range(0, " & l & "):

              mouseclick(" & x & "," & y & ");

    mousemove(int(currentpos.x),int(currentpos.y));      # Restore mouse position

    END"

  • by richardfromcascais weymouth,

    richardfromcascais weymouth richardfromcascais weymouth Jun 16, 2014 1:12 PM in response to Tony T1
    Level 1 (0 points)
    Jun 16, 2014 1:12 PM in response to Tony T1

    thankyou for the script it works great! If you composed it well done.

  • by Tony T1,

    Tony T1 Tony T1 Jun 16, 2014 3:20 PM in response to richardfromcascais weymouth
    Level 6 (9,232 points)
    Mac OS X
    Jun 16, 2014 3:20 PM in response to richardfromcascais weymouth

    richardfromcascais weymouth wrote:

     

    thankyou for the script it works great! If you composed it well done.

     

    Credit goes to: http://www.geekorgy.com/index.php/2010/06/python-mouse-click-and-move-mouse-in-a pple-mac-osx-snow-leopard-10-6-x/

  • by WordsInTheDark,

    WordsInTheDark WordsInTheDark Aug 31, 2014 11:24 AM in response to Tony T1
    Level 1 (0 points)
    Aug 31, 2014 11:24 AM in response to Tony T1

    Is there a way to modify this script to terminate early when a particular key is pressed? I don't really care if the key is option, fn1, esc, or Z, so long as it works. =p

     

    I tried to find a solution for this via Google, but the closest answer that I found was https://docs.python.org/2/faq/library.html#how-do-i-get-a-single-keypress-at-a-t ime, which I couldn't figure out how to modify for my purposes. (since I have 0 experience with coding, except for a little dabbling in XML)

  • by Max Curious,

    Max Curious Max Curious May 18, 2015 2:16 PM in response to Smiles2212
    Level 1 (0 points)
    May 18, 2015 2:16 PM in response to Smiles2212

    I have the same almost exact question..except I need to perform a mouse click on a field in Filemaker Pro. The coordinates could be anywhere at anytime...but it would be the field currently highlighted.

     

    Why is it so hard to say on Mac OS...just perform a mouse click now...and ignore coordinates?

Page 1 Next