[
  {
    "path": ".gitignore",
    "content": "node_modules"
  },
  {
    "path": "README.md",
    "content": "# Attention, this library is not maintained anymore!!!\n\n# active-window\n> Get active window title in Node.js.\n\nCompatible with Linux, Windows 7+, and OSX;\n\n## Usage\n\n```javascript\nvar monitor = require('active-window');\n\ncallback = function(window){\n  try {\n    console.log(\"App: \" + window.app);\n    console.log(\"Title: \" + window.title);\n  }catch(err) {\n      console.log(err);\n  } \n}\n/*Watch the active window \n  @callback\n  @number of requests; infinity = -1 \n  @interval between requests\n*/\n//monitor.getActiveWindow(callback,-1,1);\n\n//Get the current active window\nmonitor.getActiveWindow(callback);\n\n\n```\n## Tested on\n- Windows\n - Windows 10\n - Windows 7\n- Linux \n  - Raspbian [lxdm]\n  - Debian 8 [cinnamon]\n- OSX\n  - Yosemite 10.10.1\n\n## TODO\n\n- Test on more operating systems.\n- Use native APIs. \n\n## License\n\nMIT\n"
  },
  {
    "path": "configs.json",
    "content": "{\n  \"linux\": {\n    \"bin\"    :\"sh\",\n    \"parameters\" : [],\n    \"script_url\" : \"scripts/linux.sh\"\n  },\n  \"mac\" :{\n    \"bin\"    : \"sh\",\n    \"parameters\" : [],\n    \"script_url\" : \"scripts/mac.sh\",\n    \"subscript_url\":\"scripts/mac.scpt\"\n  },\n  \"win32\":{\n    \"bin\"    :\"powershell\",\n    \"parameters\" : [\"-ExecutionPolicy\", \"Bypass\",\"-File\"],\n    \"script_url\" : \"scripts\\\\windows.ps1\"\n  }\n}\n"
  },
  {
    "path": "index.js",
    "content": "var fs = require('fs');\nvar config = getConfig();\n\n/**\n * This callback handle the response request by getActiveWindow function\n * @callback getActiveWindowCallback\n * @param {app: string, window: string} window\n */\n\n/**\n* Get the active window\n* @param {getActiveWindowCallback} callback - The callback that handles the response.\n* @param {integer} [repeats  = 1] - Number of repeats; Use -1 to infinity repeats\n* @param {float}   [interval = 0] - Loop interval in seconds. For milliseconds use fraction (0.1 = 100ms)\n*/\nexports.getActiveWindow = function(callback,repeats,interval){\n  const spawn = require('child_process').spawn;\n\n  interval = (interval) ?  interval : 0;\n  repeats = (repeats) ? repeats : 1;\n\n  //Scape negative number of repeats on Windows OS\n  if (process.platform == 'win32' && repeats < 0 ){\n    repeats = '\\\\-1';\n  }\n\n  parameters  = config.parameters;\n  parameters.push(repeats);\n  parameters.push(process.platform == 'win32' ? (interval * 1000 | 0) : interval);\n\n  //Run shell script\n  const ls  = spawn(config.bin,parameters);\n  ls.stdout.setEncoding('utf8');\n\n  //Obtain successful response from script\n  ls.stdout.on('data', function(stdout){\n    callback(reponseTreatment(stdout.toString()));\n  });\n\n  //Obtain error response from script\n  ls.stderr.on(\"data\",function(stderr){\n   throw stderr.toString();\n  });\n\n  ls.stdin.end();\n}\n\n/**\n* Treat and format the response string and put it into a object\n* @function reponseTreatment\n* @param {string} String received from script\n*/\nfunction reponseTreatment(response){\n  window = {};\n  if(process.platform == 'linux'){\n    response = response.replace(/(WM_CLASS|WM_NAME)(\\(\\w+\\)\\s=\\s)/g,'').split(\"\\n\",2);\n    window.app = response[0];\n    window.title = response[1];\n  }else if (process.platform == 'win32'){\n    response = response.replace(/(@{ProcessName=| AppTitle=)/g,'').slice(0,-1).split(';',2);\n    window.app = response[0];\n    window.title = response[1];\n  }else if(process.platform == 'darwin'){\n    response = response.split(\",\");\n    window.app = response[0];\n    window.title = response[1].replace(/\\n$/, \"\").replace(/^\\s/, \"\");\n  }\n  return window;\n}\n\n/**\n* Get script config accordingly the operating system\n* @function getConfig\n*/\nfunction getConfig(){\n  //Retrieve configs\n  var configs = JSON.parse(fs.readFileSync(__dirname+'/configs.json', 'utf8'));\n  var path = require(\"path\");\n\n  switch(process.platform){\n      case 'linux':\n      case 'linux2':\n          config = configs.linux\n          break;\n      case 'win32':\n          config = configs.win32\n          break;\n      case 'darwin':\n          config = configs.mac;\n          break;\n      default:\n          throw \"Operating System not supported yet. \"+process.platform;\n  }\n  //Append directory to script url\n  script_url = path.join(__dirname,config.script_url);\n  config.parameters.push(script_url);\n\n  //Append directory to subscript url on OSX\n  if(process.platform==\"darwin\"){\n    config.parameters.push(path.join(__dirname,config.subscript_url));\n  }\n\n  return config;\n}\n"
  },
  {
    "path": "package.json",
    "content": "{\n  \"name\": \"active-window\",\n  \"version\": \"0.1.0\",\n  \"description\": \"Get active window title.\",\n  \"main\": \"index.js\",\n  \"contributors\":[{ \"name\" : \"Jason Stallings \"\n                  , \"email\" : \"jason@stallin.gs\"\n                  , \"url\" : \"\"},\n                  { \"name\" : \"Robson Vieira\"\n                  , \"email\" : \"robsonvnasc@gmail.com\"\n                  , \"url\" : \"\"}\n                ],\n  \"license\": \"MIT\",\n  \"dependencies\": {}\n}\n"
  },
  {
    "path": "scripts/linux.sh",
    "content": "#!/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_NAME\n   sleep $2\n   if [ \"$n\" -gt \"0\" ]\n   then\n   \t\tn=`expr $n - 1`\n   fi\ndone\n"
  },
  {
    "path": "scripts/mac.scpt",
    "content": "#!/usr/bin/env osascript\nglobal frontApp, frontAppName, windowTitle\nset windowTitle to \"\"\ntell application \"System Events\"\n\tset frontApp to first application process whose frontmost is true\n\tset frontAppName to name of frontApp\n\ttell process frontAppName\n\t\ttell (1st window whose value of attribute \"AXMain\" is true)\n\t\t\tset windowTitle to value of attribute \"AXTitle\"\n\t\tend tell\n\tend tell\nend tell\nreturn {frontAppName,windowTitle}\n"
  },
  {
    "path": "scripts/mac.sh",
    "content": "#!/bin/bash\n#Using bash script to run osascript because applescript do not print to stdout\nx=$2\nwhile(($x != \"0\" ))\ndo\n  osascript $1\n  if [ \"$x\" -gt \"0\" ]\n  then\n    ((x-=1))\n  fi\n  sleep $3\ndone\n"
  },
  {
    "path": "scripts/windows.ps1",
    "content": "[CmdletBinding()]\nParam(\n[string]$n,[string]$interval\n)\nAdd-Type @\"\n  using System;\n  using System.Runtime.InteropServices;\n  public class UserWindows {\n    [DllImport(\"user32.dll\")]\n    public static extern IntPtr GetForegroundWindow();\n}\n\"@\ntry {\n\twhile($n -ne 0){\n\t\t$ActiveHandle = [UserWindows]::GetForegroundWindow()\n\t\t$Process = Get-Process | ? {$_.MainWindowHandle -eq $activeHandle}\n\t\t$string =  $Process | Select ProcessName, @{Name=\"AppTitle\";Expression= {($_.MainWindowTitle)}}\n\t\tWrite-Host -NoNewline $string\n\t\tStart-Sleep -m $interval\n\t\tIf ($n -gt 0) {$n-=1}\n\t}\n} catch {\n Write-Error \"Failed to get active Window details. More Info: $_\"\n}\n"
  }
]