#!/usr/bin/env python3 """Record a real Jotley window locally, verify it, then optionally archive it. Uses Apple's recorder; does not drive the interface or change app preferences. Keep temporary recording files on the internal disk so a disconnected media drive cannot interrupt the movie writer. """ import argparse import datetime import json from pathlib import Path import shutil import subprocess import time def main(): parser = argparse.ArgumentParser(description=__doc__) parser.add_argument("name") parser.add_argument("--seconds", type=int, default=90) parser.add_argument("--bundle", default="com.protomota.jotley.video-demo") parser.add_argument("--window-kind", choices=["main", "widget", "menu"], default="main") parser.add_argument("--directory", type=Path, default=Path("/tmp/jotley-video-refresh-20261023/mac")) parser.add_argument("--archive", type=Path) args = parser.parse_args() if Path(args.name).name != args.name: parser.error("name must be a filename without directories") args.directory.mkdir(parents=True, exist_ok=True) movie = args.directory / (args.name + ".mov") receipt = movie.with_suffix(".capture.json") if movie.exists() or receipt.exists(): parser.error("take already exists; use a new name") swift = '''import AppKit import CoreGraphics let bundle = CommandLine.arguments.last! guard let pid = NSRunningApplication.runningApplications(withBundleIdentifier: bundle).first?.processIdentifier else { exit(2) } let windows = CGWindowListCopyWindowInfo(.optionOnScreenOnly, kCGNullWindowID) as! [[String: Any]] let own = windows.filter { $0[kCGWindowOwnerPID as String] as? Int32 == pid } print(String(data: try! JSONSerialization.data(withJSONObject: own), encoding: .utf8)!) ''' windows = json.loads(subprocess.check_output(["swift", "-e", swift, args.bundle], text=True)) if args.window_kind == "widget": windows = [w for w in windows if w["kCGWindowBounds"]["Width"] == 220 and w["kCGWindowBounds"]["Height"] == 52] if not windows: parser.error("native recording widget is not visible; start transcription and activate another app") elif args.window_kind == "menu": windows = [w for w in windows if w["kCGWindowLayer"] == 101 and w["kCGWindowBounds"]["Width"] > 100 and w["kCGWindowBounds"]["Height"] > 50] if not windows: parser.error("native status menu is not open") window = max(windows, key=lambda w: w["kCGWindowBounds"]["Width"] * w["kCGWindowBounds"]["Height"]) command = ["screencapture", "-x", "-v", "-o", "-a", "-V", str(args.seconds), "-l" + str(window["kCGWindowNumber"]), str(movie)] started = time.time() process = subprocess.Popen(command) data = {"name": args.name, "bundle": args.bundle, "window_kind": args.window_kind, "window": window, "local_movie": str(movie), "pid": process.pid, "started_wall": started, "started_utc": datetime.datetime.fromtimestamp(started, datetime.timezone.utc).isoformat(), "state": "recording", "ui": "Unmodified native app window", "interpolation": False} receipt.write_text(json.dumps(data, indent=2) + "\n") print(json.dumps({"state": "recording", "movie": str(movie), "receipt": str(receipt)}), flush=True) code = process.wait() if code != 0: data.update(state="failed", exit_code=code) receipt.write_text(json.dumps(data, indent=2) + "\n") raise SystemExit(code) probe = subprocess.run(["ffprobe", "-v", "error", "-show_format", "-show_streams", "-of", "json", str(movie)], capture_output=True, text=True, check=True) subprocess.run(["ffmpeg", "-v", "error", "-i", str(movie), "-f", "null", "-"], check=True) data.update(state="verified", probe=json.loads(probe.stdout), full_decode=True) if args.archive: if not args.archive.is_dir(): data["archive_pending"] = str(args.archive) else: target = args.archive / movie.name if target.exists(): raise FileExistsError(target) shutil.copy2(movie, target) data["archived_movie"] = str(target) receipt.write_text(json.dumps(data, indent=2) + "\n") if data.get("archived_movie"): shutil.copy2(receipt, args.archive / receipt.name) print(json.dumps({"state": "verified", "movie": str(movie), "archived": data.get("archived_movie")}), flush=True) if __name__ == "__main__": main()