hands test

This commit is contained in:
Ian Gulliver
2026-10-17 23:03:33 -07:00
parent bb908e493f
commit ccf8c551ec
5 changed files with 115 additions and 71 deletions
+11 -15
View File
@@ -1,16 +1,17 @@
import AppKit
@MainActor
func osa(_ src: String) throws -> NSAppleEventDescriptor {
guard let script = NSAppleScript(source: src) else {
throw ToolError("AppleScript did not compile")
return try DispatchQueue.main.sync {
guard let script = NSAppleScript(source: "with timeout of 10 seconds\n\(src)\nend timeout") else {
throw ToolError("AppleScript did not compile")
}
var err: NSDictionary?
let r = script.executeAndReturnError(&err)
if let err {
throw ToolError("AppleScript: \(err[NSAppleScript.errorMessage] ?? err)")
}
return r
}
var err: NSDictionary?
let r = script.executeAndReturnError(&err)
if let err {
throw ToolError("AppleScript: \(err[NSAppleScript.errorMessage] ?? err)")
}
return r
}
func quote(_ s: String) -> String {
@@ -34,18 +35,15 @@ struct TabTarget {
}
}
@MainActor
func js(_ code: String, _ t: TabTarget) throws -> String {
let r = try osa("tell application \"Orion\" to do JavaScript \(quote(code)) in \(t.spec)")
return r.stringValue ?? "undefined"
}
@MainActor
func orionWindow(_ index: Int) throws -> Int {
return Int(try osa("tell application \"Orion\" to get id of window \(index)").int32Value)
}
@MainActor
func tabs() throws -> String {
let src = """
tell application "Orion"
@@ -65,7 +63,6 @@ func tabs() throws -> String {
return "window tab title | url\n" + (try osa(src).stringValue ?? "")
}
@MainActor
func waitLoaded(_ t: TabTarget, timeout: Double) async throws -> String {
let start = Date()
await pause(300)
@@ -78,7 +75,6 @@ func waitLoaded(_ t: TabTarget, timeout: Double) async throws -> String {
return String(format: "still loading after %.1fs", timeout)
}
@MainActor
func browserTool(_ args: [String: Any]) async throws -> [Content] {
let op = try args.require("op", args.string)
let win = args.int("window") ?? 1
@@ -118,7 +114,7 @@ func browserTool(_ args: [String: Any]) async throws -> [Content] {
default:
throw ToolError("unknown op \(op.debugDescription)")
}
let present: @MainActor (String) async throws -> Bool = { needle in
let present: (String) async throws -> Bool = { needle in
(try? js("String(!!document.body && document.body.innerText.includes(\(jsString(needle))))", target)) == "true"
}
if let note = try await waitFor(args, acted: false, present) {