Skip to content

Instantly share code, notes, and snippets.

@jgthms
Created December 7, 2017 11:25
Show Gist options
  • Select an option

  • Save jgthms/5a8d2488d90790a1b6050f22c0cf4e4e to your computer and use it in GitHub Desktop.

Select an option

Save jgthms/5a8d2488d90790a1b6050f22c0cf4e4e to your computer and use it in GitHub Desktop.
Click drag mouse to specific position and dimension via command line in Mac OS
#!/usr/bin/env xcrun swift
import Foundation
let firstDelayUSec : useconds_t = 2000_000
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)!
usleep(firstDelayUSec)
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()
@jgthms
Copy link
Author

jgthms commented Dec 7, 2017

Use with ./clickdrag.swift -x 240 -y 164 -dx 1200 -dy 800

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