diff --git a/Sources/active-win/main.swift b/Sources/active-win/main.swift index fe0e5df..e8f17dc 100644 --- a/Sources/active-win/main.swift +++ b/Sources/active-win/main.swift @@ -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”.") @@ -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) } @@ -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, diff --git a/index.d.ts b/index.d.ts index 13fca36..450dd3d 100644 --- a/index.d.ts +++ b/index.d.ts @@ -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. @@ -107,7 +116,7 @@ declare const activeWin: { })(); ``` */ - (): Promise; + (options?: activeWin.Options): Promise; /** Synchronously get metadata about the [active window](https://en.wikipedia.org/wiki/Active_window) (title, id, bounds, owner, etc). @@ -132,7 +141,7 @@ declare const activeWin: { } ``` */ - sync(): activeWin.Result | undefined; + sync(options?: activeWin.Options): activeWin.Result | undefined; }; export = activeWin; diff --git a/index.js b/index.js index e18ec94..b7dc6b0 100644 --- a/index.js +++ b/index.js @@ -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'); diff --git a/index.test-d.ts b/index.test-d.ts index 7ef0f95..c565178 100644 --- a/index.test-d.ts +++ b/index.test-d.ts @@ -4,7 +4,7 @@ import {Result, LinuxResult, MacOSResult, WindowsResult} from '.'; expectType>(activeWin()); -const result = activeWin.sync(); +const result = activeWin.sync({ disableScreenRecordingPermission: false }); expectType(result); diff --git a/lib/macos.js b/lib/macos.js index ba4062a..cb10cf1 100644 --- a/lib/macos.js +++ b/lib/macos.js @@ -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); +}; diff --git a/readme.md b/readme.md index 7b17f45..08b50f5 100644 --- a/readme.md +++ b/readme.md @@ -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', @@ -45,13 +45,17 @@ const activeWin = require('active-win'); ## API -### activeWin() +### activeWin(options) -Returns a `Promise` with the result, or `Promise` 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` with the result, or `Promise` 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