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

Add disableScreenRecordingPermission option #95

Closed
wants to merge 1 commit into from
Closed
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
10 changes: 7 additions & 3 deletions Sources/active-win/main.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ func getActiveBrowserTabURLAppleScriptCommand(_ appName: String) -> String? {
}
}

let disableScreenRecordingPermission = CommandLine.arguments.contains("--disable-screen-recording-permission")

// Show accessibility permission prompt if needed. Required to get the complete window title.
if !AXIsProcessTrustedWithOptions(["AXTrustedCheckOptionPrompt": true] as CFDictionary) {
print("active-win requires the accessibility permission in “System Preferences › Security & Privacy › Privacy › Accessibility”.")
Expand All @@ -21,7 +23,7 @@ let frontmostAppPID = NSWorkspace.shared.frontmostApplication!.processIdentifier
let windows = CGWindowListCopyWindowInfo([.optionOnScreenOnly, .excludeDesktopElements], kCGNullWindowID) as! [[String: Any]]

// Show screen recording permission prompt if needed. Required to get the complete window title.
if !hasScreenRecordingPermission() {
if !disableScreenRecordingPermission && !hasScreenRecordingPermission() {
print("active-win requires the screen recording permission in “System Preferences › Security & Privacy › Privacy › Screen Recording”.")
exit(1)
}
Expand Down Expand Up @@ -50,11 +52,13 @@ for window in windows {

// This can't fail as we're only dealing with apps.
let app = NSRunningApplication(processIdentifier: appPid)!

let appName = window[kCGWindowOwnerName as String] as! String

let windowTitle = disableScreenRecordingPermission ? "" : window[kCGWindowName as String] as? String ?? ""

var dict: [String: Any] = [
"title": window[kCGWindowName as String] as? String ?? "",
"title": windowTitle,
"id": window[kCGWindowNumber as String] as! Int,
"bounds": [
"x": bounds.origin.x,
Expand Down
13 changes: 11 additions & 2 deletions index.d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,13 @@
declare namespace activeWin {
interface Options {
/**
Disable the screen recording permission check (macOS)

Setting this to true will prevent the Screen Recording permission prompt on macOS versions 10.15 and newer. The `title` property will always be set to an empty string.
*/
disableScreenRecordingPermission: boolean;
}

interface BaseOwner {
/**
Name of the app.
Expand Down Expand Up @@ -107,7 +116,7 @@ declare const activeWin: {
})();
```
*/
(): Promise<activeWin.Result | undefined>;
(options?: activeWin.Options): Promise<activeWin.Result | undefined>;

/**
Synchronously get metadata about the [active window](https://en.wikipedia.org/wiki/Active_window) (title, id, bounds, owner, etc).
Expand All @@ -132,7 +141,7 @@ declare const activeWin: {
}
```
*/
sync(): activeWin.Result | undefined;
sync(options?: activeWin.Options): activeWin.Result | undefined;
};

export = activeWin;
16 changes: 8 additions & 8 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,32 +1,32 @@
'use strict';

module.exports = () => {
module.exports = options => {
if (process.platform === 'darwin') {
return require('./lib/macos')();
return require('./lib/macos')(options);
}

if (process.platform === 'linux') {
return require('./lib/linux')();
return require('./lib/linux')(options);
}

if (process.platform === 'win32') {
return require('./lib/windows')();
return require('./lib/windows')(options);
}

return Promise.reject(new Error('macOS, Linux, and Windows only'));
};

module.exports.sync = () => {
module.exports.sync = options => {
if (process.platform === 'darwin') {
return require('./lib/macos').sync();
return require('./lib/macos').sync(options);
}

if (process.platform === 'linux') {
return require('./lib/linux').sync();
return require('./lib/linux').sync(options);
}

if (process.platform === 'win32') {
return require('./lib/windows').sync();
return require('./lib/windows').sync(options);
}

throw new Error('macOS, Linux, and Windows only');
Expand Down
2 changes: 1 addition & 1 deletion index.test-d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import {Result, LinuxResult, MacOSResult, WindowsResult} from '.';

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

const result = activeWin.sync();
const result = activeWin.sync({ disableScreenRecordingPermission: false });

expectType<Result | undefined>(result);

Expand Down
22 changes: 19 additions & 3 deletions lib/macos.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,25 @@ const parseMac = stdout => {
}
};

module.exports = async () => {
const {stdout} = await execFile(bin);
const getArguments = options => {
if (!options) {
return [];
}

const args = [];
if (options.disableScreenRecordingPermission === true) {
args.push('--disable-screen-recording-permission');
}

return args;
};

module.exports = async options => {
const {stdout} = await execFile(bin, getArguments(options));
return parseMac(stdout);
};

module.exports.sync = () => parseMac(childProcess.execFileSync(bin, {encoding: 'utf8'}));
module.exports.sync = options => {
const stdout = childProcess.execFileSync(bin, getArguments(options), {encoding: 'utf8'});
return parseMac(stdout);
};
14 changes: 9 additions & 5 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ $ npm install active-win
const activeWin = require('active-win');

(async () => {
console.log(await activeWin());
console.log(await activeWin(options));
/*
{
title: 'Unicorns - Google Search',
Expand All @@ -45,13 +45,17 @@ const activeWin = require('active-win');

## API

### activeWin()
### activeWin(options)

Returns a `Promise<Object>` with the result, or `Promise<undefined>` if there is no active window or if the information is not available.
Accepts an optional dictionary `Options` as only argument and returns a `Promise<Object>` with the result, or `Promise<undefined>` if there is no active window or if the information is not available.

### activeWin.sync()
### activeWin.sync(options)

Returns an `Object` with the result, or `undefined` if there is no active window.
Accepts an optional dictionary `Options` as only argument and returns an `Object` with the result, or `undefined` if there is no active window.

## Options

- `disableScreenRecordingPermission` *(boolean)* - Disable the screen recording permission check (macOS). Setting this to true will prevent the Screen Recording permission prompt on macOS versions 10.15 and newer. The `title` property will always be set to an empty string.

## Result

Expand Down