Skip to content

Commit 0573a54

Browse files
Add accessibilityPermission option (#177)
Co-authored-by: Sindre Sorhus <sindresorhus@gmail.com>
1 parent 97aca0a commit 0573a54

File tree

5 files changed

+36
-5
lines changed

5 files changed

+36
-5
lines changed

Sources/ActiveWinCLI/main.swift

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -71,8 +71,9 @@ func getWindowInformation(window: [String: Any], windowOwnerPID: pid_t) -> [Stri
7171
"memoryUsage": window[kCGWindowMemoryUsage as String] as? Int ?? 0
7272
]
7373

74-
// Only run the AppleScript if active window is a compatible browser.
74+
// Run the AppleScript to get the URL if the active window is a compatible browser and accessibility permissions are enabled.
7575
if
76+
!disableAccessibilityPermission,
7677
let bundleIdentifier = app.bundleIdentifier,
7778
let script = getActiveBrowserTabURLAppleScriptCommand(bundleIdentifier),
7879
let url = runAppleScript(source: script)
@@ -83,17 +84,24 @@ func getWindowInformation(window: [String: Any], windowOwnerPID: pid_t) -> [Stri
8384
return output
8485
}
8586

87+
let disableAccessibilityPermission = CommandLine.arguments.contains("--no-accessibility-permission")
8688
let disableScreenRecordingPermission = CommandLine.arguments.contains("--no-screen-recording-permission")
8789
let enableOpenWindowsList = CommandLine.arguments.contains("--open-windows-list")
8890

89-
// Show accessibility permission prompt if needed. Required to get the complete window title.
90-
if !AXIsProcessTrustedWithOptions(["AXTrustedCheckOptionPrompt": true] as CFDictionary) {
91+
// Show accessibility permission prompt if needed. Required to get the URL of the active tab in browsers.
92+
if
93+
!disableAccessibilityPermission,
94+
!AXIsProcessTrustedWithOptions(["AXTrustedCheckOptionPrompt": true] as CFDictionary)
95+
{
9196
print("active-win requires the accessibility permission in “System Settings › Privacy & Security › Accessibility”.")
9297
exit(1)
9398
}
9499

95100
// Show screen recording permission prompt if needed. Required to get the complete window title.
96-
if !disableScreenRecordingPermission && !hasScreenRecordingPermission() {
101+
if
102+
!disableScreenRecordingPermission,
103+
!hasScreenRecordingPermission()
104+
{
97105
print("active-win requires the screen recording permission in “System Settings › Privacy & Security › Screen Recording”.")
98106
exit(1)
99107
}

index.d.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,14 @@
11
declare namespace activeWindow {
22
interface Options {
3+
/**
4+
Enable the accessibility permission check. _(macOS)_
5+
6+
Setting this to `false` will prevent the accessibility permission prompt on macOS versions 10.15 and newer. The `url` property won't be retrieved.
7+
8+
@default true
9+
*/
10+
readonly accessibilityPermission: boolean;
11+
312
/**
413
Enable the screen recording permission check. _(macOS)_
514

index.test-d.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,10 @@ import {Result, LinuxResult, MacOSResult, WindowsResult, BaseOwner} from './inde
44

55
expectType<Promise<Result | undefined>>(activeWindow());
66

7-
const result = activeWindow.sync({screenRecordingPermission: false});
7+
const result = activeWindow.sync({
8+
screenRecordingPermission: false,
9+
accessibilityPermission: false
10+
});
811

912
expectType<Result | undefined>(result);
1013

lib/macos.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,10 @@ const getArguments = options => {
2121
}
2222

2323
const args = [];
24+
if (options.accessibilityPermission === false) {
25+
args.push('--no-accessibility-permission');
26+
}
27+
2428
if (options.screenRecordingPermission === false) {
2529
args.push('--no-screen-recording-permission');
2630
}

readme.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,13 @@ Get metadata about the active window.
5050

5151
Type: `object`
5252

53+
##### accessibilityPermission **(macOS only)**
54+
55+
Type: `boolean`\
56+
Default: `true`
57+
58+
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.
59+
5360
##### screenRecordingPermission **(macOS only)**
5461

5562
Type: `boolean`\

0 commit comments

Comments
 (0)