From 0573a549a7e627dc32525db1295ff721ffeb14fc Mon Sep 17 00:00:00 2001 From: Kent Betita <153492344+kentbetita@users.noreply.github.com> Date: Fri, 23 Feb 2024 11:26:46 +0800 Subject: [PATCH] Add `accessibilityPermission` option (#177) Co-authored-by: Sindre Sorhus --- Sources/ActiveWinCLI/main.swift | 16 ++++++++++++---- index.d.ts | 9 +++++++++ index.test-d.ts | 5 ++++- lib/macos.js | 4 ++++ readme.md | 7 +++++++ 5 files changed, 36 insertions(+), 5 deletions(-) diff --git a/Sources/ActiveWinCLI/main.swift b/Sources/ActiveWinCLI/main.swift index fa260f0..59b10d4 100644 --- a/Sources/ActiveWinCLI/main.swift +++ b/Sources/ActiveWinCLI/main.swift @@ -71,8 +71,9 @@ func getWindowInformation(window: [String: Any], windowOwnerPID: pid_t) -> [Stri "memoryUsage": window[kCGWindowMemoryUsage as String] as? Int ?? 0 ] - // Only run the AppleScript if active window is a compatible browser. + // Run the AppleScript to get the URL if the active window is a compatible browser and accessibility permissions are enabled. if + !disableAccessibilityPermission, let bundleIdentifier = app.bundleIdentifier, let script = getActiveBrowserTabURLAppleScriptCommand(bundleIdentifier), let url = runAppleScript(source: script) @@ -83,17 +84,24 @@ func getWindowInformation(window: [String: Any], windowOwnerPID: pid_t) -> [Stri return output } +let disableAccessibilityPermission = CommandLine.arguments.contains("--no-accessibility-permission") let disableScreenRecordingPermission = CommandLine.arguments.contains("--no-screen-recording-permission") let enableOpenWindowsList = CommandLine.arguments.contains("--open-windows-list") -// Show accessibility permission prompt if needed. Required to get the complete window title. -if !AXIsProcessTrustedWithOptions(["AXTrustedCheckOptionPrompt": true] as CFDictionary) { +// Show accessibility permission prompt if needed. Required to get the URL of the active tab in browsers. +if + !disableAccessibilityPermission, + !AXIsProcessTrustedWithOptions(["AXTrustedCheckOptionPrompt": true] as CFDictionary) +{ print("active-win requires the accessibility permission in “System Settings › Privacy & Security › Accessibility”.") exit(1) } // Show screen recording permission prompt if needed. Required to get the complete window title. -if !disableScreenRecordingPermission && !hasScreenRecordingPermission() { +if + !disableScreenRecordingPermission, + !hasScreenRecordingPermission() +{ print("active-win requires the screen recording permission in “System Settings › Privacy & Security › Screen Recording”.") exit(1) } diff --git a/index.d.ts b/index.d.ts index fd4fd55..f2f360a 100644 --- a/index.d.ts +++ b/index.d.ts @@ -1,5 +1,14 @@ declare namespace activeWindow { interface Options { + /** + Enable the accessibility permission check. _(macOS)_ + + Setting this to `false` will prevent the accessibility permission prompt on macOS versions 10.15 and newer. The `url` property won't be retrieved. + + @default true + */ + readonly accessibilityPermission: boolean; + /** Enable the screen recording permission check. _(macOS)_ diff --git a/index.test-d.ts b/index.test-d.ts index c83e2d3..4065154 100644 --- a/index.test-d.ts +++ b/index.test-d.ts @@ -4,7 +4,10 @@ import {Result, LinuxResult, MacOSResult, WindowsResult, BaseOwner} from './inde expectType>(activeWindow()); -const result = activeWindow.sync({screenRecordingPermission: false}); +const result = activeWindow.sync({ + screenRecordingPermission: false, + accessibilityPermission: false +}); expectType(result); diff --git a/lib/macos.js b/lib/macos.js index 4b21bce..9a5bb1a 100644 --- a/lib/macos.js +++ b/lib/macos.js @@ -21,6 +21,10 @@ const getArguments = options => { } const args = []; + if (options.accessibilityPermission === false) { + args.push('--no-accessibility-permission'); + } + if (options.screenRecordingPermission === false) { args.push('--no-screen-recording-permission'); } diff --git a/readme.md b/readme.md index 5b071d0..d6da80a 100644 --- a/readme.md +++ b/readme.md @@ -50,6 +50,13 @@ Get metadata about the active window. Type: `object` +##### accessibilityPermission **(macOS only)** + +Type: `boolean`\ +Default: `true` + +Enable the accessibility permission check. Setting this to `false` will prevent the accessibility permission prompt on macOS versions 10.15 and newer. The `url` property won't be retrieved. + ##### screenRecordingPermission **(macOS only)** Type: `boolean`\