Skip to content

Instantly share code, notes, and snippets.

@Adam-Langley
Forked from shanecelis/clickdrag.swift
Last active February 15, 2018 21:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Adam-Langley/c87c97ff243bb26e1dc9a626471c688f to your computer and use it in GitHub Desktop.
Save Adam-Langley/c87c97ff243bb26e1dc9a626471c688f to your computer and use it in GitHub Desktop.
Enhanced to support negative values for all 4 inputs
#!/usr/bin/env xcrun swift
// ~/bin/clickdrag -x 10 -y 20 -dx 200 -dy 100 -negx true -negy false -negdx false -negdy false
// 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
var x = CGFloat(args.integer(forKey: "x"))
var 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: "negx")) {
x = -x;
}
if (args.bool(forKey: "negy")) {
y = -y;
}
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