SPB Git

Repository Public

mlax.swift 7.8 KB 143 lines swift

1// =============================================================================2//                  SP BOUCHER | SOFTWARE & RESEARCH3//             ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING4// =============================================================================5//6// PROJECT METADATA7// -----------------------------------------------------------------------------8// Project          : MacLustr Social Engine & Remote Navigator9// File             : native/MacAccessibilityBridge/mlax.swift10// Module           : native11// Repository       : https://git.spboucher.ai/maclustr-social12// Project Website  : https://social.spboucher.ai13// Created          : 2026-12-0114// Last Modified    : 2026-12-0115//16// =============================================================================17// AUTHOR & PROFESSIONAL AFFILIATION18// =============================================================================19//20// Author           : Simon-Pierre Boucher21// Position         : Professor of Finance22// Institution      : Université du Québec en Outaouais (UQO)23// Department       : Département des sciences administratives24// Organization     : SP Boucher25//26// Website          : https://spboucher.ai27// Git              : https://git.spboucher.ai28//29// =============================================================================30// OFFICIAL CONTACT DIRECTORY31// =============================================================================32//33// General          : info@spboucher.ai34// Professional     : contact@spboucher.ai35// Direct           : simon@spboucher.ai36// Research         : research@spboucher.ai37// Development      : dev@spboucher.ai38// Support          : support@spboucher.ai39// Security/Admin   : admin@spboucher.ai40// Notifications    : notifications@spboucher.ai41//42// Preferred Contact: admin@spboucher.ai43//44// =============================================================================45// COPYRIGHT & INTELLECTUAL PROPERTY46// =============================================================================47//48// Copyright (c) 202649// Simon-Pierre Boucher50//51// All rights reserved where applicable.52//53// This file is governed by the applicable project license,54// copyright notices, institutional agreements and relevant law.55//56// Unless expressly authorized by the applicable license or57// rights holder, copying, redistribution, sublicensing,58// publication and commercial exploitation are not permitted.59//60// Existing third-party rights and notices remain unaffected.61//62// =============================================================================63// CONFIDENTIALITY & SECURITY64// =============================================================================65//66// Classification   : Public67// Distribution     : Public repository; use is governed by the project license68// Security Contact : admin@spboucher.ai69//70// Access to this source does not independently grant permission71// to redistribute or disclose confidential materials.72//73// Do not embed credentials, private keys, access tokens,74// certificates or confidential personal information.75//76// =============================================================================77// LICENSE & MAINTENANCE78// =============================================================================79//80// License          : None specified (no license file in this repository)81// Maintainer       : Simon-Pierre Boucher82// Technical Contact: dev@spboucher.ai83//84// =============================================================================85// END OF SP BOUCHER SOURCE HEADER86// =============================================================================8788// mlax — MacLustr Accessibility bridge (Adaptive Navigator Engine, level 3).89// A small, fixed-verb tool. It never takes a shell string, never reads window CONTENTS of the browser page,90// and does nothing unless macOS Accessibility permission was granted by a person in System Settings.91// Verbs: check | windows <pid> | activate <pid> | dialogs <pid> | dismiss-dialog <pid> <button-title>92import AppKit93import ApplicationServices94import CoreGraphics9596func out(_ o: Any) { let d = try! JSONSerialization.data(withJSONObject: o, options: [.sortedKeys]); print(String(data: d, encoding: .utf8)!) }97func fail(_ msg: String, _ code: Int32 = 2) -> Never { out(["ok": false, "error": msg]); exit(code) }98func attr<T>(_ el: AXUIElement, _ name: String) -> T? { var v: CFTypeRef?; return AXUIElementCopyAttributeValue(el, name as CFString, &v) == .success ? v as? T : nil }99func pidArg(_ i: Int) -> pid_t { guard CommandLine.arguments.count > i, let p = Int32(CommandLine.arguments[i]), p > 1 else { fail("pid requis") }; return p }100/** Only Chromium-family processes started for the navigator may be touched. */101func requireBrowser(_ pid: pid_t) -> NSRunningApplication {102  guard let app = NSRunningApplication(processIdentifier: pid) else { fail("processus introuvable") }103  let name = (app.localizedName ?? "") + " " + (app.bundleIdentifier ?? "")104  guard name.range(of: "chrom", options: .caseInsensitive) != nil else { fail("processus hors périmètre") }105  return app106}107let args = CommandLine.arguments108guard args.count >= 2 else { fail("usage: mlax check | windows <pid> | activate <pid> | dialogs <pid> | dismiss-dialog <pid> <titre>") }109switch args[1] {110case "check":111  // Never prompts: reports the current grants only.112  let ax = AXIsProcessTrustedWithOptions([kAXTrustedCheckOptionPrompt.takeUnretainedValue() as String: false] as CFDictionary)113  out(["ok": true, "accessibility": ax, "screenRecording": CGPreflightScreenCaptureAccess(), "sip": "unchanged", "console": CGSessionCopyCurrentDictionary() != nil])114case "windows":115  let pid = pidArg(2); _ = requireBrowser(pid)116  guard AXIsProcessTrusted() else { fail("permission Accessibilité non accordée", 3) }117  let wins: [AXUIElement] = attr(AXUIElementCreateApplication(pid), kAXWindowsAttribute) ?? []118  out(["ok": true, "windows": wins.map { w -> [String: Any] in119    let role: String = attr(w, kAXSubroleAttribute) ?? ""; let mini: Bool = attr(w, kAXMinimizedAttribute) ?? false120    return ["subrole": role, "minimized": mini] }]) // titles are deliberately not returned: they can carry page content121case "activate":122  let pid = pidArg(2); let app = requireBrowser(pid)123  out(["ok": app.activate(options: [.activateAllWindows])])124case "dialogs":125  let pid = pidArg(2); _ = requireBrowser(pid)126  guard AXIsProcessTrusted() else { fail("permission Accessibilité non accordée", 3) }127  let wins: [AXUIElement] = attr(AXUIElementCreateApplication(pid), kAXWindowsAttribute) ?? []128  var found: [[String: Any]] = []129  for w in wins { let sheets: [AXUIElement] = (attr(w, kAXChildrenAttribute) ?? []).filter { (attr($0, kAXRoleAttribute) as String?) == (kAXSheetRole as String) }130    for s in sheets { let buttons: [AXUIElement] = (attr(s, kAXChildrenAttribute) ?? []).filter { (attr($0, kAXRoleAttribute) as String?) == (kAXButtonRole as String) }131      found.append(["kind": "sheet", "buttons": buttons.compactMap { attr($0, kAXTitleAttribute) as String? }]) } }132  out(["ok": true, "dialogs": found])133case "dismiss-dialog":134  let pid = pidArg(2); _ = requireBrowser(pid)135  guard args.count > 3, ["Annuler", "Cancel", "Fermer", "Close", "OK"].contains(args[3]) else { fail("bouton non permis") } // closing only; never "Open", "Allow", "Save"136  guard AXIsProcessTrusted() else { fail("permission Accessibilité non accordée", 3) }137  let wins: [AXUIElement] = attr(AXUIElementCreateApplication(pid), kAXWindowsAttribute) ?? []138  for w in wins { for s in (attr(w, kAXChildrenAttribute) ?? [] as [AXUIElement]) where (attr(s, kAXRoleAttribute) as String?) == (kAXSheetRole as String) {139    for b in (attr(s, kAXChildrenAttribute) ?? [] as [AXUIElement]) where (attr(b, kAXTitleAttribute) as String?) == args[3] { out(["ok": AXUIElementPerformAction(b, kAXPressAction as CFString) == .success]); exit(0) } } }140  out(["ok": false, "error": "aucune boîte de dialogue correspondante"])141default: fail("verbe inconnu")142}143
Files main
Menu

Theme