Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
#!/usr/bin/env xcrun swift
import Foundation
let kDelayUSec : useconds_t = 500_000
func DragMouse(from p0: CGPoint, to p1: CGPoint) {
let mouseDown = CGEventCreateMouseEvent(nil, .LeftMouseDown, p0, .Left)
let mouseDrag = CGEventCreateMouseEvent(nil, .LeftMouseDragged, p1, .Left)
let mouseUp = CGEventCreateMouseEvent(nil, .LeftMouseUp, p1, .Left)
CGEventPost(.CGHIDEventTap, mouseDown)
usleep(kDelayUSec)
CGEventPost(.CGHIDEventTap, mouseDrag)
usleep(kDelayUSec)
CGEventPost(.CGHIDEventTap, mouseUp)
}
func main() {
let args = NSUserDefaults.standardUserDefaults()
let x = CGFloat(args.integerForKey("x"))
let y = CGFloat(args.integerForKey("y"))
let dx = CGFloat(args.integerForKey("dx"))
let dy = CGFloat(args.integerForKey("dy"))
let p0 = CGPointMake(x, y)
let p1 = CGPointMake(x + dx, y + dy)
DragMouse(from: p0, to: p1)
}
main()
@mgreiner
Copy link

mgreiner commented Nov 25, 2015

Thanks for sharing this. It was just what I needed. I had to modify it to get it working on my machine – possibly due to some Swift 2.0 changes. Here's the update: https://gist.github.com/mgreiner/353aadf3dda008b1ca86

@walmsley
Copy link

walmsley commented Nov 9, 2016

Thought I'd share a quick follow-up with some modifications I made for Swift 3, plus added a function to actually tap the center of the window to initiate the screen recording:

#!/usr/bin/swift

import Foundation

let kDelayUSec : useconds_t = 500000

func DragMouse(p0: CGPoint, p1: CGPoint) {
    let mouseDown = CGEvent.init(mouseEventSource:nil, mouseType:.leftMouseDown, mouseCursorPosition:p0, mouseButton:CGMouseButton.left)
    let mouseDrag = CGEvent.init(mouseEventSource:nil, mouseType:.leftMouseDragged, mouseCursorPosition:p1, mouseButton:CGMouseButton.left)
    let mouseUp = CGEvent.init(mouseEventSource:nil, mouseType:.leftMouseUp, mouseCursorPosition:p1, mouseButton:CGMouseButton.left)
    

    mouseDown!.post(tap:.cghidEventTap)
    usleep(kDelayUSec)
    mouseDrag!.post(tap:.cghidEventTap)
    usleep(kDelayUSec)
    mouseUp!.post(tap:.cghidEventTap)
}

func TapCenter(p0: CGPoint, p1: CGPoint) {
    
    let pC = CGPoint(x:(p0.x+p1.x) / 2, y:(p0.y+p1.y) / 2)
    
    let mouseDown = CGEvent.init(mouseEventSource:nil, mouseType:.leftMouseDown, mouseCursorPosition:pC, mouseButton:CGMouseButton.left)
    let mouseUp = CGEvent.init(mouseEventSource:nil, mouseType:.leftMouseUp, mouseCursorPosition:pC, mouseButton:CGMouseButton.left)
    
    
    mouseDown!.post(tap:.cghidEventTap)
    usleep(kDelayUSec/5)
    mouseUp!.post(tap:.cghidEventTap)
}

func main() {
    let args = UserDefaults.standard

    let x  = CGFloat(args.integer(forKey:"x"))
    let y  = CGFloat(args.integer(forKey:"y"))
    let dx = CGFloat(args.integer(forKey:"dx"))
    let dy = CGFloat(args.integer(forKey:"dy"))

    let p0 = CGPoint(x: x, y: y)
    let p1 = CGPoint(x: x + dx, y: y + dy)

    DragMouse(p0: p0, p1: p1)
    usleep(kDelayUSec)
    TapCenter(p0: p0, p1: p1)
}

main()

@ir-fuel
Copy link

ir-fuel commented Feb 1, 2017

Swift 3 version

#!/usr/bin/env xcrun swift
import Foundation

let kDelayUSec : useconds_t = 500_000

func DragMouse(from p0: CGPoint, to p1: CGPoint) {
    let mouseDown = CGEvent.init(mouseEventSource:nil, mouseType:.leftMouseDown,    mouseCursorPosition:p0, mouseButton:.left)!
    let mouseDrag = CGEvent.init(mouseEventSource:nil, mouseType:.leftMouseDragged, mouseCursorPosition:p1, mouseButton:.left)!
    let mouseUp   = CGEvent.init(mouseEventSource:nil, mouseType:.leftMouseUp,      mouseCursorPosition:p1, mouseButton:.left)!

    mouseDown.post(tap:.cghidEventTap)
    usleep(kDelayUSec)
    mouseDrag.post(tap:.cghidEventTap)
    usleep(kDelayUSec)
    mouseUp.post(tap:.cghidEventTap)
}

func main() {
    let args = UserDefaults.standard

    let x  = CGFloat(args.integer(forKey:"x"))
    let y  = CGFloat(args.integer(forKey:"y"))
    let dx = CGFloat(args.integer(forKey:"dx"))
    let dy = CGFloat(args.integer(forKey:"dy"))

    let p0 = CGPoint(x:x, y:y)
    let p1 = CGPoint(x:x + dx, y:y + dy)

    DragMouse(from: p0, to: p1)
}

main()

@MayurBar
Copy link

MayurBar commented Oct 14, 2019

I want to use postTopid

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment