Last active
August 10, 2023 19:20
-
-
Save pwc3/ea255791598a79a1bcd9 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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() |
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()
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()
I want to use postTopid
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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