1

I'm currently using Keyboard maestro to create a macro that allows me to move the cursor to certain coordinates and perform a left click.

Unfortunately under Yosemite it doesn't work as expected, since it clicks sporadically (it always moves the cursor but doesn't click everytime).

Are there any other viable options to achieve this? It would be great if I can set coordinates relatively to active window.

  • Have a look at this post apple.stackexchange.com/questions/176555/… – markhunte Mar 14 '15 at 16:52
  • I would prefer if I can achieve it without applescript, because it takes too long to load when I click the hotkey... – KingBOB Mar 14 '15 at 17:07
  • The main part that does the move and clicking is not applescript. Ignore the Applescript. – markhunte Mar 15 '15 at 7:01
  • I have posted an answer with a working example to show what I mean. – markhunte Mar 15 '15 at 7:40
1

As an Adaption from @mat-burns code in this post ( Note You'll also need xcode installed. )

       #!/usr/bin/env xcrun swift

import Foundation

let kDelayUSec : useconds_t = 500_000

func moveMouseClick( p1: CGPoint) {

    let mouseMove = CGEventCreateMouseEvent(nil, CGEventType(kCGEventLeftMouseDragged), p1, CGMouseButton(kCGMouseButtonLeft)).takeUnretainedValue()
    let mouseDown = CGEventCreateMouseEvent(nil, CGEventType(kCGEventLeftMouseDown), p1, CGMouseButton(kCGMouseButtonLeft)).takeUnretainedValue()
    let mouseUp = CGEventCreateMouseEvent(nil, CGEventType(kCGEventLeftMouseUp), p1, CGMouseButton(kCGMouseButtonLeft)).takeUnretainedValue()


    CGEventPost(CGEventTapLocation(kCGHIDEventTap), mouseMove)
    usleep(kDelayUSec)
    CGEventPost(CGEventTapLocation(kCGHIDEventTap), mouseDown)
    usleep(kDelayUSec)
    CGEventPost(CGEventTapLocation(kCGHIDEventTap), mouseUp)
}


func main() {

    let pointX = 396
    let pointY = 214

    let p1 = CGPointMake( CGFloat(pointX),  CGFloat(pointY))
    moveMouseClick( p1)


}

main()

Change the lines:

let pointX = 396
let pointY = 214

to reflect you click point.

Save this code down as moveClick.swift

And then in Terminal.app run /bin/chmod +x ~/scripts/moveClick.swift to make the script executable

Change ~/scripts/moveClick.swift to your actual path to your swift file.

I do not have Keyboard maestro But I see it can execute a shell script.

Execute a Shell Script Action

~/scripts/moveClick.swift


In the example above we have hard coded the click point.

But if you want to make it versatile, we can make the code take arguments.

Using this code instead.

    #!/usr/bin/env xcrun swift

import Foundation

let kDelayUSec : useconds_t = 500_000

func moveMouseClick( p1: CGPoint) {

    let mouseMove = CGEventCreateMouseEvent(nil, CGEventType(kCGEventLeftMouseDragged), p1, CGMouseButton(kCGMouseButtonLeft)).takeUnretainedValue()
    let mouseDown = CGEventCreateMouseEvent(nil, CGEventType(kCGEventLeftMouseDown), p1, CGMouseButton(kCGMouseButtonLeft)).takeUnretainedValue()
    let mouseUp = CGEventCreateMouseEvent(nil, CGEventType(kCGEventLeftMouseUp), p1, CGMouseButton(kCGMouseButtonLeft)).takeUnretainedValue()


    CGEventPost(CGEventTapLocation(kCGHIDEventTap), mouseMove)
    usleep(kDelayUSec)
    CGEventPost(CGEventTapLocation(kCGHIDEventTap), mouseDown)
    usleep(kDelayUSec)
    CGEventPost(CGEventTapLocation(kCGHIDEventTap), mouseUp)
}


func main() {

    let p1 = CGPointMake( CGFloat(atof( C_ARGV[1])),  CGFloat(atof( C_ARGV[2])))


    moveMouseClick( p1)


}

main()

Again in Keyboard maestro.

Execute a Shell Script Action

~/scripts/moveClick.swift 396, 214

  • Thanks for your comprehensive answer. I'm already testing it. Just a question: does the second code click on those coordinates relatively to active window? I would like it to happen and I'm not totally sure I understand your code :) – KingBOB Mar 15 '15 at 9:27
  • @Giorgio In your question you don't really say what window or how you current method works and how it gets the cords . Can you add that and any related info. that may help me or others to understand your exact goal. – markhunte Mar 15 '15 at 10:39
  • @Giorgio Also the second code clicks on exactly the point coords you give related to the screen. You would have to work out what that point is you want before you run it and then supply that to the command. Hence my last comment requesting you add info. Also what are you trying to click in the window – markhunte Mar 15 '15 at 10:42

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .