diff --git a/Sources/active-win/main.swift b/Sources/active-win/main.swift index 47359c1..fe0e5df 100644 --- a/Sources/active-win/main.swift +++ b/Sources/active-win/main.swift @@ -1,11 +1,5 @@ import AppKit -@discardableResult -func runAppleScript(source: String) -> String? { - NSAppleScript(source: source)?.executeAndReturnError(nil).stringValue -} - -// Format the AppleScript command for Chrome, Safari, Brave, and Edge func getActiveBrowserTabURLAppleScriptCommand(_ appName: String) -> String? { switch appName { case "Google Chrome", "Brave Browser", "Microsoft Edge": @@ -17,25 +11,6 @@ func getActiveBrowserTabURLAppleScriptCommand(_ appName: String) -> String? { } } -// Show the system prompt if there's no permission. -func hasScreenRecordingPermission() -> Bool { - CGDisplayStream( - dispatchQueueDisplay: CGMainDisplayID(), - outputWidth: 1, - outputHeight: 1, - pixelFormat: Int32(kCVPixelFormatType_32BGRA), - properties: nil, - queue: DispatchQueue.global(), - handler: { _, _, _, _ in } - ) != nil -} - -// Serialize data dict to JSON -func toJson(_ data: T) throws -> String { - let json = try JSONSerialization.data(withJSONObject: data) - return String(data: json, encoding: .utf8)! -} - // 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”.") @@ -58,14 +33,14 @@ for window in windows { continue } - // Skip transparent windows, like with Chrome + // Skip transparent windows, like with Chrome. if (window[kCGWindowAlpha as String] as! Double) == 0 { continue } let bounds = CGRect(dictionaryRepresentation: window[kCGWindowBounds as String] as! CFDictionary)! - // Skip tiny windows, like the Chrome link hover statusbar + // Skip tiny windows, like the Chrome link hover statusbar. let minWinSize: CGFloat = 50 if bounds.width < minWinSize || bounds.height < minWinSize { continue @@ -73,7 +48,7 @@ for window in windows { let appPid = window[kCGWindowOwnerPID as String] as! pid_t - // This can't fail as we're only dealing with apps + // This can't fail as we're only dealing with apps. let app = NSRunningApplication(processIdentifier: appPid)! let appName = window[kCGWindowOwnerName as String] as! String @@ -96,7 +71,7 @@ for window in windows { "memoryUsage": window[kCGWindowMemoryUsage as String] as! Int ] - // Only run the AppleScript if active window is a compatible browser + // Only run the AppleScript if active window is a compatible browser. if let script = getActiveBrowserTabURLAppleScriptCommand(appName), let url = runAppleScript(source: script) diff --git a/Sources/active-win/utilities.swift b/Sources/active-win/utilities.swift new file mode 100644 index 0000000..22cbb98 --- /dev/null +++ b/Sources/active-win/utilities.swift @@ -0,0 +1,27 @@ +import AppKit + + +@discardableResult +func runAppleScript(source: String) -> String? { + NSAppleScript(source: source)?.executeAndReturnError(nil).stringValue +} + + +func toJson(_ data: T) throws -> String { + let json = try JSONSerialization.data(withJSONObject: data) + return String(data: json, encoding: .utf8)! +} + + +// Show the system prompt if there's no permission. +func hasScreenRecordingPermission() -> Bool { + CGDisplayStream( + dispatchQueueDisplay: CGMainDisplayID(), + outputWidth: 1, + outputHeight: 1, + pixelFormat: Int32(kCVPixelFormatType_32BGRA), + properties: nil, + queue: DispatchQueue.global(), + handler: { _, _, _, _ in } + ) != nil +} diff --git a/index.test-d.ts b/index.test-d.ts index e00b84d..7ef0f95 100644 --- a/index.test-d.ts +++ b/index.test-d.ts @@ -20,6 +20,7 @@ if (result) { expectType(result.owner.processId); expectType(result.owner.path); expectType(result.memoryUsage); + if (result.platform === 'macos') { expectType(result); expectType(result.owner.bundleId); diff --git a/license b/license index e7af2f7..fa7ceba 100644 --- a/license +++ b/license @@ -1,6 +1,6 @@ MIT License -Copyright (c) Sindre Sorhus (sindresorhus.com) +Copyright (c) Sindre Sorhus (https://sindresorhus.com) Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: diff --git a/package.json b/package.json index 9a10b00..b86fb07 100644 --- a/package.json +++ b/package.json @@ -1,13 +1,14 @@ { "name": "active-win", "version": "6.0.0", - "description": "Get metadata about the active window (title, id, bounds, owner, etc). Works on macOS, Linux, Windows.", + "description": "Get metadata about the active window (title, id, bounds, owner, URL, etc). Works on macOS, Linux, Windows.", "license": "MIT", "repository": "sindresorhus/active-win", + "funding": "https://github.com/sponsors/sindresorhus", "author": { "name": "Sindre Sorhus", "email": "sindresorhus@gmail.com", - "url": "sindresorhus.com" + "url": "https://sindresorhus.com" }, "engines": { "node": ">=8" @@ -44,7 +45,13 @@ "bounds", "memory", "usage", - "bundleid" + "bundleid", + "browser", + "url", + "chrome", + "safari", + "edge", + "brave" ], "devDependencies": { "ava": "^2.4.0", diff --git a/readme.md b/readme.md index 310d8c4..3fc78b3 100644 --- a/readme.md +++ b/readme.md @@ -6,14 +6,12 @@ Works on macOS, Linux, Windows. Users on macOS 10.13 or earlier needs to download the [Swift runtime support libraries](https://support.apple.com/kb/DL1998). - ## Install ``` $ npm install active-win ``` - ## Usage ```js @@ -55,7 +53,6 @@ Returns a `Promise` with the result, or `Promise` if there is Returns an `Object` with the result, or `undefined` if there is no active window. - ## Result - `platform` *(string)* - `'macos'` | `'linux'` | `'windows'` @@ -74,20 +71,17 @@ Returns an `Object` with the result, or `undefined` if there is no active window - `url` *(string?)* - URL of the active browser tab if the active window is Safari, Chrome, Edge, or Brave *(macOS only)* - `memoryUsage` *(number)* - Memory usage by the window owner process - ## OS support It works on macOS, Linux, and Windows 7+. **Note**: On Windows, there isn't a clear notion of a "Window ID". Instead it returns the memory address of the window "handle" in the `id` property. That "handle" is unique per window, so it can be used to identify them. [Read more…](https://msdn.microsoft.com/en-us/library/windows/desktop/ms632597(v=vs.85).aspx#window_handle). - ## Related - [active-win-cli](https://github.com/sindresorhus/active-win-cli) - CLI for this module - [active-win-log](https://github.com/uglow/active-win-log) - Window-usage logging CLI using this module - ## Maintainers - [Sindre Sorhus](https://github.com/sindresorhus)