-
Notifications
You must be signed in to change notification settings - Fork 1
/
caperture.swift
executable file
·58 lines (47 loc) · 1.96 KB
/
caperture.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#!/usr/bin/env xcrun swift
import Foundation
// Start QuickTime Player using AppleScript
func startQT() {
var scriptToPerform: NSAppleScript?
let asCommand = "tell application \"QuickTime Player\" \n" +
" activate \n" +
" new screen recording \n" +
" delay 1 \n" +
" tell application \"System Events\" to key code 49 \n" +
" delay 1\n" +
" end tell"
scriptToPerform = NSAppleScript(source:asCommand)
var possibleError: NSDictionary?
if let script = scriptToPerform {
script.executeAndReturnError(&possibleError)
if let error = possibleError {
print("ERROR: \(error)")
}
}
}
// Click and drag the mouse as defined by the supplied commandline arguments
func dragMouse() {
let args = UserDefaults.standard
let x = CGFloat(args.integer(forKey: "x"))
let y = CGFloat(args.integer(forKey: "y"))
let w = CGFloat(args.integer(forKey: "w"))
let h = CGFloat(args.integer(forKey: "h"))
let p0 = NSPointToCGPoint(NSMakePoint(x, y))
let p1 = NSPointToCGPoint(NSMakePoint(x + w, y + h))
let mouseDown = CGEvent(mouseEventSource: nil, mouseType: CGEventType.leftMouseDown, mouseCursorPosition: p0, mouseButton: CGMouseButton.left)!
let mouseDrag = CGEvent(mouseEventSource: nil, mouseType: CGEventType.leftMouseDragged, mouseCursorPosition: p1, mouseButton: CGMouseButton.left)!
let mouseUp = CGEvent(mouseEventSource: nil, mouseType: CGEventType.leftMouseUp, mouseCursorPosition: p1, mouseButton: CGMouseButton.left)!
let kDelayUSec : useconds_t = 500_000
mouseDown.post(tap: CGEventTapLocation.cghidEventTap)
usleep(kDelayUSec)
mouseDrag.post(tap: CGEventTapLocation.cghidEventTap)
usleep(kDelayUSec)
mouseUp.post(tap: CGEventTapLocation.cghidEventTap)
}
if (CommandLine.arguments.count != 9) {
print("usage:")
print(" ./caperture.swift -x 100 -y 100 -w 400 -h 300")
} else {
startQT()
dragMouse()
}