Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add accessibilityPermission option #177

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 12 additions & 4 deletions Sources/ActiveWinCLI/main.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -83,17 +84,24 @@ func getWindowInformation(window: [String: Any], windowOwnerPID: pid_t) -> [Stri
return output
}

let disableAccessibilityPermission = CommandLine.arguments.contains("--no-accessibility-permission")
kentbetita marked this conversation as resolved.
Show resolved Hide resolved
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)
}
Expand Down
9 changes: 9 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
declare namespace activeWindow {
interface Options {
/**
Enable the accessibility permission check. _(macOS)_

kentbetita marked this conversation as resolved.
Show resolved Hide resolved
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)_

Expand Down
5 changes: 4 additions & 1 deletion index.test-d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@ import {Result, LinuxResult, MacOSResult, WindowsResult, BaseOwner} from './inde

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

const result = activeWindow.sync({screenRecordingPermission: false});
const result = activeWindow.sync({
screenRecordingPermission: false,
accessibilityPermission: false
});

expectType<Result | undefined>(result);

Expand Down
4 changes: 4 additions & 0 deletions lib/macos.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
}
Expand Down
7 changes: 7 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`\
Expand Down
Loading