Skip to content

Instantly share code, notes, and snippets.

@irrationalistic
Last active August 29, 2015 14:11
Show Gist options
  • Select an option

  • Save irrationalistic/c5ddf7e1c22ce5b72f27 to your computer and use it in GitHub Desktop.

Select an option

Save irrationalistic/c5ddf7e1c22ce5b72f27 to your computer and use it in GitHub Desktop.
Hacky way to detect terminal focus for given process name (OSX)
/*
If terminal doesn't have focus, it won't execute the child process.
If it does have focus, the process will execute and then check
to see if the windows title matches your regular expression.
Only then will timeMet be set and the timeout will run
*/
var timeMet = false;
setTimeout(function(){
if(!timeMet){
// do your thing here
}
}, 500);
var res = require('child_process').exec('osascript osx-active-app.scpt');
res.stdout.on('data', function (d) {
// change this regular expression to match the executable
var rex = new RegExp('myapp -flags');
if(rex.test(d)){
timeMet = true;
}
});
tell application "System Events"
set frontApp to name of first application process whose frontmost is true
end tell
tell application frontApp
if the (count of windows) is not 0 then
set window_name to name of front window
end if
end tell
@irrationalistic
Copy link
Author

This has been turned into an NPM package: https://www.npmjs.com/package/detect-active-title

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment