@pmndre/pine 0.0.0-stage → 0.22.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +226 -2
- package/bin/pine.js +27 -0
- package/docs/SKILL.md +234 -0
- package/docs/references/agents.md +454 -0
- package/docs/references/coding.md +311 -0
- package/docs/references/computer.md +212 -0
- package/docs/references/configuration.md +310 -0
- package/docs/references/events.md +267 -0
- package/docs/references/mcp.md +420 -0
- package/docs/references/operations.md +394 -0
- package/docs/references/overview.md +195 -0
- package/docs/references/security.md +896 -0
- package/docs/references/setup.md +385 -0
- package/docs/references/sources.md +772 -0
- package/docs/references/stack.md +121 -0
- package/docs/references/tui.md +575 -0
- package/examples/home/.env.example +7 -0
- package/examples/home/USER.md +4 -0
- package/examples/home/agents/main/agent.json +4 -0
- package/examples/home/agents/main/workspace/AGENTS.md +14 -0
- package/examples/home/agents/main/workspace/MEMORY.md +4 -0
- package/examples/home/agents/main/workspace/SOUL.md +4 -0
- package/examples/home/models.json +62 -0
- package/examples/home/pine.json +34 -0
- package/examples/home/skills/README.txt +7 -0
- package/examples/launchd/com.pine.daemon.plist +32 -0
- package/examples/systemd/pine.service +22 -0
- package/mcp/browser.d.ts +99 -0
- package/mcp/browser.js +496 -0
- package/mcp/commands.d.ts +20 -0
- package/mcp/commands.js +88 -0
- package/mcp/computer.d.ts +139 -0
- package/mcp/computer.js +698 -0
- package/mcp/computer.swift +989 -0
- package/mcp/lib.d.ts +23 -0
- package/mcp/lib.js +52 -0
- package/mcp/web.d.ts +6 -0
- package/mcp/web.js +140 -0
- package/package.json +32 -4
- package/src/access.d.ts +112 -0
- package/src/access.js +438 -0
- package/src/actions.d.ts +75 -0
- package/src/actions.js +167 -0
- package/src/admin.d.ts +167 -0
- package/src/admin.js +625 -0
- package/src/agent-admin.d.ts +24 -0
- package/src/agent-admin.js +55 -0
- package/src/agents.d.ts +540 -0
- package/src/agents.js +2776 -0
- package/src/background.d.ts +29 -0
- package/src/background.js +100 -0
- package/src/cli.d.ts +2 -0
- package/src/cli.js +777 -0
- package/src/computer.d.ts +30 -0
- package/src/computer.js +32 -0
- package/src/config.d.ts +396 -0
- package/src/config.js +549 -0
- package/src/dev/claude-model.d.ts +37 -0
- package/src/dev/claude-model.js +204 -0
- package/src/env-file.d.ts +4 -0
- package/src/env-file.js +40 -0
- package/src/goals.d.ts +133 -0
- package/src/goals.js +316 -0
- package/src/http.d.ts +46 -0
- package/src/http.js +509 -0
- package/src/limiter.d.ts +32 -0
- package/src/limiter.js +84 -0
- package/src/login.d.ts +23 -0
- package/src/login.js +130 -0
- package/src/manual.d.ts +61 -0
- package/src/manual.js +443 -0
- package/src/mcp-config.d.ts +49 -0
- package/src/mcp-config.js +182 -0
- package/src/models.d.ts +90 -0
- package/src/models.js +274 -0
- package/src/net.d.ts +50 -0
- package/src/net.js +150 -0
- package/src/pine.d.ts +32 -0
- package/src/pine.js +134 -0
- package/src/programs.d.ts +123 -0
- package/src/programs.js +412 -0
- package/src/router.d.ts +56 -0
- package/src/router.js +164 -0
- package/src/sandbox-fs.d.ts +34 -0
- package/src/sandbox-fs.js +186 -0
- package/src/sandbox-linux.d.ts +96 -0
- package/src/sandbox-linux.js +297 -0
- package/src/sandbox-setup.d.ts +70 -0
- package/src/sandbox-setup.js +216 -0
- package/src/security.d.ts +238 -0
- package/src/security.js +660 -0
- package/src/skills.d.ts +67 -0
- package/src/skills.js +163 -0
- package/src/snapshots.d.ts +68 -0
- package/src/snapshots.js +306 -0
- package/src/source-types.d.ts +61 -0
- package/src/source-types.js +1 -0
- package/src/sources/command.d.ts +4 -0
- package/src/sources/command.js +77 -0
- package/src/sources/folder.d.ts +6 -0
- package/src/sources/folder.js +116 -0
- package/src/sources/ftp.d.ts +4 -0
- package/src/sources/ftp.js +307 -0
- package/src/sources/http.d.ts +4 -0
- package/src/sources/http.js +201 -0
- package/src/sources/mqtt.d.ts +18 -0
- package/src/sources/mqtt.js +97 -0
- package/src/sources/telegram.d.ts +22 -0
- package/src/sources/telegram.js +283 -0
- package/src/sources/webhook.d.ts +4 -0
- package/src/sources/webhook.js +52 -0
- package/src/sources.d.ts +344 -0
- package/src/sources.js +966 -0
- package/src/store.d.ts +99 -0
- package/src/store.js +277 -0
- package/src/timers.d.ts +83 -0
- package/src/timers.js +318 -0
- package/src/tools.d.ts +11 -0
- package/src/tools.js +1297 -0
- package/src/tui/app.d.ts +305 -0
- package/src/tui/app.js +3195 -0
- package/src/tui/client.d.ts +236 -0
- package/src/tui/client.js +131 -0
- package/src/tui/code-pane.d.ts +68 -0
- package/src/tui/code-pane.js +535 -0
- package/src/tui/highlight.d.ts +8 -0
- package/src/tui/highlight.js +86 -0
- package/src/tui/picture.d.ts +206 -0
- package/src/tui/picture.js +801 -0
- package/src/tui/run.d.ts +8 -0
- package/src/tui/run.js +97 -0
- package/src/tui/theme.d.ts +32 -0
- package/src/tui/theme.js +128 -0
- package/src/tui/undo.d.ts +5 -0
- package/src/tui/undo.js +50 -0
- package/src/tui/voice.d.ts +49 -0
- package/src/tui/voice.js +155 -0
- package/src/tui/voice.swift +223 -0
- package/src/types.d.ts +139 -0
- package/src/types.js +1 -0
- package/src/undo.d.ts +224 -0
- package/src/undo.js +465 -0
- package/src/update.d.ts +35 -0
- package/src/update.js +130 -0
- package/src/util.d.ts +65 -0
- package/src/util.js +171 -0
- package/src/watching.d.ts +20 -0
- package/src/watching.js +68 -0
- package/src/workspace-view.d.ts +72 -0
- package/src/workspace-view.js +221 -0
|
@@ -0,0 +1,989 @@
|
|
|
1
|
+
// The helper of pine's bundled MCP server "computer" (mcp/computer.ts), for macOS.
|
|
2
|
+
//
|
|
3
|
+
// It is the only native code in pine, and it is small on purpose: it does what Node cannot do
|
|
4
|
+
// (read an app's accessibility tree, press its controls, post mouse and key events, take a
|
|
5
|
+
// picture of a window) and decides nothing. Which app may be operated, what a result says and how
|
|
6
|
+
// a picture's coordinates become a point on the screen is all in computer.ts, where it is tested.
|
|
7
|
+
//
|
|
8
|
+
// pine compiles this file with `swiftc` the first time app control is used and keeps the program
|
|
9
|
+
// in its home. It speaks JSON lines: one request per line on stdin ({"id", "cmd", ...}), one
|
|
10
|
+
// answer per line on stdout ({"id", "ok", ...} or {"id", "ok": false, "code", "error"}).
|
|
11
|
+
//
|
|
12
|
+
// Points are global screen points: the origin is the top left corner of the main display, y
|
|
13
|
+
// grows downwards, and a second display continues the same plane. The accessibility API, mouse
|
|
14
|
+
// events and window bounds all use them.
|
|
15
|
+
//
|
|
16
|
+
// Started with --demo it opens a small window of its own ("Pine Demo"). A process may read its
|
|
17
|
+
// own accessibility tree without the owner's grant, so the live tests can drive the real code
|
|
18
|
+
// against that window on a machine where nothing was granted.
|
|
19
|
+
import AppKit
|
|
20
|
+
import ApplicationServices
|
|
21
|
+
import Carbon.HIToolbox
|
|
22
|
+
|
|
23
|
+
struct Fail: Error {
|
|
24
|
+
let code: String
|
|
25
|
+
let message: String
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
func send(_ obj: [String: Any]) {
|
|
29
|
+
guard let data = try? JSONSerialization.data(withJSONObject: obj) else { return }
|
|
30
|
+
FileHandle.standardOutput.write(data)
|
|
31
|
+
FileHandle.standardOutput.write("\n".data(using: .utf8)!)
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
/** Waits without stopping the run loop: what AppKit knows about other apps arrives through it. */
|
|
35
|
+
func wait(_ seconds: Double) {
|
|
36
|
+
RunLoop.current.run(until: Date().addingTimeInterval(seconds))
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
// ---- references ----------------------------------------------------------------------------------
|
|
40
|
+
|
|
41
|
+
/** An accessibility element as a dictionary key: the same control gets the same reference every time. */
|
|
42
|
+
final class Key: Hashable {
|
|
43
|
+
let el: AXUIElement
|
|
44
|
+
init(_ el: AXUIElement) { self.el = el }
|
|
45
|
+
static func == (a: Key, b: Key) -> Bool { return CFEqual(a.el, b.el) }
|
|
46
|
+
func hash(into h: inout Hasher) { h.combine(CFHash(el)) }
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
var elements: [String: AXUIElement] = [:]
|
|
50
|
+
var names: [Key: String] = [:]
|
|
51
|
+
var counter = 0
|
|
52
|
+
|
|
53
|
+
func refFor(_ el: AXUIElement) -> String {
|
|
54
|
+
let k = Key(el)
|
|
55
|
+
if let r = names[k] { return r }
|
|
56
|
+
// Controls that are gone are forgotten all at once, rarely: a reference that was dropped is answered with "gone".
|
|
57
|
+
if elements.count > 50_000 {
|
|
58
|
+
elements.removeAll()
|
|
59
|
+
names.removeAll()
|
|
60
|
+
}
|
|
61
|
+
counter += 1
|
|
62
|
+
let r = "e\(counter)"
|
|
63
|
+
names[k] = r
|
|
64
|
+
elements[r] = el
|
|
65
|
+
return r
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
func element(_ p: [String: Any]) throws -> AXUIElement {
|
|
69
|
+
guard let r = p["ref"] as? String, let el = elements[r] else { throw Fail(code: "gone", message: "no control has this reference") }
|
|
70
|
+
return el
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
// ---- accessibility -------------------------------------------------------------------------------
|
|
74
|
+
|
|
75
|
+
func isMissing(_ v: AnyObject) -> Bool {
|
|
76
|
+
if v is NSNull { return true }
|
|
77
|
+
if CFGetTypeID(v) == AXValueGetTypeID() { return AXValueGetType(v as! AXValue) == .axError }
|
|
78
|
+
return false
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
func copy(_ el: AXUIElement, _ name: String) -> AnyObject? {
|
|
82
|
+
var v: CFTypeRef?
|
|
83
|
+
guard AXUIElementCopyAttributeValue(el, name as CFString, &v) == .success, let got = v, !isMissing(got) else { return nil }
|
|
84
|
+
return got
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
func many(_ el: AXUIElement, _ attrs: [String]) -> [AnyObject?]? {
|
|
88
|
+
var raw: CFArray?
|
|
89
|
+
guard AXUIElementCopyMultipleAttributeValues(el, attrs as CFArray, AXCopyMultipleAttributeOptions(rawValue: 0), &raw) == .success, let vals = raw as? [AnyObject], vals.count == attrs.count else { return nil }
|
|
90
|
+
return vals.map { isMissing($0) ? nil : $0 }
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
func check(_ e: AXError, _ what: String) throws {
|
|
94
|
+
switch e {
|
|
95
|
+
case .success: return
|
|
96
|
+
case .apiDisabled: throw Fail(code: "accessibility", message: "")
|
|
97
|
+
case .invalidUIElement: throw Fail(code: "gone", message: "this control does not exist any more")
|
|
98
|
+
case .cannotComplete: throw Fail(code: "failed", message: "\(what): the app did not answer")
|
|
99
|
+
default: throw Fail(code: "failed", message: "\(what) did not work (accessibility error \(e.rawValue))")
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
func point(_ v: AnyObject?) -> CGPoint? {
|
|
104
|
+
guard let v = v, CFGetTypeID(v) == AXValueGetTypeID() else { return nil }
|
|
105
|
+
var p = CGPoint.zero
|
|
106
|
+
return AXValueGetValue(v as! AXValue, .cgPoint, &p) ? p : nil
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
func size(_ v: AnyObject?) -> CGSize? {
|
|
110
|
+
guard let v = v, CFGetTypeID(v) == AXValueGetTypeID() else { return nil }
|
|
111
|
+
var s = CGSize.zero
|
|
112
|
+
return AXValueGetValue(v as! AXValue, .cgSize, &s) ? s : nil
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
func frame(_ el: AXUIElement) -> CGRect? {
|
|
116
|
+
guard let p = point(copy(el, "AXPosition")), let s = size(copy(el, "AXSize")) else { return nil }
|
|
117
|
+
return CGRect(origin: p, size: s)
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
func actions(_ el: AXUIElement) -> [String] {
|
|
121
|
+
var raw: CFArray?
|
|
122
|
+
guard AXUIElementCopyActionNames(el, &raw) == .success else { return [] }
|
|
123
|
+
return (raw as? [String]) ?? []
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
func settable(_ el: AXUIElement, _ attr: String) -> Bool {
|
|
127
|
+
var yes: DarwinBoolean = false
|
|
128
|
+
return AXUIElementIsAttributeSettable(el, attr as CFString, &yes) == .success && yes.boolValue
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
/** Reading another app needs the owner's grant. A process may always read itself (the demo window). */
|
|
132
|
+
func needAccess(_ pid: pid_t) throws {
|
|
133
|
+
if pid != getpid() && !AXIsProcessTrusted() { throw Fail(code: "accessibility", message: "") }
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
/** Mouse and key events go nowhere without the grant, and say nothing about it. */
|
|
137
|
+
func needInput() throws {
|
|
138
|
+
if !AXIsProcessTrusted() { throw Fail(code: "accessibility", message: "") }
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
// ---- apps ----------------------------------------------------------------------------------------
|
|
142
|
+
|
|
143
|
+
func parentOf(_ pid: pid_t) -> pid_t {
|
|
144
|
+
var info = kinfo_proc()
|
|
145
|
+
var length = MemoryLayout<kinfo_proc>.stride
|
|
146
|
+
var mib: [Int32] = [CTL_KERN, KERN_PROC, KERN_PROC_PID, pid]
|
|
147
|
+
guard sysctl(&mib, 4, &info, &length, nil, 0) == 0, length > 0 else { return 0 }
|
|
148
|
+
return info.kp_eproc.e_ppid
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
/** The app pine itself runs in: the first app with windows among the processes that started this one. */
|
|
152
|
+
let hostApp: NSRunningApplication? = {
|
|
153
|
+
var pid = getppid()
|
|
154
|
+
for _ in 0..<32 {
|
|
155
|
+
if pid <= 1 { break }
|
|
156
|
+
if let a = NSRunningApplication(processIdentifier: pid), a.activationPolicy == .regular { return a }
|
|
157
|
+
pid = parentOf(pid)
|
|
158
|
+
}
|
|
159
|
+
return nil
|
|
160
|
+
}()
|
|
161
|
+
|
|
162
|
+
let demoName = "Pine Demo"
|
|
163
|
+
var demo: Demo?
|
|
164
|
+
|
|
165
|
+
func nameOf(_ a: NSRunningApplication) -> String {
|
|
166
|
+
if demo != nil && a.processIdentifier == getpid() { return demoName }
|
|
167
|
+
return a.localizedName ?? a.executableURL?.lastPathComponent ?? "pid \(a.processIdentifier)"
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
func info(_ a: NSRunningApplication) -> [String: Any] {
|
|
171
|
+
return [
|
|
172
|
+
"name": nameOf(a),
|
|
173
|
+
"pid": Int(a.processIdentifier),
|
|
174
|
+
"bundle": a.bundleIdentifier ?? "",
|
|
175
|
+
"host": hostApp != nil && a.processIdentifier == hostApp!.processIdentifier,
|
|
176
|
+
"front": NSWorkspace.shared.frontmostApplication?.processIdentifier == a.processIdentifier,
|
|
177
|
+
"hidden": a.isHidden,
|
|
178
|
+
]
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
/** The running app somebody means by a name: its name, its bundle id, or the name of its file. */
|
|
182
|
+
func lookUp(_ query: String) -> NSRunningApplication? {
|
|
183
|
+
let q = query.trimmingCharacters(in: .whitespaces).lowercased()
|
|
184
|
+
if q.isEmpty { return nil }
|
|
185
|
+
if demo != nil && q == demoName.lowercased() { return NSRunningApplication.current }
|
|
186
|
+
let all = NSWorkspace.shared.runningApplications
|
|
187
|
+
// An app with windows before a background one of the same name.
|
|
188
|
+
let ordered = all.filter { $0.activationPolicy == .regular } + all.filter { $0.activationPolicy != .regular }
|
|
189
|
+
let tests: [(NSRunningApplication) -> String?] = [
|
|
190
|
+
{ $0.localizedName },
|
|
191
|
+
{ $0.bundleIdentifier },
|
|
192
|
+
{ $0.bundleURL?.deletingPathExtension().lastPathComponent },
|
|
193
|
+
{ $0.executableURL?.lastPathComponent },
|
|
194
|
+
]
|
|
195
|
+
for test in tests {
|
|
196
|
+
if let a = ordered.first(where: { test($0)?.lowercased() == q }) { return a }
|
|
197
|
+
}
|
|
198
|
+
return nil
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
func running(_ p: [String: Any]) throws -> NSRunningApplication {
|
|
202
|
+
guard let n = p["pid"] as? Int, let a = NSRunningApplication(processIdentifier: pid_t(n)), !a.isTerminated else { throw Fail(code: "not_running", message: "the app is not running any more") }
|
|
203
|
+
return a
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
func appElement(_ pid: pid_t) -> AXUIElement {
|
|
207
|
+
let el = AXUIElementCreateApplication(pid)
|
|
208
|
+
// An app that hangs must not hang pine with it.
|
|
209
|
+
AXUIElementSetMessagingTimeout(el, 3)
|
|
210
|
+
return el
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
func windowsOf(_ app: AXUIElement) -> [AXUIElement] {
|
|
214
|
+
return (copy(app, "AXWindows") as? [AXUIElement]) ?? []
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
func describe(_ w: AXUIElement, index: Int) -> [String: Any] {
|
|
218
|
+
var d: [String: Any] = ["index": index, "ref": refFor(w), "title": (copy(w, "AXTitle") as? String) ?? ""]
|
|
219
|
+
if (copy(w, "AXMinimized") as? Bool) == true { d["minimized"] = true }
|
|
220
|
+
if (copy(w, "AXMain") as? Bool) == true { d["main"] = true }
|
|
221
|
+
if let f = frame(w) {
|
|
222
|
+
d["x"] = Int(f.minX)
|
|
223
|
+
d["y"] = Int(f.minY)
|
|
224
|
+
d["w"] = Int(f.width)
|
|
225
|
+
d["h"] = Int(f.height)
|
|
226
|
+
}
|
|
227
|
+
return d
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
func windowList(_ pid: pid_t) -> [[String: Any]] {
|
|
231
|
+
return windowsOf(appElement(pid)).enumerated().map { describe($0.element, index: $0.offset + 1) }
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
// ---- the outline ---------------------------------------------------------------------------------
|
|
235
|
+
|
|
236
|
+
let ATTRS = ["AXRole", "AXSubrole", "AXTitle", "AXDescription", "AXValue", "AXEnabled", "AXFocused", "AXSelected", "AXPosition", "AXSize", "AXChildren", "AXHelp", "AXPlaceholderValue", "AXVisibleRows"]
|
|
237
|
+
/** Not worth a line in an outline, with everything inside. */
|
|
238
|
+
let SKIPPED: Set<String> = ["AXScrollBar", "AXGrowArea", "AXSplitter", "AXColumn", "AXRuler", "AXMenuBar"]
|
|
239
|
+
/** Containers: asking them what can be done with them is a wasted question. */
|
|
240
|
+
let PASSIVE: Set<String> = ["AXWindow", "AXScrollArea", "AXSplitGroup", "AXTable", "AXOutline", "AXList", "AXToolbar", "AXTabGroup", "AXLayoutArea", "AXStaticText", "AXSheet", "AXWebArea"]
|
|
241
|
+
let PRESS = ["AXPress", "AXPick", "AXOpen", "AXConfirm"]
|
|
242
|
+
let EDITABLE: Set<String> = ["AXTextField", "AXTextArea", "AXComboBox", "AXSlider", "AXIncrementor"]
|
|
243
|
+
|
|
244
|
+
struct Walk {
|
|
245
|
+
var count = 0
|
|
246
|
+
var cut = false
|
|
247
|
+
let limit: Int
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
func node(_ el: AXUIElement, depth: Int, walk: inout Walk) -> [String: Any]? {
|
|
251
|
+
if walk.count >= walk.limit || depth > 60 {
|
|
252
|
+
walk.cut = true
|
|
253
|
+
return nil
|
|
254
|
+
}
|
|
255
|
+
guard let v = many(el, ATTRS), let role = v[0] as? String, !SKIPPED.contains(role) else { return nil }
|
|
256
|
+
walk.count += 1
|
|
257
|
+
var n: [String: Any] = ["role": role]
|
|
258
|
+
if let s = v[1] as? String, !s.isEmpty { n["subrole"] = s }
|
|
259
|
+
if let s = v[2] as? String, !s.isEmpty { n["title"] = s }
|
|
260
|
+
if let s = v[3] as? String, !s.isEmpty { n["desc"] = s }
|
|
261
|
+
if let s = v[4] as? String {
|
|
262
|
+
if !s.isEmpty { n["value"] = s.count > 6000 ? String(s.prefix(6000)) : s }
|
|
263
|
+
if s.count > 6000 { n["more"] = s.count - 6000 }
|
|
264
|
+
} else if let num = v[4] as? NSNumber {
|
|
265
|
+
n["value"] = num
|
|
266
|
+
}
|
|
267
|
+
if (v[5] as? Bool) == false { n["disabled"] = true }
|
|
268
|
+
if (v[6] as? Bool) == true { n["focused"] = true }
|
|
269
|
+
if (v[7] as? Bool) == true { n["selected"] = true }
|
|
270
|
+
if let s = v[11] as? String, !s.isEmpty { n["help"] = s }
|
|
271
|
+
if let s = v[12] as? String, !s.isEmpty { n["placeholder"] = s }
|
|
272
|
+
let press = PASSIVE.contains(role) ? false : actions(el).contains(where: { PRESS.contains($0) })
|
|
273
|
+
let edit = EDITABLE.contains(role) && settable(el, "AXValue")
|
|
274
|
+
if press { n["press"] = true }
|
|
275
|
+
if edit { n["edit"] = true }
|
|
276
|
+
// A reference for what can be acted on: pressed, written into, or (a row) selected.
|
|
277
|
+
if press || edit || role == "AXRow" || EDITABLE.contains(role) { n["ref"] = refFor(el) }
|
|
278
|
+
// A long table is its visible rows: the rest is reached by scrolling.
|
|
279
|
+
let kids = (v[13] as? [AXUIElement]) ?? (v[10] as? [AXUIElement]) ?? []
|
|
280
|
+
var children: [[String: Any]] = []
|
|
281
|
+
for kid in kids {
|
|
282
|
+
if let c = node(kid, depth: depth + 1, walk: &walk) { children.append(c) }
|
|
283
|
+
}
|
|
284
|
+
if !children.isEmpty { n["children"] = children }
|
|
285
|
+
return n
|
|
286
|
+
}
|
|
287
|
+
|
|
288
|
+
// ---- input ---------------------------------------------------------------------------------------
|
|
289
|
+
|
|
290
|
+
let source = CGEventSource(stateID: .hidSystemState)
|
|
291
|
+
|
|
292
|
+
func mouse(_ type: CGEventType, _ at: CGPoint, _ button: CGMouseButton = .left, clicks: Int64 = 1) {
|
|
293
|
+
guard let e = CGEvent(mouseEventSource: source, mouseType: type, mouseCursorPosition: at, mouseButton: button) else { return }
|
|
294
|
+
e.setIntegerValueField(.mouseEventClickState, value: clicks)
|
|
295
|
+
e.post(tap: .cghidEventTap)
|
|
296
|
+
}
|
|
297
|
+
|
|
298
|
+
let NAMED_KEYS: [String: Int] = [
|
|
299
|
+
"return": kVK_Return, "enter": kVK_Return, "tab": kVK_Tab, "space": kVK_Space, "delete": kVK_Delete, "backspace": kVK_Delete,
|
|
300
|
+
"forwarddelete": kVK_ForwardDelete, "escape": kVK_Escape, "esc": kVK_Escape, "left": kVK_LeftArrow, "right": kVK_RightArrow,
|
|
301
|
+
"down": kVK_DownArrow, "up": kVK_UpArrow, "home": kVK_Home, "end": kVK_End, "pageup": kVK_PageUp, "pagedown": kVK_PageDown,
|
|
302
|
+
"f1": kVK_F1, "f2": kVK_F2, "f3": kVK_F3, "f4": kVK_F4, "f5": kVK_F5, "f6": kVK_F6, "f7": kVK_F7, "f8": kVK_F8, "f9": kVK_F9, "f10": kVK_F10, "f11": kVK_F11, "f12": kVK_F12,
|
|
303
|
+
]
|
|
304
|
+
/** Where the characters are on an American keyboard: used when the layout in use cannot be read. */
|
|
305
|
+
let ANSI_KEYS: [String: Int] = [
|
|
306
|
+
"a": 0, "s": 1, "d": 2, "f": 3, "h": 4, "g": 5, "z": 6, "x": 7, "c": 8, "v": 9, "b": 11, "q": 12, "w": 13, "e": 14, "r": 15, "y": 16, "t": 17,
|
|
307
|
+
"1": 18, "2": 19, "3": 20, "4": 21, "6": 22, "5": 23, "=": 24, "9": 25, "7": 26, "-": 27, "8": 28, "0": 29, "]": 30, "o": 31, "u": 32, "[": 33,
|
|
308
|
+
"i": 34, "p": 35, "l": 37, "j": 38, "'": 39, "k": 40, ";": 41, "\\": 42, ",": 43, "/": 44, "n": 45, "m": 46, ".": 47, "`": 50,
|
|
309
|
+
]
|
|
310
|
+
|
|
311
|
+
/**
|
|
312
|
+
* Which key makes which character on the keyboard layout in use. A shortcut is pressed by key,
|
|
313
|
+
* and on a German keyboard the key for "z" is where an American one has "y".
|
|
314
|
+
*/
|
|
315
|
+
func layoutKeys() -> [String: (Int, Bool)] {
|
|
316
|
+
var map: [String: (Int, Bool)] = [:]
|
|
317
|
+
for candidate in [TISCopyCurrentKeyboardLayoutInputSource(), TISCopyCurrentASCIICapableKeyboardLayoutInputSource()] {
|
|
318
|
+
guard let src = candidate?.takeRetainedValue(), let raw = TISGetInputSourceProperty(src, kTISPropertyUnicodeKeyLayoutData) else { continue }
|
|
319
|
+
let data = Unmanaged<CFData>.fromOpaque(raw).takeUnretainedValue() as Data
|
|
320
|
+
data.withUnsafeBytes { (buf: UnsafeRawBufferPointer) in
|
|
321
|
+
guard let layout = buf.baseAddress?.assumingMemoryBound(to: UCKeyboardLayout.self) else { return }
|
|
322
|
+
for shift in [false, true] {
|
|
323
|
+
for code in 0..<128 {
|
|
324
|
+
var dead: UInt32 = 0
|
|
325
|
+
var chars = [UniChar](repeating: 0, count: 4)
|
|
326
|
+
var length = 0
|
|
327
|
+
let mods: UInt32 = shift ? UInt32((shiftKey >> 8) & 0xFF) : 0
|
|
328
|
+
let status = UCKeyTranslate(layout, UInt16(code), UInt16(kUCKeyActionDown), mods, UInt32(LMGetKbdType()), OptionBits(kUCKeyTranslateNoDeadKeysMask), &dead, 4, &length, &chars)
|
|
329
|
+
if status == noErr && length == 1 && chars[0] > 32 {
|
|
330
|
+
let s = String(utf16CodeUnits: chars, count: 1)
|
|
331
|
+
if map[s] == nil { map[s] = (code, shift) }
|
|
332
|
+
}
|
|
333
|
+
}
|
|
334
|
+
}
|
|
335
|
+
}
|
|
336
|
+
}
|
|
337
|
+
return map
|
|
338
|
+
}
|
|
339
|
+
|
|
340
|
+
func flags(_ mods: [String]) -> CGEventFlags {
|
|
341
|
+
var f = CGEventFlags()
|
|
342
|
+
for m in mods {
|
|
343
|
+
switch m {
|
|
344
|
+
case "cmd": f.insert(.maskCommand)
|
|
345
|
+
case "shift": f.insert(.maskShift)
|
|
346
|
+
case "alt": f.insert(.maskAlternate)
|
|
347
|
+
case "ctrl": f.insert(.maskControl)
|
|
348
|
+
case "fn": f.insert(.maskSecondaryFn)
|
|
349
|
+
default: break
|
|
350
|
+
}
|
|
351
|
+
}
|
|
352
|
+
return f
|
|
353
|
+
}
|
|
354
|
+
|
|
355
|
+
/**
|
|
356
|
+
* Key events go to the app they are meant for, not to whatever is in front: an owner who clicks
|
|
357
|
+
* into another window while an agent types does not get the rest of the text typed into it.
|
|
358
|
+
*/
|
|
359
|
+
func post(_ e: CGEvent, to pid: pid_t) {
|
|
360
|
+
if pid > 0 { e.postToPid(pid) } else { e.post(tap: .cghidEventTap) }
|
|
361
|
+
}
|
|
362
|
+
|
|
363
|
+
func pressKey(_ code: Int, _ f: CGEventFlags, to pid: pid_t) {
|
|
364
|
+
for down in [true, false] {
|
|
365
|
+
guard let e = CGEvent(keyboardEventSource: source, virtualKey: CGKeyCode(code), keyDown: down) else { continue }
|
|
366
|
+
e.flags = f
|
|
367
|
+
post(e, to: pid)
|
|
368
|
+
usleep(10_000)
|
|
369
|
+
}
|
|
370
|
+
}
|
|
371
|
+
|
|
372
|
+
func typeText(_ text: String, to pid: pid_t) {
|
|
373
|
+
// A line break is the return key and a tab the tab key: as text they are not always taken for that.
|
|
374
|
+
var run: [UniChar] = []
|
|
375
|
+
func flush() {
|
|
376
|
+
var i = 0
|
|
377
|
+
while i < run.count {
|
|
378
|
+
let part = Array(run[i..<min(i + 16, run.count)])
|
|
379
|
+
for down in [true, false] {
|
|
380
|
+
guard let e = CGEvent(keyboardEventSource: source, virtualKey: 0, keyDown: down) else { continue }
|
|
381
|
+
part.withUnsafeBufferPointer { e.keyboardSetUnicodeString(stringLength: part.count, unicodeString: $0.baseAddress) }
|
|
382
|
+
// Without this the event carries the modifier keys the owner happens to hold.
|
|
383
|
+
e.flags = []
|
|
384
|
+
post(e, to: pid)
|
|
385
|
+
usleep(10_000)
|
|
386
|
+
}
|
|
387
|
+
i += 16
|
|
388
|
+
}
|
|
389
|
+
run = []
|
|
390
|
+
}
|
|
391
|
+
for unit in text.replacingOccurrences(of: "\r\n", with: "\n").utf16 {
|
|
392
|
+
if unit == 10 || unit == 13 {
|
|
393
|
+
flush()
|
|
394
|
+
pressKey(kVK_Return, [], to: pid)
|
|
395
|
+
} else if unit == 9 {
|
|
396
|
+
flush()
|
|
397
|
+
pressKey(kVK_Tab, [], to: pid)
|
|
398
|
+
} else {
|
|
399
|
+
run.append(unit)
|
|
400
|
+
}
|
|
401
|
+
}
|
|
402
|
+
flush()
|
|
403
|
+
}
|
|
404
|
+
|
|
405
|
+
// ---- pictures ------------------------------------------------------------------------------------
|
|
406
|
+
|
|
407
|
+
func runTool(_ path: String, _ args: [String]) -> (Int32, String) {
|
|
408
|
+
let p = Process()
|
|
409
|
+
p.executableURL = URL(fileURLWithPath: path)
|
|
410
|
+
p.arguments = args
|
|
411
|
+
let err = Pipe()
|
|
412
|
+
p.standardError = err
|
|
413
|
+
p.standardOutput = FileHandle.nullDevice
|
|
414
|
+
do { try p.run() } catch { return (-1, "\(error.localizedDescription)") }
|
|
415
|
+
let data = err.fileHandleForReading.readDataToEndOfFile()
|
|
416
|
+
p.waitUntilExit()
|
|
417
|
+
return (p.terminationStatus, String(data: data, encoding: .utf8) ?? "")
|
|
418
|
+
}
|
|
419
|
+
|
|
420
|
+
/** The window server's number for a window of an app, found by where the window is. */
|
|
421
|
+
func windowNumber(pid: pid_t, at f: CGRect) -> Int? {
|
|
422
|
+
guard let list = CGWindowListCopyWindowInfo([.optionOnScreenOnly, .excludeDesktopElements], kCGNullWindowID) as? [[String: Any]] else { return nil }
|
|
423
|
+
for w in list {
|
|
424
|
+
guard (w[kCGWindowOwnerPID as String] as? NSNumber)?.int32Value == pid, (w[kCGWindowLayer as String] as? NSNumber)?.intValue == 0 else { continue }
|
|
425
|
+
guard let b = w[kCGWindowBounds as String] as? NSDictionary, let rect = CGRect(dictionaryRepresentation: b), let id = (w[kCGWindowNumber as String] as? NSNumber)?.intValue else { continue }
|
|
426
|
+
if alike(rect, f) { return id }
|
|
427
|
+
}
|
|
428
|
+
return nil
|
|
429
|
+
}
|
|
430
|
+
|
|
431
|
+
func alike(_ a: CGRect, _ b: CGRect) -> Bool {
|
|
432
|
+
return abs(a.minX - b.minX) < 3 && abs(a.minY - b.minY) < 3 && abs(a.width - b.width) < 3 && abs(a.height - b.height) < 3
|
|
433
|
+
}
|
|
434
|
+
|
|
435
|
+
/**
|
|
436
|
+
* The area that holds a window and what its app has open over it that is no window of its own:
|
|
437
|
+
* a menu, a sheet, a popover. nil when there is nothing of the kind. `own` are the frames of the
|
|
438
|
+
* app's windows, which stay out of it: another document is not part of this one.
|
|
439
|
+
*/
|
|
440
|
+
func withAttached(pid: pid_t, window f: CGRect, own: [CGRect]) -> (area: CGRect, parts: [CGRect])? {
|
|
441
|
+
guard let list = CGWindowListCopyWindowInfo([.optionOnScreenOnly, .excludeDesktopElements], kCGNullWindowID) as? [[String: Any]] else { return nil }
|
|
442
|
+
var above: [CGRect] = []
|
|
443
|
+
// The list goes from the front to the back: what comes after the window is behind it.
|
|
444
|
+
for w in list {
|
|
445
|
+
guard (w[kCGWindowOwnerPID as String] as? NSNumber)?.int32Value == pid else { continue }
|
|
446
|
+
guard let b = w[kCGWindowBounds as String] as? NSDictionary, let rect = CGRect(dictionaryRepresentation: b) else { continue }
|
|
447
|
+
let layer = (w[kCGWindowLayer as String] as? NSNumber)?.intValue ?? 0
|
|
448
|
+
if layer == 0 && alike(rect, f) { break }
|
|
449
|
+
// 25 is the level of the icons in the menu bar: an app's icon there is not part of its window.
|
|
450
|
+
if layer == 25 || ((w[kCGWindowAlpha as String] as? NSNumber)?.doubleValue ?? 1) <= 0 || rect.width < 2 || rect.height < 2 { continue }
|
|
451
|
+
if layer == 0 && own.contains(where: { alike($0, rect) }) { continue }
|
|
452
|
+
above.append(rect)
|
|
453
|
+
}
|
|
454
|
+
var area = f
|
|
455
|
+
var parts = [f]
|
|
456
|
+
var grew = true
|
|
457
|
+
// A submenu hangs on a menu, not on the window: so again until nothing is added.
|
|
458
|
+
while grew {
|
|
459
|
+
grew = false
|
|
460
|
+
for (i, r) in above.enumerated().reversed() where r.intersects(area.insetBy(dx: -8, dy: -8)) {
|
|
461
|
+
area = area.union(r)
|
|
462
|
+
parts.append(r)
|
|
463
|
+
above.remove(at: i)
|
|
464
|
+
grew = true
|
|
465
|
+
}
|
|
466
|
+
}
|
|
467
|
+
return parts.count > 1 ? (area, parts) : nil
|
|
468
|
+
}
|
|
469
|
+
|
|
470
|
+
/** The part of an area that is on a screen, in whole points. */
|
|
471
|
+
func onScreens(_ r: CGRect) -> CGRect {
|
|
472
|
+
var all = CGRect.null
|
|
473
|
+
for d in displays() { all = all.union(CGRect(x: d["x"] as! Int, y: d["y"] as! Int, width: d["w"] as! Int, height: d["h"] as! Int)) }
|
|
474
|
+
let part = all.isNull ? r : r.intersection(all)
|
|
475
|
+
if part.isNull || part.width < 2 || part.height < 2 { return .null }
|
|
476
|
+
let x = part.minX.rounded(.down)
|
|
477
|
+
let y = part.minY.rounded(.down)
|
|
478
|
+
return CGRect(x: x, y: y, width: (part.maxX - x).rounded(.down), height: (part.maxY - y).rounded(.down))
|
|
479
|
+
}
|
|
480
|
+
|
|
481
|
+
func pixels(_ file: URL) -> (Double, Double)? {
|
|
482
|
+
guard let src = CGImageSourceCreateWithURL(file as CFURL, nil), let props = CGImageSourceCopyPropertiesAtIndex(src, 0, nil) as? [CFString: Any] else { return nil }
|
|
483
|
+
guard let w = (props[kCGImagePropertyPixelWidth] as? NSNumber)?.doubleValue, let h = (props[kCGImagePropertyPixelHeight] as? NSNumber)?.doubleValue else { return nil }
|
|
484
|
+
return (w, h)
|
|
485
|
+
}
|
|
486
|
+
|
|
487
|
+
/** Whose window is on top at a point, among ordinary windows. */
|
|
488
|
+
func windowOwner(at p: CGPoint) -> pid_t? {
|
|
489
|
+
guard let list = CGWindowListCopyWindowInfo([.optionOnScreenOnly, .excludeDesktopElements], kCGNullWindowID) as? [[String: Any]] else { return nil }
|
|
490
|
+
for w in list {
|
|
491
|
+
guard (w[kCGWindowLayer as String] as? NSNumber)?.intValue == 0, ((w[kCGWindowAlpha as String] as? NSNumber)?.doubleValue ?? 1) > 0 else { continue }
|
|
492
|
+
guard let b = w[kCGWindowBounds as String] as? NSDictionary, let rect = CGRect(dictionaryRepresentation: b), rect.contains(p) else { continue }
|
|
493
|
+
return (w[kCGWindowOwnerPID as String] as? NSNumber)?.int32Value
|
|
494
|
+
}
|
|
495
|
+
return nil
|
|
496
|
+
}
|
|
497
|
+
|
|
498
|
+
func displays() -> [[String: Any]] {
|
|
499
|
+
var ids = [CGDirectDisplayID](repeating: 0, count: 16)
|
|
500
|
+
var count: UInt32 = 0
|
|
501
|
+
guard CGGetActiveDisplayList(16, &ids, &count) == .success else { return [] }
|
|
502
|
+
return (0..<Int(count)).map { i in
|
|
503
|
+
let b = CGDisplayBounds(ids[i])
|
|
504
|
+
return ["index": i + 1, "x": Int(b.minX), "y": Int(b.minY), "w": Int(b.width), "h": Int(b.height), "main": CGDisplayIsMain(ids[i]) != 0]
|
|
505
|
+
}
|
|
506
|
+
}
|
|
507
|
+
|
|
508
|
+
/** Writes a picture no larger than asked for, as JPEG: a model is sent every picture again with every turn. */
|
|
509
|
+
func shrink(_ from: URL, to: URL, edge: Double, area: Double, keep: [CGRect] = []) throws -> (Int, Int) {
|
|
510
|
+
guard let src = CGImageSourceCreateWithURL(from as CFURL, nil), let image = CGImageSourceCreateImageAtIndex(src, 0, nil) else { throw Fail(code: "failed", message: "the picture could not be read") }
|
|
511
|
+
let W = Double(image.width)
|
|
512
|
+
let H = Double(image.height)
|
|
513
|
+
let scale = min(1, edge / max(W, H), (area / (W * H)).squareRoot())
|
|
514
|
+
let w = max(1, Int((W * scale).rounded()))
|
|
515
|
+
let h = max(1, Int((H * scale).rounded()))
|
|
516
|
+
guard let ctx = CGContext(data: nil, width: w, height: h, bitsPerComponent: 8, bytesPerRow: 0, space: CGColorSpace(name: CGColorSpace.sRGB) ?? CGColorSpaceCreateDeviceRGB(), bitmapInfo: CGImageAlphaInfo.noneSkipLast.rawValue) else { throw Fail(code: "failed", message: "the picture could not be made smaller") }
|
|
517
|
+
// `keep`: the parts of the picture that are shown, as fractions of it from its top left corner.
|
|
518
|
+
// The rest stays grey: a picture of one app does not show what lies beside it on the screen.
|
|
519
|
+
ctx.setFillColor(CGColor(gray: keep.isEmpty ? 1 : 0.5, alpha: 1))
|
|
520
|
+
ctx.fill(CGRect(x: 0, y: 0, width: w, height: h))
|
|
521
|
+
if !keep.isEmpty { ctx.clip(to: keep.map { CGRect(x: $0.minX * Double(w), y: (1 - $0.maxY) * Double(h), width: $0.width * Double(w), height: $0.height * Double(h)) }) }
|
|
522
|
+
ctx.interpolationQuality = .high
|
|
523
|
+
ctx.draw(image, in: CGRect(x: 0, y: 0, width: w, height: h))
|
|
524
|
+
guard let small = ctx.makeImage(), let dest = CGImageDestinationCreateWithURL(to as CFURL, "public.jpeg" as CFString, 1, nil) else { throw Fail(code: "failed", message: "the picture could not be written") }
|
|
525
|
+
CGImageDestinationAddImage(dest, small, [kCGImageDestinationLossyCompressionQuality: 0.8] as CFDictionary)
|
|
526
|
+
guard CGImageDestinationFinalize(dest) else { throw Fail(code: "failed", message: "the picture could not be written") }
|
|
527
|
+
return (w, h)
|
|
528
|
+
}
|
|
529
|
+
|
|
530
|
+
// ---- the demo window -----------------------------------------------------------------------------
|
|
531
|
+
|
|
532
|
+
final class Demo: NSObject {
|
|
533
|
+
let window = NSWindow(contentRect: NSRect(x: 240, y: 320, width: 360, height: 190), styleMask: [.titled, .closable], backing: .buffered, defer: false)
|
|
534
|
+
let field = NSTextField(frame: NSRect(x: 20, y: 130, width: 320, height: 24))
|
|
535
|
+
let result = NSTextField(labelWithString: "Nothing yet")
|
|
536
|
+
let loud = NSButton(checkboxWithTitle: "Loud", target: nil, action: nil)
|
|
537
|
+
|
|
538
|
+
func open() {
|
|
539
|
+
NSApp.setActivationPolicy(.accessory)
|
|
540
|
+
window.title = demoName
|
|
541
|
+
window.isReleasedWhenClosed = false
|
|
542
|
+
field.placeholderString = "A word"
|
|
543
|
+
let button = NSButton(title: "Shout", target: self, action: #selector(shout(_:)))
|
|
544
|
+
button.frame = NSRect(x: 20, y: 80, width: 100, height: 32)
|
|
545
|
+
loud.frame = NSRect(x: 140, y: 84, width: 120, height: 24)
|
|
546
|
+
result.frame = NSRect(x: 20, y: 30, width: 320, height: 24)
|
|
547
|
+
for view in [field, button, loud, result] as [NSView] { window.contentView?.addSubview(view) }
|
|
548
|
+
let bar = NSMenu()
|
|
549
|
+
let item = NSMenuItem(title: "Demo", action: nil, keyEquivalent: "")
|
|
550
|
+
let menu = NSMenu(title: "Demo")
|
|
551
|
+
let reset = NSMenuItem(title: "Reset", action: #selector(reset(_:)), keyEquivalent: "r")
|
|
552
|
+
reset.target = self
|
|
553
|
+
menu.addItem(reset)
|
|
554
|
+
item.submenu = menu
|
|
555
|
+
bar.addItem(NSMenuItem(title: "Apple", action: nil, keyEquivalent: ""))
|
|
556
|
+
bar.addItem(item)
|
|
557
|
+
NSApp.mainMenu = bar
|
|
558
|
+
window.orderFrontRegardless()
|
|
559
|
+
}
|
|
560
|
+
|
|
561
|
+
@objc func shout(_ sender: Any?) {
|
|
562
|
+
result.stringValue = field.stringValue.uppercased() + (loud.state == .on ? "!!!" : "!")
|
|
563
|
+
}
|
|
564
|
+
|
|
565
|
+
@objc func reset(_ sender: Any?) {
|
|
566
|
+
field.stringValue = ""
|
|
567
|
+
result.stringValue = "Nothing yet"
|
|
568
|
+
}
|
|
569
|
+
}
|
|
570
|
+
|
|
571
|
+
// ---- commands ------------------------------------------------------------------------------------
|
|
572
|
+
|
|
573
|
+
func num(_ p: [String: Any], _ key: String) throws -> Double {
|
|
574
|
+
guard let n = p[key] as? NSNumber else { throw Fail(code: "failed", message: "\(key) is missing") }
|
|
575
|
+
return n.doubleValue
|
|
576
|
+
}
|
|
577
|
+
|
|
578
|
+
func handle(_ cmd: String, _ p: [String: Any]) throws -> [String: Any] {
|
|
579
|
+
switch cmd {
|
|
580
|
+
case "status":
|
|
581
|
+
// Asking makes macOS show its own window, which leads the owner to the right switch.
|
|
582
|
+
if (p["ask"] as? String) == "accessibility" { _ = AXIsProcessTrustedWithOptions([kAXTrustedCheckOptionPrompt.takeUnretainedValue() as String: true] as CFDictionary) }
|
|
583
|
+
if (p["ask"] as? String) == "screen" { _ = CGRequestScreenCaptureAccess() }
|
|
584
|
+
var r: [String: Any] = ["accessibility": AXIsProcessTrusted(), "screen": CGPreflightScreenCaptureAccess(), "pid": Int(getpid())]
|
|
585
|
+
if let h = hostApp { r["host"] = info(h) }
|
|
586
|
+
return r
|
|
587
|
+
|
|
588
|
+
case "apps":
|
|
589
|
+
let trusted = AXIsProcessTrusted()
|
|
590
|
+
var list: [[String: Any]] = []
|
|
591
|
+
var apps = NSWorkspace.shared.runningApplications.filter { $0.activationPolicy == .regular }
|
|
592
|
+
if demo != nil { apps.append(NSRunningApplication.current) }
|
|
593
|
+
for a in apps {
|
|
594
|
+
var d = info(a)
|
|
595
|
+
if trusted || a.processIdentifier == getpid() {
|
|
596
|
+
let el = AXUIElementCreateApplication(a.processIdentifier)
|
|
597
|
+
AXUIElementSetMessagingTimeout(el, 1)
|
|
598
|
+
d["windows"] = windowsOf(el).enumerated().map { describe($0.element, index: $0.offset + 1) }
|
|
599
|
+
}
|
|
600
|
+
list.append(d)
|
|
601
|
+
}
|
|
602
|
+
return ["apps": list, "displays": displays(), "accessibility": trusted]
|
|
603
|
+
|
|
604
|
+
case "find":
|
|
605
|
+
guard let query = p["app"] as? String, let a = lookUp(query) else { return ["running": false] }
|
|
606
|
+
var d = info(a)
|
|
607
|
+
d["running"] = true
|
|
608
|
+
return d
|
|
609
|
+
|
|
610
|
+
case "open":
|
|
611
|
+
guard let query = p["app"] as? String, !query.isEmpty else { throw Fail(code: "failed", message: "app is missing") }
|
|
612
|
+
if let a = lookUp(query) {
|
|
613
|
+
var d = info(a)
|
|
614
|
+
d["launched"] = false
|
|
615
|
+
return d
|
|
616
|
+
}
|
|
617
|
+
var (status, err) = runTool("/usr/bin/open", ["-a", query])
|
|
618
|
+
if status != 0 && query.range(of: "^[A-Za-z0-9-]+(\\.[A-Za-z0-9-]+)+$", options: .regularExpression) != nil { (status, err) = runTool("/usr/bin/open", ["-b", query]) }
|
|
619
|
+
if status != 0 { throw Fail(code: "not_found", message: err.trimmingCharacters(in: .whitespacesAndNewlines)) }
|
|
620
|
+
for _ in 0..<150 {
|
|
621
|
+
if let a = lookUp(query), a.isFinishedLaunching {
|
|
622
|
+
var d = info(a)
|
|
623
|
+
d["launched"] = true
|
|
624
|
+
return d
|
|
625
|
+
}
|
|
626
|
+
wait(0.1)
|
|
627
|
+
}
|
|
628
|
+
throw Fail(code: "failed", message: "the app was started but did not appear")
|
|
629
|
+
|
|
630
|
+
case "activate":
|
|
631
|
+
let a = try running(p)
|
|
632
|
+
let pid = a.processIdentifier
|
|
633
|
+
if pid == getpid() {
|
|
634
|
+
demo?.window.orderFrontRegardless()
|
|
635
|
+
return ["front": true]
|
|
636
|
+
}
|
|
637
|
+
let wasFront = NSWorkspace.shared.frontmostApplication?.processIdentifier == pid
|
|
638
|
+
if a.isHidden { a.unhide() }
|
|
639
|
+
let el = appElement(pid)
|
|
640
|
+
// Bringing an app forward works without the owner's grant; its windows are only reached with it.
|
|
641
|
+
let trusted = AXIsProcessTrusted()
|
|
642
|
+
if trusted {
|
|
643
|
+
let all = windowsOf(el)
|
|
644
|
+
// Windows that were all put away into the Dock: the first one comes back.
|
|
645
|
+
if !all.isEmpty && all.allSatisfy({ (copy($0, "AXMinimized") as? Bool) == true }) { AXUIElementSetAttributeValue(all[0], "AXMinimized" as CFString, kCFBooleanFalse) }
|
|
646
|
+
if p["ref"] != nil, let w = try? element(p) {
|
|
647
|
+
if (copy(w, "AXMinimized") as? Bool) == true { AXUIElementSetAttributeValue(w, "AXMinimized" as CFString, kCFBooleanFalse) }
|
|
648
|
+
AXUIElementPerformAction(w, "AXRaise" as CFString)
|
|
649
|
+
AXUIElementSetAttributeValue(w, "AXMain" as CFString, kCFBooleanTrue)
|
|
650
|
+
}
|
|
651
|
+
}
|
|
652
|
+
// 3: all windows, and in front of the app that is active now.
|
|
653
|
+
a.activate(options: NSApplication.ActivationOptions(rawValue: 3))
|
|
654
|
+
if trusted { AXUIElementSetAttributeValue(el, "AXFrontmost" as CFString, kCFBooleanTrue) }
|
|
655
|
+
for _ in 0..<30 {
|
|
656
|
+
if NSWorkspace.shared.frontmostApplication?.processIdentifier == pid {
|
|
657
|
+
// An app that has just come forward drops the keys and clicks of its first moment.
|
|
658
|
+
if !wasFront { wait(0.35) }
|
|
659
|
+
return ["front": true]
|
|
660
|
+
}
|
|
661
|
+
wait(0.05)
|
|
662
|
+
}
|
|
663
|
+
return ["front": trusted && (copy(el, "AXFrontmost") as? Bool) == true]
|
|
664
|
+
|
|
665
|
+
case "windows":
|
|
666
|
+
let a = try running(p)
|
|
667
|
+
try needAccess(a.processIdentifier)
|
|
668
|
+
return ["windows": windowList(a.processIdentifier)]
|
|
669
|
+
|
|
670
|
+
case "frame":
|
|
671
|
+
let el = try element(p)
|
|
672
|
+
guard let f = frame(el) else { throw Fail(code: "gone", message: "this window does not exist any more") }
|
|
673
|
+
return ["x": Double(f.minX), "y": Double(f.minY), "w": Double(f.width), "h": Double(f.height)]
|
|
674
|
+
|
|
675
|
+
case "tree":
|
|
676
|
+
let a = try running(p)
|
|
677
|
+
let pid = a.processIdentifier
|
|
678
|
+
try needAccess(pid)
|
|
679
|
+
let el = appElement(pid)
|
|
680
|
+
// Apps built on a browser (Electron) only describe their windows to somebody who asks for it.
|
|
681
|
+
if pid != getpid() { AXUIElementSetAttributeValue(el, "AXManualAccessibility" as CFString, kCFBooleanTrue) }
|
|
682
|
+
let all = windowsOf(el)
|
|
683
|
+
var r: [String: Any] = ["windows": all.enumerated().map { describe($0.element, index: $0.offset + 1) }]
|
|
684
|
+
var chosen: AXUIElement?
|
|
685
|
+
if let n = p["window"] as? Int {
|
|
686
|
+
guard n >= 1 && n <= all.count else { throw Fail(code: "not_found", message: "there is no window \(n)") }
|
|
687
|
+
chosen = all[n - 1]
|
|
688
|
+
} else if let w = copy(el, "AXFocusedWindow"), CFGetTypeID(w) == AXUIElementGetTypeID() {
|
|
689
|
+
chosen = (w as! AXUIElement)
|
|
690
|
+
} else if let w = copy(el, "AXMainWindow"), CFGetTypeID(w) == AXUIElementGetTypeID() {
|
|
691
|
+
chosen = (w as! AXUIElement)
|
|
692
|
+
} else {
|
|
693
|
+
chosen = all.first
|
|
694
|
+
}
|
|
695
|
+
guard let w = chosen else { return r }
|
|
696
|
+
var walk = Walk(limit: (p["max"] as? Int) ?? 2500)
|
|
697
|
+
let index = (all.firstIndex(where: { CFEqual($0, w) }) ?? 0) + 1
|
|
698
|
+
r["window"] = describe(w, index: index)
|
|
699
|
+
if let root = node(w, depth: 0, walk: &walk) { r["root"] = root }
|
|
700
|
+
r["cut"] = walk.cut
|
|
701
|
+
return r
|
|
702
|
+
|
|
703
|
+
case "menu":
|
|
704
|
+
let a = try running(p)
|
|
705
|
+
try needAccess(a.processIdentifier)
|
|
706
|
+
var parent: AXUIElement
|
|
707
|
+
if p["ref"] != nil {
|
|
708
|
+
parent = try element(p)
|
|
709
|
+
} else {
|
|
710
|
+
guard let bar = copy(appElement(a.processIdentifier), "AXMenuBar"), CFGetTypeID(bar) == AXUIElementGetTypeID() else { return ["items": [[String: Any]]()] }
|
|
711
|
+
parent = bar as! AXUIElement
|
|
712
|
+
}
|
|
713
|
+
var kids = (copy(parent, "AXChildren") as? [AXUIElement]) ?? []
|
|
714
|
+
// An entry holds its menu, and the menu holds the entries.
|
|
715
|
+
if kids.count == 1, (copy(kids[0], "AXRole") as? String) == "AXMenu" {
|
|
716
|
+
parent = kids[0]
|
|
717
|
+
kids = (copy(parent, "AXChildren") as? [AXUIElement]) ?? []
|
|
718
|
+
}
|
|
719
|
+
var items: [[String: Any]] = []
|
|
720
|
+
for kid in kids {
|
|
721
|
+
guard let v = many(kid, ["AXTitle", "AXEnabled", "AXMenuItemMarkChar", "AXMenuItemCmdChar", "AXMenuItemCmdModifiers", "AXChildren"]) else { continue }
|
|
722
|
+
guard let title = v[0] as? String, !title.isEmpty else { continue }
|
|
723
|
+
var d: [String: Any] = ["ref": refFor(kid), "title": title]
|
|
724
|
+
if (v[1] as? Bool) == false { d["disabled"] = true }
|
|
725
|
+
if let mark = v[2] as? String, !mark.isEmpty { d["mark"] = mark }
|
|
726
|
+
if let ch = v[3] as? String, !ch.isEmpty {
|
|
727
|
+
let m = (v[4] as? NSNumber)?.intValue ?? 0
|
|
728
|
+
d["shortcut"] = (m & 4 != 0 ? "ctrl+" : "") + (m & 2 != 0 ? "alt+" : "") + (m & 1 != 0 ? "shift+" : "") + (m & 8 == 0 ? "cmd+" : "") + ch.lowercased()
|
|
729
|
+
}
|
|
730
|
+
if let sub = v[5] as? [AXUIElement], !sub.isEmpty { d["submenu"] = true }
|
|
731
|
+
items.append(d)
|
|
732
|
+
}
|
|
733
|
+
return ["items": items]
|
|
734
|
+
|
|
735
|
+
case "whose":
|
|
736
|
+
let el = try element(p)
|
|
737
|
+
var pid: pid_t = 0
|
|
738
|
+
try check(AXUIElementGetPid(el, &pid), "finding the control")
|
|
739
|
+
guard let v = many(el, ["AXRole", "AXSubrole", "AXTitle", "AXDescription"]), let role = v[0] as? String else { throw Fail(code: "gone", message: "this control does not exist any more") }
|
|
740
|
+
var d: [String: Any] = ["pid": Int(pid), "role": role]
|
|
741
|
+
if let s = v[1] as? String, !s.isEmpty { d["subrole"] = s }
|
|
742
|
+
if let s = (v[2] as? String) ?? (v[3] as? String), !s.isEmpty { d["title"] = s }
|
|
743
|
+
return d
|
|
744
|
+
|
|
745
|
+
case "press":
|
|
746
|
+
let el = try element(p)
|
|
747
|
+
let has = actions(el)
|
|
748
|
+
for name in PRESS where has.contains(name) {
|
|
749
|
+
let e = AXUIElementPerformAction(el, name as CFString)
|
|
750
|
+
// A press that opens a dialog is often not answered until the dialog is closed.
|
|
751
|
+
if e == .cannotComplete { return ["done": true, "unsure": true] }
|
|
752
|
+
try check(e, "pressing")
|
|
753
|
+
return ["done": true]
|
|
754
|
+
}
|
|
755
|
+
if settable(el, "AXSelected") {
|
|
756
|
+
try check(AXUIElementSetAttributeValue(el, "AXSelected" as CFString, kCFBooleanTrue), "selecting")
|
|
757
|
+
return ["done": true, "selected": true]
|
|
758
|
+
}
|
|
759
|
+
// Nothing to press by name: the caller clicks in its middle instead.
|
|
760
|
+
guard let f = frame(el), f.width > 0, f.height > 0 else { throw Fail(code: "failed", message: "this control cannot be pressed") }
|
|
761
|
+
return ["done": false, "x": Double(f.midX), "y": Double(f.midY)]
|
|
762
|
+
|
|
763
|
+
case "set":
|
|
764
|
+
let el = try element(p)
|
|
765
|
+
guard let text = p["text"] as? String else { throw Fail(code: "failed", message: "text is missing") }
|
|
766
|
+
AXUIElementSetAttributeValue(el, "AXFocused" as CFString, kCFBooleanTrue)
|
|
767
|
+
if settable(el, "AXValue") {
|
|
768
|
+
// A slider takes a number, everything else text.
|
|
769
|
+
let value: AnyObject = (copy(el, "AXValue") is NSNumber && Double(text) != nil) ? NSNumber(value: Double(text)!) : text as NSString
|
|
770
|
+
try check(AXUIElementSetAttributeValue(el, "AXValue" as CFString, value), "writing")
|
|
771
|
+
return ["done": true]
|
|
772
|
+
}
|
|
773
|
+
guard let f = frame(el), f.width > 0, f.height > 0 else { throw Fail(code: "failed", message: "this control takes no text") }
|
|
774
|
+
return ["done": false, "x": Double(f.midX), "y": Double(f.midY)]
|
|
775
|
+
|
|
776
|
+
case "at":
|
|
777
|
+
try needInput()
|
|
778
|
+
let x = try num(p, "x")
|
|
779
|
+
let y = try num(p, "y")
|
|
780
|
+
var r: [String: Any] = [:]
|
|
781
|
+
var hit: AXUIElement?
|
|
782
|
+
if AXUIElementCopyElementAtPosition(AXUIElementCreateSystemWide(), Float(x), Float(y), &hit) == .success, let el = hit {
|
|
783
|
+
var pid: pid_t = 0
|
|
784
|
+
if AXUIElementGetPid(el, &pid) == .success {
|
|
785
|
+
r["pid"] = Int(pid)
|
|
786
|
+
if let a = NSRunningApplication(processIdentifier: pid) { r["name"] = nameOf(a) }
|
|
787
|
+
}
|
|
788
|
+
if let v = many(el, ["AXRole", "AXSubrole", "AXTitle", "AXDescription"]), let role = v[0] as? String {
|
|
789
|
+
r["role"] = role
|
|
790
|
+
if let s = v[1] as? String, !s.isEmpty { r["subrole"] = s }
|
|
791
|
+
if let s = (v[2] as? String) ?? (v[3] as? String), !s.isEmpty { r["title"] = s }
|
|
792
|
+
}
|
|
793
|
+
}
|
|
794
|
+
if let owner = windowOwner(at: CGPoint(x: x, y: y)) {
|
|
795
|
+
r["wpid"] = Int(owner)
|
|
796
|
+
if let a = NSRunningApplication(processIdentifier: owner) { r["wname"] = nameOf(a) }
|
|
797
|
+
}
|
|
798
|
+
return r
|
|
799
|
+
|
|
800
|
+
case "click":
|
|
801
|
+
try needInput()
|
|
802
|
+
let at = CGPoint(x: try num(p, "x"), y: try num(p, "y"))
|
|
803
|
+
let right = (p["button"] as? String) == "right"
|
|
804
|
+
let count = max(1, min(3, (p["count"] as? Int) ?? 1))
|
|
805
|
+
mouse(.mouseMoved, at)
|
|
806
|
+
usleep(30_000)
|
|
807
|
+
for i in 1...count {
|
|
808
|
+
mouse(right ? .rightMouseDown : .leftMouseDown, at, right ? .right : .left, clicks: Int64(i))
|
|
809
|
+
usleep(20_000)
|
|
810
|
+
mouse(right ? .rightMouseUp : .leftMouseUp, at, right ? .right : .left, clicks: Int64(i))
|
|
811
|
+
usleep(40_000)
|
|
812
|
+
}
|
|
813
|
+
return [:]
|
|
814
|
+
|
|
815
|
+
case "drag":
|
|
816
|
+
try needInput()
|
|
817
|
+
let from = CGPoint(x: try num(p, "x"), y: try num(p, "y"))
|
|
818
|
+
let to = CGPoint(x: try num(p, "x2"), y: try num(p, "y2"))
|
|
819
|
+
mouse(.mouseMoved, from)
|
|
820
|
+
usleep(30_000)
|
|
821
|
+
mouse(.leftMouseDown, from)
|
|
822
|
+
usleep(80_000)
|
|
823
|
+
// In steps: an app only takes a drag for one when the pointer travels.
|
|
824
|
+
for i in 1...12 {
|
|
825
|
+
let t = CGFloat(i) / 12
|
|
826
|
+
mouse(.leftMouseDragged, CGPoint(x: from.x + (to.x - from.x) * t, y: from.y + (to.y - from.y) * t))
|
|
827
|
+
usleep(15_000)
|
|
828
|
+
}
|
|
829
|
+
usleep(60_000)
|
|
830
|
+
mouse(.leftMouseUp, to)
|
|
831
|
+
return [:]
|
|
832
|
+
|
|
833
|
+
case "scroll":
|
|
834
|
+
try needInput()
|
|
835
|
+
let at = CGPoint(x: try num(p, "x"), y: try num(p, "y"))
|
|
836
|
+
var dx = Int32((p["dx"] as? Int) ?? 0)
|
|
837
|
+
var dy = Int32((p["dy"] as? Int) ?? 0)
|
|
838
|
+
mouse(.mouseMoved, at)
|
|
839
|
+
usleep(30_000)
|
|
840
|
+
// A few lines at a time, as a wheel does.
|
|
841
|
+
while dx != 0 || dy != 0 {
|
|
842
|
+
let sx = max(-3, min(3, dx))
|
|
843
|
+
let sy = max(-3, min(3, dy))
|
|
844
|
+
if let e = CGEvent(scrollWheelEvent2Source: source, units: .line, wheelCount: 2, wheel1: sy, wheel2: sx, wheel3: 0) { e.post(tap: .cghidEventTap) }
|
|
845
|
+
dx -= sx
|
|
846
|
+
dy -= sy
|
|
847
|
+
usleep(15_000)
|
|
848
|
+
}
|
|
849
|
+
return [:]
|
|
850
|
+
|
|
851
|
+
case "type":
|
|
852
|
+
try needInput()
|
|
853
|
+
guard let text = p["text"] as? String else { throw Fail(code: "failed", message: "text is missing") }
|
|
854
|
+
typeText(text, to: pid_t((p["pid"] as? Int) ?? 0))
|
|
855
|
+
return [:]
|
|
856
|
+
|
|
857
|
+
case "key":
|
|
858
|
+
try needInput()
|
|
859
|
+
guard let key = (p["key"] as? String)?.lowercased(), !key.isEmpty else { throw Fail(code: "failed", message: "key is missing") }
|
|
860
|
+
var f = flags((p["mods"] as? [String]) ?? [])
|
|
861
|
+
let to = pid_t((p["pid"] as? Int) ?? 0)
|
|
862
|
+
if let code = NAMED_KEYS[key] {
|
|
863
|
+
pressKey(code, f, to: to)
|
|
864
|
+
} else if let (code, shift) = layoutKeys()[key] {
|
|
865
|
+
if shift { f.insert(.maskShift) }
|
|
866
|
+
pressKey(code, f, to: to)
|
|
867
|
+
} else if let code = ANSI_KEYS[key] {
|
|
868
|
+
pressKey(code, f, to: to)
|
|
869
|
+
} else {
|
|
870
|
+
throw Fail(code: "unknown_key", message: "there is no key \"\(key)\" on this keyboard")
|
|
871
|
+
}
|
|
872
|
+
return [:]
|
|
873
|
+
|
|
874
|
+
case "shot":
|
|
875
|
+
if !CGPreflightScreenCaptureAccess() { throw Fail(code: "screen", message: "") }
|
|
876
|
+
guard let dir = p["dir"] as? String else { throw Fail(code: "failed", message: "dir is missing") }
|
|
877
|
+
let name = "shot-\(getpid())-\(Int(Date().timeIntervalSince1970 * 1000))"
|
|
878
|
+
let raw = URL(fileURLWithPath: dir).appendingPathComponent(name + ".png")
|
|
879
|
+
let small = URL(fileURLWithPath: dir).appendingPathComponent(name + ".jpg")
|
|
880
|
+
defer { try? FileManager.default.removeItem(at: raw) }
|
|
881
|
+
let base = ["-x", "-t", "png"]
|
|
882
|
+
var args = base
|
|
883
|
+
var area: CGRect
|
|
884
|
+
var r: [String: Any] = [:]
|
|
885
|
+
var whole: CGRect?
|
|
886
|
+
var keep: [CGRect] = []
|
|
887
|
+
var front = true
|
|
888
|
+
let region = { (a: CGRect) -> [String] in ["-R", "\(Int(a.minX)),\(Int(a.minY)),\(Int(a.width)),\(Int(a.height))"] }
|
|
889
|
+
if p["ref"] != nil {
|
|
890
|
+
let w = try element(p)
|
|
891
|
+
guard let f = frame(w), f.width > 1, f.height > 1 else { throw Fail(code: "gone", message: "this window does not exist any more") }
|
|
892
|
+
if (copy(w, "AXMinimized") as? Bool) == true { throw Fail(code: "minimized", message: "") }
|
|
893
|
+
var pid: pid_t = 0
|
|
894
|
+
AXUIElementGetPid(w, &pid)
|
|
895
|
+
area = f
|
|
896
|
+
r["wx"] = Double(f.minX)
|
|
897
|
+
r["wy"] = Double(f.minY)
|
|
898
|
+
let own = windowsOf(appElement(pid)).compactMap { frame($0) }
|
|
899
|
+
// A part of the screen shows the app only while the app is in front: pine brings it there and asks again.
|
|
900
|
+
let behind = Fail(code: "behind", message: "this window is behind another app")
|
|
901
|
+
front = NSWorkspace.shared.frontmostApplication?.processIdentifier == pid
|
|
902
|
+
if let wide = withAttached(pid: pid, window: f, own: own) {
|
|
903
|
+
if !front { throw behind }
|
|
904
|
+
// A menu or a sheet is open over the window. A picture of the window alone would leave it
|
|
905
|
+
// out, or hold it at a place nobody can work out: so the picture is of that part of the screen.
|
|
906
|
+
area = onScreens(wide.area)
|
|
907
|
+
if !area.isNull { keep = wide.parts.map { CGRect(x: ($0.minX - area.minX) / area.width, y: ($0.minY - area.minY) / area.height, width: $0.width / area.width, height: $0.height / area.height) } }
|
|
908
|
+
r["more"] = true
|
|
909
|
+
} else if let id = windowNumber(pid: pid, at: f) {
|
|
910
|
+
// The window itself, also where another one covers it.
|
|
911
|
+
args += ["-o", "-l", String(id)]
|
|
912
|
+
whole = f
|
|
913
|
+
} else {
|
|
914
|
+
if !front { throw behind }
|
|
915
|
+
area = onScreens(f)
|
|
916
|
+
}
|
|
917
|
+
if area.isNull { throw Fail(code: "failed", message: "this window is not on a screen") }
|
|
918
|
+
if whole == nil { args += region(area) }
|
|
919
|
+
} else {
|
|
920
|
+
let all = displays()
|
|
921
|
+
let n = (p["display"] as? Int) ?? 1
|
|
922
|
+
guard n >= 1 && n <= all.count else { throw Fail(code: "not_found", message: "there is no screen \(n); there \(all.count == 1 ? "is 1 screen" : "are \(all.count) screens")") }
|
|
923
|
+
let d = all[n - 1]
|
|
924
|
+
area = CGRect(x: d["x"] as! Int, y: d["y"] as! Int, width: d["w"] as! Int, height: d["h"] as! Int)
|
|
925
|
+
args += region(area)
|
|
926
|
+
r["display"] = n
|
|
927
|
+
r["displays"] = all.count
|
|
928
|
+
}
|
|
929
|
+
var (status, err) = runTool("/usr/sbin/screencapture", args + [raw.path])
|
|
930
|
+
if status == 0, let f = whole, let (W, H) = pixels(raw) {
|
|
931
|
+
// A picture of a window is the window, at one, two or three pixels to the point. One that
|
|
932
|
+
// is not holds something else as well, and positions in it would be guesses.
|
|
933
|
+
let k = (W / f.width).rounded()
|
|
934
|
+
if k < 1 || abs(W - f.width * k) > 2 * k || abs(H - f.height * k) > 2 * k {
|
|
935
|
+
if !front { throw Fail(code: "behind", message: "this window is behind another app") }
|
|
936
|
+
area = onScreens(f)
|
|
937
|
+
if area.isNull { throw Fail(code: "failed", message: "this window is not on a screen") }
|
|
938
|
+
try? FileManager.default.removeItem(at: raw)
|
|
939
|
+
(status, err) = runTool("/usr/sbin/screencapture", base + region(area) + [raw.path])
|
|
940
|
+
}
|
|
941
|
+
}
|
|
942
|
+
guard status == 0, FileManager.default.fileExists(atPath: raw.path) else { throw Fail(code: "failed", message: "the picture could not be taken\(err.isEmpty ? "" : ": " + err.trimmingCharacters(in: .whitespacesAndNewlines))") }
|
|
943
|
+
let (w, h) = try shrink(raw, to: small, edge: (p["edge"] as? NSNumber)?.doubleValue ?? 1280, area: (p["area"] as? NSNumber)?.doubleValue ?? 1_150_000, keep: keep)
|
|
944
|
+
r["file"] = small.path
|
|
945
|
+
r["width"] = w
|
|
946
|
+
r["height"] = h
|
|
947
|
+
r["x"] = Double(area.minX)
|
|
948
|
+
r["y"] = Double(area.minY)
|
|
949
|
+
r["w"] = Double(area.width)
|
|
950
|
+
r["h"] = Double(area.height)
|
|
951
|
+
return r
|
|
952
|
+
|
|
953
|
+
default:
|
|
954
|
+
throw Fail(code: "failed", message: "unknown command \(cmd)")
|
|
955
|
+
}
|
|
956
|
+
}
|
|
957
|
+
|
|
958
|
+
// ---- main ----------------------------------------------------------------------------------------
|
|
959
|
+
|
|
960
|
+
let app = NSApplication.shared
|
|
961
|
+
// No icon in the Dock, and never the active app.
|
|
962
|
+
app.setActivationPolicy(.prohibited)
|
|
963
|
+
if CommandLine.arguments.contains("--demo") {
|
|
964
|
+
demo = Demo()
|
|
965
|
+
demo!.open()
|
|
966
|
+
}
|
|
967
|
+
|
|
968
|
+
Thread.detachNewThread {
|
|
969
|
+
while let line = readLine(strippingNewline: true) {
|
|
970
|
+
guard let data = line.data(using: .utf8), let msg = (try? JSONSerialization.jsonObject(with: data)) as? [String: Any], let cmd = msg["cmd"] as? String else { continue }
|
|
971
|
+
let id = msg["id"] ?? NSNull()
|
|
972
|
+
// One at a time, on the main thread: AppKit and the accessibility API both want it.
|
|
973
|
+
DispatchQueue.main.async {
|
|
974
|
+
do {
|
|
975
|
+
var r = try handle(cmd, msg)
|
|
976
|
+
r["id"] = id
|
|
977
|
+
r["ok"] = true
|
|
978
|
+
send(r)
|
|
979
|
+
} catch let f as Fail {
|
|
980
|
+
send(["id": id, "ok": false, "code": f.code, "error": f.message])
|
|
981
|
+
} catch {
|
|
982
|
+
send(["id": id, "ok": false, "code": "failed", "error": error.localizedDescription])
|
|
983
|
+
}
|
|
984
|
+
}
|
|
985
|
+
}
|
|
986
|
+
// pine closed the pipe: nothing is left to do.
|
|
987
|
+
exit(0)
|
|
988
|
+
}
|
|
989
|
+
app.run()
|