All the context for this issue can be found here: https://stackoverflow.com/q/68522402/61624
I will inline that information:
I want a cygwin shell script to create a Windows notification (one of those events you see in the lower right-hand corner). When you click on the notification, I want it to bring another application into focus.
Here's how I'll use this: from the command line, I'll run tests. When they finish, I want to be notified by that with the notification. If I click on that notification, I want cygwin to go back into focus so I can decide what to do next.
Okay this was a huge pain to figure out, so it's worth writing down somewhere to help others. It's not hard to create a notification. It's not hard to focus an application. But it is hard to create a notification that focus is a window when clicked.
First I will start with the script:
set -ex
notification_exit_code=0 # default value, exited successfully
# create a notification that says "Build Done". When notifu64 exits, it's exit code tells why it exited (see below)
notifu64.exe /i 'C:\\cygwin64\\Cygwin-Terminal.ico' /t info /p "Build Done" /m "Build Done" || notification_exit_code=$?
# these are some of the ways the notification could have exited
user_clicked_on_notification=3
notification_timeout=4
user_clicked_on_notification_icon=7
if [[ "$notification_exit_code" -eq "$user_clicked_on_notification" ]] || [[ "$notification_exit_code" -eq "$user_clicked_on_notification_icon" ]]
then
# The order of the steps is very important or else the window will remain in the background. I figured out these steps from here: https://stackoverflow.com/a/59819671/61624
nircmd win activate process mintty.exe
nircmd win settopmost process mintty.exe 1
nircmd win settopmost process mintty.exe 0
nircmd win focus process mintty.exe
# I'm not sure why, but "activate" resizes the window. This puts it back the way it always is on my computer: maximized
nircmd win max process mintty.exe
fi
if [[ "$notification_exit_code" -eq "$notification_timeout" ]]
then
# this makes the taskbar icon highlighted to remind me that the build is done even if I don't want to look at it immediately
nircmd win flash process mintty.exe
fi
notifu64.exe is a commandline tool that can create windows notifications.
nircmd is a commandline tool that can control various aspects of the Windows operating system. In this script, I use it to control application windows.
This is 99% of the way there. The problem is the window still doesn't have focus, even though it brings it to the front. You actually have to click on it. It's the ShellExperienceHost.exe that maintains focus, and I can't figure out how to stop that from happening.