Last active
August 29, 2015 14:11
-
-
Save irrationalistic/c5ddf7e1c22ce5b72f27 to your computer and use it in GitHub Desktop.
Hacky way to detect terminal focus for given process name (OSX)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /* | |
| 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; | |
| } | |
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This has been turned into an NPM package: https://www.npmjs.com/package/detect-active-title