views:

31

answers:

1

Is it possible to trigger Windows' "flash the task bar button X times or until the window comes to the foreground" behavior from a batch file? I'm trying to call the user's attention to a long-running script upon completion.

Using an external program to trigger the flashing is fine, as long as it doesn't require an install (i.e. the executable can be bundled with my scripts).

Update

Here's what I ended up with (a minimalist port of Andreas' Delphi code). I've compiled it under MinGW at it appears to be dependent only on KERNEL32.DLL and USER32.DLL, so should be highly portable.

Flashes three times, then stays highlighted until brought to the foreground.

#define WINVER 0x501
#define _WIN32_WINNT 0x501

#include <windows.h>

void main(int argc, char **argv) {
    FLASHWINFO info = { sizeof(info), GetConsoleWindow(), FLASHW_TIMERNOFG | FLASHW_TRAY, 3, 0 };

    FlashWindowEx(&info);
}
+1  A: 

It is very easy to do using a very simple external *.exe. It simply has to call the FlashWindowEx function of the Windows API.

This is a sample Delphi console application:

program flashwnd;

{$APPTYPE CONSOLE}

uses
  SysUtils, Windows;

var
  OldTitle, UniqueTitle: string;
  h: HWND;
  c: cardinal;
  fwi: TFlashWInfo;

begin

  try
    // We need a unique title
    SetLength(OldTitle, 1024);
    GetConsoleTitle(PChar(OldTitle), 1024);
    UniqueTitle := 'RAND' + IntToStr(GetTickCount);
    SetConsoleTitle(PChar(UniqueTitle));
    sleep(50);
    h := FindWindow(nil, PChar(UniqueTitle));
    SetConsoleTitle(PChar(OldTitle));

    c := 10;
    if ParamCount = 1 then
      c := StrToInt(ParamStr(1));

    FillChar(fwi, sizeof(fwi), 0);
    fwi.cbSize := sizeof(fwi);
    fwi.hwnd := h;
    fwi.dwFlags := FLASHW_ALL;
    fwi.uCount := c;
    fwi.dwTimeout := 0;
    FlashWindowEx(fwi);
  except
    on E: Exception do
      Writeln(E.ClassName + ': ' + E.Message);
  end;
end.

Simply call it like

flashwnd

to flash the current console window ten times. Call

flashwnd 27

to flash the window 27 times.

Andreas Rejbrand
I suspected something like this would be the case, but am hopeless at Windows APIs! I'll see if I can get this working.
Ben Blank
Finally got it working in MinGW (as I have no Delphi compiler handy). Thanks!
Ben Blank