active-win
Advanced tools
Comparing version
80
index.js
'use strict'; | ||
const path = require('path'); | ||
const childProcess = require('child_process'); | ||
const pify = require('pify'); | ||
const bin = path.join(__dirname, 'main'); | ||
const xpropBin = 'xprop'; | ||
const xpropActiveArgs = ['-root', '\t$0', '_NET_ACTIVE_WINDOW']; | ||
const xpropDetailsArgs = ['-id']; | ||
const parseMac = stdout => { | ||
const parts = stdout.trimRight().split('\n'); | ||
return { | ||
title: parts[0], | ||
id: Number(parts[1]), | ||
app: parts[2], | ||
pid: Number(parts[3]) | ||
}; | ||
}; | ||
const parseLinux = linuxData => { | ||
const stdout = linuxData.stdout; | ||
const activeWindowId = linuxData.activeWindowId; | ||
const result = {}; | ||
for (const row of stdout.trim().split('\n')) { | ||
if (row.includes('=')) { | ||
const parts = row.split('='); | ||
result[parts[0].trim()] = parts[1].trim(); | ||
} else if (row.includes(':')) { | ||
const parts = row.split(':'); | ||
result[parts[0].trim()] = parts[1].trim(); | ||
} | ||
} | ||
const windowIdProperty = 'WM_CLIENT_LEADER(WINDOW)'; | ||
const resultKeys = Object.keys(result); | ||
const windowId = (resultKeys.indexOf(windowIdProperty) > 0 && | ||
parseInt(result[windowIdProperty].split('#').pop(), 16)) || activeWindowId; | ||
return { | ||
title: JSON.parse(result['_NET_WM_NAME(UTF8_STRING)']) || null, | ||
id: windowId, | ||
app: JSON.parse(result['WM_CLASS(STRING)'].split(',').pop()), | ||
pid: parseInt(result['_NET_WM_PID(CARDINAL)'], 10) | ||
}; | ||
}; | ||
const getActiveWindowId = activeWindowIdStdout => parseInt(activeWindowIdStdout.split('\t')[1], 16); | ||
module.exports = () => { | ||
if (process.platform === 'darwin') { | ||
return pify(childProcess.execFile)(bin).then(parseMac); | ||
return require('./lib/macos')(); | ||
} else if (process.platform === 'linux') { | ||
return pify(childProcess.execFile)(xpropBin, xpropActiveArgs).then( | ||
activeWindowIdStdout => { | ||
const activeWindowId = getActiveWindowId(activeWindowIdStdout); | ||
return pify(childProcess.execFile)(xpropBin, xpropDetailsArgs.concat(activeWindowId)).then(stdout => { | ||
return { | ||
activeWindowId, | ||
stdout | ||
}; | ||
}); | ||
} | ||
).then(parseLinux); | ||
return require('./lib/linux')(); | ||
} else if (process.platform === 'win32') { | ||
const windows = require('./windows'); | ||
return Promise.resolve(windows()); | ||
return require('./lib/windows')(); | ||
} | ||
return Promise.reject(new Error('macOS, Linux, and Windows only')); | ||
@@ -76,14 +17,7 @@ }; | ||
if (process.platform === 'darwin') { | ||
return parseMac(childProcess.execFileSync(bin, {encoding: 'utf8'})); | ||
return require('./lib/macos').sync(); | ||
} else if (process.platform === 'linux') { | ||
const activeWindowIdStdout = childProcess.execFileSync(xpropBin, xpropActiveArgs, {encoding: 'utf8'}); | ||
const activeWindowId = getActiveWindowId(activeWindowIdStdout); | ||
const stdout = childProcess.execFileSync(xpropBin, xpropDetailsArgs.concat(activeWindowId), {encoding: 'utf8'}); | ||
return parseLinux({ | ||
activeWindowId, | ||
stdout | ||
}); | ||
return require('./lib/linux').sync(); | ||
} else if (process.platform === 'win32') { | ||
const windows = require('./windows'); | ||
return windows(); | ||
return require('./lib/windows').sync(); | ||
} | ||
@@ -90,0 +24,0 @@ |
110
package.json
{ | ||
"name": "active-win", | ||
"version": "2.0.0", | ||
"description": "Get the title / window id / app name / process ID of the active window (macOS, Linux, Windows)", | ||
"license": "MIT", | ||
"repository": "sindresorhus/active-win", | ||
"author": { | ||
"name": "Sindre Sorhus", | ||
"email": "sindresorhus@gmail.com", | ||
"url": "sindresorhus.com" | ||
}, | ||
"engines": { | ||
"node": ">=4" | ||
}, | ||
"scripts": { | ||
"test": "xo && ava", | ||
"make": "clang main.m -fmodules -mmacosx-version-min=10.7 -o main" | ||
}, | ||
"files": [ | ||
"index.js", | ||
"windows.js", | ||
"main" | ||
], | ||
"keywords": [ | ||
"macos", | ||
"linux", | ||
"windows", | ||
"app", | ||
"application", | ||
"window", | ||
"win", | ||
"active", | ||
"focused", | ||
"current", | ||
"title", | ||
"name", | ||
"id", | ||
"pid", | ||
"screenshot", | ||
"capture" | ||
], | ||
"dependencies": { | ||
"pify": "^2.3.0" | ||
}, | ||
"devDependencies": { | ||
"ava": "*", | ||
"xo": "*" | ||
}, | ||
"optionalDependencies": { | ||
"ffi": "^2.2.0", | ||
"ref": "^1.3.4", | ||
"ref-wchar": "^1.0.2" | ||
} | ||
"name": "active-win", | ||
"version": "3.0.0", | ||
"description": "Get metadata about the active window (title, id, bounds, owner, etc). Works on macOS, Linux, Windows.", | ||
"license": "MIT", | ||
"repository": "sindresorhus/active-win", | ||
"author": { | ||
"name": "Sindre Sorhus", | ||
"email": "sindresorhus@gmail.com", | ||
"url": "sindresorhus.com" | ||
}, | ||
"engines": { | ||
"node": ">=4" | ||
}, | ||
"scripts": { | ||
"test": "xo && ava", | ||
"build": "swift build --configuration=release -Xswiftc -static-stdlib && mv .build/release/active-win main", | ||
"prepublishOnly": "npm run build" | ||
}, | ||
"files": [ | ||
"index.js", | ||
"lib", | ||
"main" | ||
], | ||
"keywords": [ | ||
"macos", | ||
"linux", | ||
"windows", | ||
"app", | ||
"application", | ||
"window", | ||
"win", | ||
"active", | ||
"focused", | ||
"current", | ||
"title", | ||
"name", | ||
"id", | ||
"pid", | ||
"screenshot", | ||
"capture", | ||
"metadata", | ||
"bounds", | ||
"memory", | ||
"usage", | ||
"bundleid" | ||
], | ||
"dependencies": { | ||
"pify": "^3.0.0" | ||
}, | ||
"devDependencies": { | ||
"ava": "*", | ||
"xo": "*" | ||
}, | ||
"optionalDependencies": { | ||
"ffi": "^2.2.0", | ||
"ref": "^1.3.4", | ||
"ref-wchar": "^1.0.2" | ||
} | ||
} |
# active-win [](https://travis-ci.org/sindresorhus/active-win) | ||
Get the title / window id / app name / process ID of the [active window](https://en.wikipedia.org/wiki/Active_window) *(macOS, Linux, Windows)* | ||
Get metadata about the [active window](https://en.wikipedia.org/wiki/Active_window) (title, id, bounds, owner, etc) | ||
Works on macOS, Linux, Windows. | ||
## Install | ||
``` | ||
$ npm install --save active-win | ||
$ npm install active-win | ||
``` | ||
@@ -18,13 +20,24 @@ | ||
activeWin().then(result => { | ||
console.log(result); | ||
(async () => { | ||
console.log(await activeWin()); | ||
/* | ||
{ | ||
title: 'npm install', | ||
id: 54, | ||
app: 'Terminal', | ||
pid: 368 | ||
title: 'Unicorns - Google Search', | ||
id: 5762, | ||
bounds: { | ||
x: 0, | ||
y: 0, | ||
height: 900, | ||
width: 1440 | ||
}, | ||
owner: { | ||
name: 'Google Chrome', | ||
processId: 310, | ||
bundleId: 'com.google.Chrome', | ||
path: '/Applications/Google Chrome.app' | ||
}, | ||
memoryUsage: 11015432 | ||
} | ||
*/ | ||
}); | ||
})(); | ||
``` | ||
@@ -37,7 +50,7 @@ | ||
Returns a `Promise` for the result `Object`. | ||
Returns a `Promise<Object>` with the result. | ||
### activeWin.sync() | ||
Returns the result `Object`. | ||
Returns an `Object` with the result. | ||
@@ -47,6 +60,15 @@ | ||
- `title` - Window title | ||
- `id` - Window ID | ||
- `app` - App owning the window | ||
- `pid` - Process ID of the app owning the window | ||
- `title` *(string)* - Window title | ||
- `id` *(number)* - Window identifier | ||
- `bounds` *(Object)* - Window position and size *(macOS only)* | ||
- `x` *(number)* | ||
- `y` *(number)* | ||
- `width` *(number)* | ||
- `height` *(number)* | ||
- `owner` *(Object)* - App that owns the window | ||
- `name` *(string)* - Name of the app | ||
- `processId` *(number)* - Process identifier | ||
- `bundleId` *(string)* - Bundle identifier *(macOS only)* | ||
- `path` *(string)* - Path to the app *(macOS and Windows only)* | ||
- `memoryUsage` *(number)* - Memory usage by the window *(macOS only)* | ||
@@ -68,2 +90,3 @@ | ||
- [Sindre Sorhus](https://github.com/sindresorhus) | ||
- [Sebastián Ramírez](https://github.com/tiangolo) | ||
@@ -74,2 +97,2 @@ | ||
MIT © [Sindre Sorhus](https://sindresorhus.com) | ||
MIT |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
10305793
41282.08%8
33.33%177
9.94%94
32.39%4
100%+ Added
- Removed
Updated