Skip to content

Instantly share code, notes, and snippets.

@bhavyaw
Created September 12, 2016 09:26
Show Gist options
  • Save bhavyaw/cc327acb3ac8557b2a56c7077461a1ec to your computer and use it in GitHub Desktop.
Save bhavyaw/cc327acb3ac8557b2a56c7077461a1ec to your computer and use it in GitHub Desktop.
var exec = require("child_process").exec;
var multiline = require("multiline");
var Q = require('q');
module.exports = {
start : runScript,
stop : stopScript
}
//Applescript to get active window title.
var getWindowScript = multiline(function(){
/*
global windowTitle,browserTabUrl,appName,appID
set windowTitle to ""
set currentTabTitle to ""
tell application "System Events"
set frontApp to first application process whose frontmost is true
set appName to name of frontApp
set x to first process whose its frontmost is true
set appID to unix id of x
tell process appName
try
tell (1st window whose value of attribute "AXMain" is true)
set windowTitle to value of attribute "AXTitle"
end tell
end try
end tell
end tell
return {windowTitle,appName,appID}
*/
});
function runScript() {
_pollingInterval = setInterval(function() {
getActiveWindowDetails();
}, 500);
}
function stopScript() {
clearInterval(_pollingInterval);
}
function getActiveWindowDetails(callback){
//Run applescript from shell.
var script = "osascript -e '" + getWindowScript + "'";
exec(script, function(error, stdout, stderr){
if(!error){
var activeWindow = {};
console.log("Active Window Details : ",stdout);
}
else{
console.error(error,stdout,stderr);
}
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment