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: $_" }