Skip to content

Instantly share code, notes, and snippets.

@shanecelis
Forked from pwc3/clickdrag.swift
Last active February 15, 2018 21:33
Show Gist options
  • Save shanecelis/f0754fbd7d9126b18f83966cd538567b to your computer and use it in GitHub Desktop.
Save shanecelis/f0754fbd7d9126b18f83966cd538567b to your computer and use it in GitHub Desktop.
#!/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