How do I detect whether my terminal has focus in the GUI from a shell script?
Clash Royale CLAN TAG#URR8PPP
up vote
0
down vote
favorite
Problem/Goal description
Ideally, I would like a good way of detecting from a shell script whether or not the window has focus. By a "good" way, I mean some way which requires minimal steps and preferably does not require sifting through each open window blindly to find mine based on title.
The purpose is for controlling notifications in many different scripts -- so I'm just looking for a general solution that can apply to any and all of them.
What I've come up with so far is roundabout and hacky -- it is as follows:
Set my title to something unique or mechanically relevant (in my model, it is my PTS path or, more robustly, a UUID). Hope desperately that this title is not overridden by something.
Get a list of all open windows, by title.
Iterate through list to identify my window by matching it to the title element. (Note the possibility of errors here if another window happens to have that same title element.)
Detect whether said window has focus or not.
Compromises
This solution is obviously terrible, so I'd like to know if there is anything remotely better, in any way. I would prefer something portable, elegant, and perfect, but I recognize the potential need to compromise. By better I mean any of the following:
A solution that only works with a specific terminal emulator, e.g. by having the terminal emulator itself set an environment variable allowing the script to detect which window it is in.
A solution that does not require setting the title, and instead uses some other invisible marker in window state that is accessible and detectable from a shell script attached to said window.
Recusing up the parent process ladder to find the parent terminal emulator PID, and working from there (Note that a solution that works by recusing up the the process tree to detect the parent process that started the script will only work if the script is running locally, so this solution is incomplete but still good!)
shell-script shell terminal-emulator focus
add a comment |Â
up vote
0
down vote
favorite
Problem/Goal description
Ideally, I would like a good way of detecting from a shell script whether or not the window has focus. By a "good" way, I mean some way which requires minimal steps and preferably does not require sifting through each open window blindly to find mine based on title.
The purpose is for controlling notifications in many different scripts -- so I'm just looking for a general solution that can apply to any and all of them.
What I've come up with so far is roundabout and hacky -- it is as follows:
Set my title to something unique or mechanically relevant (in my model, it is my PTS path or, more robustly, a UUID). Hope desperately that this title is not overridden by something.
Get a list of all open windows, by title.
Iterate through list to identify my window by matching it to the title element. (Note the possibility of errors here if another window happens to have that same title element.)
Detect whether said window has focus or not.
Compromises
This solution is obviously terrible, so I'd like to know if there is anything remotely better, in any way. I would prefer something portable, elegant, and perfect, but I recognize the potential need to compromise. By better I mean any of the following:
A solution that only works with a specific terminal emulator, e.g. by having the terminal emulator itself set an environment variable allowing the script to detect which window it is in.
A solution that does not require setting the title, and instead uses some other invisible marker in window state that is accessible and detectable from a shell script attached to said window.
Recusing up the parent process ladder to find the parent terminal emulator PID, and working from there (Note that a solution that works by recusing up the the process tree to detect the parent process that started the script will only work if the script is running locally, so this solution is incomplete but still good!)
shell-script shell terminal-emulator focus
add a comment |Â
up vote
0
down vote
favorite
up vote
0
down vote
favorite
Problem/Goal description
Ideally, I would like a good way of detecting from a shell script whether or not the window has focus. By a "good" way, I mean some way which requires minimal steps and preferably does not require sifting through each open window blindly to find mine based on title.
The purpose is for controlling notifications in many different scripts -- so I'm just looking for a general solution that can apply to any and all of them.
What I've come up with so far is roundabout and hacky -- it is as follows:
Set my title to something unique or mechanically relevant (in my model, it is my PTS path or, more robustly, a UUID). Hope desperately that this title is not overridden by something.
Get a list of all open windows, by title.
Iterate through list to identify my window by matching it to the title element. (Note the possibility of errors here if another window happens to have that same title element.)
Detect whether said window has focus or not.
Compromises
This solution is obviously terrible, so I'd like to know if there is anything remotely better, in any way. I would prefer something portable, elegant, and perfect, but I recognize the potential need to compromise. By better I mean any of the following:
A solution that only works with a specific terminal emulator, e.g. by having the terminal emulator itself set an environment variable allowing the script to detect which window it is in.
A solution that does not require setting the title, and instead uses some other invisible marker in window state that is accessible and detectable from a shell script attached to said window.
Recusing up the parent process ladder to find the parent terminal emulator PID, and working from there (Note that a solution that works by recusing up the the process tree to detect the parent process that started the script will only work if the script is running locally, so this solution is incomplete but still good!)
shell-script shell terminal-emulator focus
Problem/Goal description
Ideally, I would like a good way of detecting from a shell script whether or not the window has focus. By a "good" way, I mean some way which requires minimal steps and preferably does not require sifting through each open window blindly to find mine based on title.
The purpose is for controlling notifications in many different scripts -- so I'm just looking for a general solution that can apply to any and all of them.
What I've come up with so far is roundabout and hacky -- it is as follows:
Set my title to something unique or mechanically relevant (in my model, it is my PTS path or, more robustly, a UUID). Hope desperately that this title is not overridden by something.
Get a list of all open windows, by title.
Iterate through list to identify my window by matching it to the title element. (Note the possibility of errors here if another window happens to have that same title element.)
Detect whether said window has focus or not.
Compromises
This solution is obviously terrible, so I'd like to know if there is anything remotely better, in any way. I would prefer something portable, elegant, and perfect, but I recognize the potential need to compromise. By better I mean any of the following:
A solution that only works with a specific terminal emulator, e.g. by having the terminal emulator itself set an environment variable allowing the script to detect which window it is in.
A solution that does not require setting the title, and instead uses some other invisible marker in window state that is accessible and detectable from a shell script attached to said window.
Recusing up the parent process ladder to find the parent terminal emulator PID, and working from there (Note that a solution that works by recusing up the the process tree to detect the parent process that started the script will only work if the script is running locally, so this solution is incomplete but still good!)
shell-script shell terminal-emulator focus
shell-script shell terminal-emulator focus
asked 4 mins ago
Alexandria P.
93
93
add a comment |Â
add a comment |Â
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f480052%2fhow-do-i-detect-whether-my-terminal-has-focus-in-the-gui-from-a-shell-script%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password