Repository: octalmage/active-window
Branch: master
Commit: cefad4d9ee4d
Files: 9
Total size: 6.0 KB
Directory structure:
gitextract_xlrd6azz/
├── .gitignore
├── README.md
├── configs.json
├── index.js
├── package.json
└── scripts/
├── linux.sh
├── mac.scpt
├── mac.sh
└── windows.ps1
================================================
FILE CONTENTS
================================================
================================================
FILE: .gitignore
================================================
node_modules
================================================
FILE: README.md
================================================
# Attention, this library is not maintained anymore!!!
# active-window
> Get active window title in Node.js.
Compatible with Linux, Windows 7+, and OSX;
## Usage
```javascript
var monitor = require('active-window');
callback = function(window){
try {
console.log("App: " + window.app);
console.log("Title: " + window.title);
}catch(err) {
console.log(err);
}
}
/*Watch the active window
@callback
@number of requests; infinity = -1
@interval between requests
*/
//monitor.getActiveWindow(callback,-1,1);
//Get the current active window
monitor.getActiveWindow(callback);
```
## Tested on
- Windows
- Windows 10
- Windows 7
- Linux
- Raspbian [lxdm]
- Debian 8 [cinnamon]
- OSX
- Yosemite 10.10.1
## TODO
- Test on more operating systems.
- Use native APIs.
## License
MIT
================================================
FILE: configs.json
================================================
{
"linux": {
"bin" :"sh",
"parameters" : [],
"script_url" : "scripts/linux.sh"
},
"mac" :{
"bin" : "sh",
"parameters" : [],
"script_url" : "scripts/mac.sh",
"subscript_url":"scripts/mac.scpt"
},
"win32":{
"bin" :"powershell",
"parameters" : ["-ExecutionPolicy", "Bypass","-File"],
"script_url" : "scripts\\windows.ps1"
}
}
================================================
FILE: index.js
================================================
var fs = require('fs');
var config = getConfig();
/**
* This callback handle the response request by getActiveWindow function
* @callback getActiveWindowCallback
* @param {app: string, window: string} window
*/
/**
* Get the active window
* @param {getActiveWindowCallback} callback - The callback that handles the response.
* @param {integer} [repeats = 1] - Number of repeats; Use -1 to infinity repeats
* @param {float} [interval = 0] - Loop interval in seconds. For milliseconds use fraction (0.1 = 100ms)
*/
exports.getActiveWindow = function(callback,repeats,interval){
const spawn = require('child_process').spawn;
interval = (interval) ? interval : 0;
repeats = (repeats) ? repeats : 1;
//Scape negative number of repeats on Windows OS
if (process.platform == 'win32' && repeats < 0 ){
repeats = '\\-1';
}
parameters = config.parameters;
parameters.push(repeats);
parameters.push(process.platform == 'win32' ? (interval * 1000 | 0) : interval);
//Run shell script
const ls = spawn(config.bin,parameters);
ls.stdout.setEncoding('utf8');
//Obtain successful response from script
ls.stdout.on('data', function(stdout){
callback(reponseTreatment(stdout.toString()));
});
//Obtain error response from script
ls.stderr.on("data",function(stderr){
throw stderr.toString();
});
ls.stdin.end();
}
/**
* Treat and format the response string and put it into a object
* @function reponseTreatment
* @param {string} String received from script
*/
function reponseTreatment(response){
window = {};
if(process.platform == 'linux'){
response = response.replace(/(WM_CLASS|WM_NAME)(\(\w+\)\s=\s)/g,'').split("\n",2);
window.app = response[0];
window.title = response[1];
}else if (process.platform == 'win32'){
response = response.replace(/(@{ProcessName=| AppTitle=)/g,'').slice(0,-1).split(';',2);
window.app = response[0];
window.title = response[1];
}else if(process.platform == 'darwin'){
response = response.split(",");
window.app = response[0];
window.title = response[1].replace(/\n$/, "").replace(/^\s/, "");
}
return window;
}
/**
* Get script config accordingly the operating system
* @function getConfig
*/
function getConfig(){
//Retrieve configs
var configs = JSON.parse(fs.readFileSync(__dirname+'/configs.json', 'utf8'));
var path = require("path");
switch(process.platform){
case 'linux':
case 'linux2':
config = configs.linux
break;
case 'win32':
config = configs.win32
break;
case 'darwin':
config = configs.mac;
break;
default:
throw "Operating System not supported yet. "+process.platform;
}
//Append directory to script url
script_url = path.join(__dirname,config.script_url);
config.parameters.push(script_url);
//Append directory to subscript url on OSX
if(process.platform=="darwin"){
config.parameters.push(path.join(__dirname,config.subscript_url));
}
return config;
}
================================================
FILE: package.json
================================================
{
"name": "active-window",
"version": "0.1.0",
"description": "Get active window title.",
"main": "index.js",
"contributors":[{ "name" : "Jason Stallings "
, "email" : "jason@stallin.gs"
, "url" : ""},
{ "name" : "Robson Vieira"
, "email" : "robsonvnasc@gmail.com"
, "url" : ""}
],
"license": "MIT",
"dependencies": {}
}
================================================
FILE: scripts/linux.sh
================================================
#!/bin/bash
n=$1
while [ 0 != $n ]
do
xprop -id $(xprop -root 32x '\t$0' _NET_ACTIVE_WINDOW | cut -f 2) WM_CLASS WM_NAME
sleep $2
if [ "$n" -gt "0" ]
then
n=`expr $n - 1`
fi
done
================================================
FILE: scripts/mac.scpt
================================================
#!/usr/bin/env osascript
global frontApp, frontAppName, windowTitle
set windowTitle to ""
tell application "System Events"
set frontApp to first application process whose frontmost is true
set frontAppName to name of frontApp
tell process frontAppName
tell (1st window whose value of attribute "AXMain" is true)
set windowTitle to value of attribute "AXTitle"
end tell
end tell
end tell
return {frontAppName,windowTitle}
================================================
FILE: scripts/mac.sh
================================================
#!/bin/bash
#Using bash script to run osascript because applescript do not print to stdout
x=$2
while(($x != "0" ))
do
osascript $1
if [ "$x" -gt "0" ]
then
((x-=1))
fi
sleep $3
done
================================================
FILE: scripts/windows.ps1
================================================
[CmdletBinding()]
Param(
[string]$n,[string]$interval
)
Add-Type @"
using System;
using System.Runtime.InteropServices;
public class UserWindows {
[DllImport("user32.dll")]
public static extern IntPtr GetForegroundWindow();
}
"@
try {
while($n -ne 0){
$ActiveHandle = [UserWindows]::GetForegroundWindow()
$Process = Get-Process | ? {$_.MainWindowHandle -eq $activeHandle}
$string = $Process | Select ProcessName, @{Name="AppTitle";Expression= {($_.MainWindowTitle)}}
Write-Host -NoNewline $string
Start-Sleep -m $interval
If ($n -gt 0) {$n-=1}
}
} catch {
Write-Error "Failed to get active Window details. More Info: $_"
}
gitextract_xlrd6azz/
├── .gitignore
├── README.md
├── configs.json
├── index.js
├── package.json
└── scripts/
├── linux.sh
├── mac.scpt
├── mac.sh
└── windows.ps1
SYMBOL INDEX (2 symbols across 1 files)
FILE: index.js
function reponseTreatment (line 53) | function reponseTreatment(response){
function getConfig (line 75) | function getConfig(){
Condensed preview — 9 files, each showing path, character count, and a content snippet. Download the .json file or copy for the full structured content (7K chars).
[
{
"path": ".gitignore",
"chars": 12,
"preview": "node_modules"
},
{
"path": "README.md",
"chars": 823,
"preview": "# Attention, this library is not maintained anymore!!!\n\n# active-window\n> Get active window title in Node.js.\n\nCompatibl"
},
{
"path": "configs.json",
"chars": 384,
"preview": "{\n \"linux\": {\n \"bin\" :\"sh\",\n \"parameters\" : [],\n \"script_url\" : \"scripts/linux.sh\"\n },\n \"mac\" :{\n \"bin"
},
{
"path": "index.js",
"chars": 3036,
"preview": "var fs = require('fs');\nvar config = getConfig();\n\n/**\n * This callback handle the response request by getActiveWindow f"
},
{
"path": "package.json",
"chars": 441,
"preview": "{\n \"name\": \"active-window\",\n \"version\": \"0.1.0\",\n \"description\": \"Get active window title.\",\n \"main\": \"index.js\",\n "
},
{
"path": "scripts/linux.sh",
"chars": 199,
"preview": "#!/bin/bash\nn=$1\nwhile [ 0 != $n ]\ndo\n xprop -id $(xprop -root 32x '\\t$0' _NET_ACTIVE_WINDOW | cut -f 2) WM_CLASS WM_N"
},
{
"path": "scripts/mac.scpt",
"chars": 432,
"preview": "#!/usr/bin/env osascript\nglobal frontApp, frontAppName, windowTitle\nset windowTitle to \"\"\ntell application \"System Event"
},
{
"path": "scripts/mac.sh",
"chars": 197,
"preview": "#!/bin/bash\n#Using bash script to run osascript because applescript do not print to stdout\nx=$2\nwhile(($x != \"0\" ))\ndo\n "
},
{
"path": "scripts/windows.ps1",
"chars": 653,
"preview": "[CmdletBinding()]\nParam(\n[string]$n,[string]$interval\n)\nAdd-Type @\"\n using System;\n using System.Runtime.InteropServic"
}
]
About this extraction
This page contains the full source code of the octalmage/active-window GitHub repository, extracted and formatted as plain text for AI agents and large language models (LLMs). The extraction includes 9 files (6.0 KB), approximately 1.9k tokens, and a symbol index with 2 extracted functions, classes, methods, constants, and types. Use this with OpenClaw, Claude, ChatGPT, Cursor, Windsurf, or any other AI tool that accepts text input. You can copy the full output to your clipboard or download it as a .txt file.
Extracted by GitExtract — free GitHub repo to text converter for AI. Built by Nikandr Surkov.