Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
#!/usr/bin/env xcrun swift
// ~/bin/clickdrag -x 10 -y 20 -dx 200 -dy 100
// http://www.paulcalnan.com/archives/2014/10/quicktime-screen-recording-of-a-single-window.html
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"))
var dx = CGFloat(args.integer(forKey: "dx"))
var dy = CGFloat(args.integer(forKey: "dy"))
// Turns out you can't pass a negative integer to swift
// this way.
if (args.bool(forKey: "negdx")) {
dx = -dx;
}
if (args.bool(forKey: "negdy")) {
dy = -dy;
}
let p0 = CGPoint(x: x, y: y)
let p1 = CGPoint(x: x + dx, y: y + dy)
print(dx)
DragMouse(from: p0, to: p1)
}
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment