Here is a simple AutoHotKey script that “flashes” the newly compiled PDF document when
[Changes detected; refreshing] is removed from the Sumatra window’s title text. It “flashes” the Sumatra window with a newly compiled PDF file as long as such Sumatra window exists. (If such a window is not at the forefront, the window will be activated to show on top of other windows.)
What follows is a standalone AHK script. It is meant to be first saved as a text file called
something.ahk and executed through Autoohtkey. To install Autohotkey, please use this installer (link pointing to the official website).
; What this script does?
; When compiling tex files locally with latexmk as an "auto-compiler", flash
; the output PDF when compilation completes.
; Warning: this script is a quick draft. It may not cover all edge cases.
; The main loop, in AHK, things before the first "return" is called: auto-execution section
WinGetTitle, TitleLast, ahk_exe SumatraPDF.exe
SetTimer, CheckChanged, 250 ;Check every quarter-second
WinGetTitle, Title, ahk_exe SumatraPDF.exe
If (Title <> TitleLast) and InStr(TitleLast, "[Changes detected; refreshing]")
; Here, only consider flashing things when "[Changes detected; refreshing]" is found in title
; Only to flash when a window no longer has "[Changes detected; refreshing]"
If not WinExist("[Changes detected; refreshing]")
; Note, WinExist helps to look for all existing Sumatra windows.
; Log for the current window ID, as ActiveSumatraID
WinGet, ActiveSumatraID, ID, ahk_exe SumatraPDF.exe
; Check that ActiveSumatraID should be unique.
; msgbox, Stored the active window ID it is: %ActiveSumatraID%
; Lastly, this is the "flashing" piece: hide the window for a few
; milliseconds and activate it again.
WinMinimize, ahk_id %ActiveSumatraID%
sleep 20 ; Tune this number (in milliseconds) for a "slower flash" upon completion of the compiler.
WinActivate, ahk_id %ActiveSumatraID%
; Caveat here: this will interrupt an "active typing session", buy shifting the "focus" to the Sumatra window. See the "ugly gray box" workaround below for a less distracting implementation.
TitleLast := Title
; Credits: the monitor-title-change structure - https://autohotkey.com/board/topic/57043-command-to-watch-for-window-title-change/
Note, the “flash” action is implemented in a makeshift manner: when a Sumatra window changes its title text by removing the
"[Changes detected; refreshing]" bit, the window is minimized first, and “activated” 20 milliseconds after.