Created
March 10, 2019 22:32
-
-
Save jpluimers/77b24e5bce6970d4924e293d2e414612 to your computer and use it in GitHub Desktop.
Automatically close finished ABBYY FineReader 5.0 and Creative Cloud windows.
This file contains 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
program AbbyyFineReader_CreativeCloud_AutoCloseConsoleProject; | |
{$APPTYPE CONSOLE} | |
{$R *.res} | |
uses | |
Winapi.Messages, | |
Winapi.Windows, | |
System.SysUtils; | |
const | |
CreativeCloud = 'Creative Cloud'; | |
AbbyyFineReaderForScanSnap50 = 'ABBYY FineReader for ScanSnap 5.0'; | |
type | |
TWindowDump = record | |
public | |
HWnd: HWnd; | |
IsAppWindow: Boolean; | |
IsOwned: Boolean; | |
IsVisible: Boolean; | |
WindowText: string; | |
WindowTextLength: Integer; | |
ParentHWnd: lParam; | |
Prefix: string; | |
constructor Create(const APrefix: string; const AHWnd: HWnd; const AParentHWnd: lParam); | |
function ToString: string; | |
end; | |
constructor TWindowDump.Create(const APrefix: string; const AHWnd: HWnd; const AParentHWnd: lParam); | |
begin | |
Prefix := APrefix; | |
HWnd := AHWnd; | |
ParentHWnd := AParentHWnd; | |
IsAppWindow := GetWindowLongPtr(HWnd, GWL_STYLE) and WS_EX_APPWINDOW <> 0; | |
IsOwned := GetWindow(HWnd, GW_OWNER) <> 0; | |
IsVisible := IsWindowVisible(HWnd); | |
WindowTextLength := GetWindowTextLength(HWnd); | |
if WindowTextLength <> 0 then | |
begin // without Length check you can get an access violation | |
SetLength(WindowText, WindowTextLength); | |
GetWindowText(HWnd, PChar(WindowText), Length(WindowText) + 1); | |
end; | |
end; | |
function TWindowDump.ToString: string; | |
begin | |
Result := Format('%sParentHWnd=$%8.8x;HWnd=$%8.8x;IsVisible=%s;IsOwned=%s;IsAppWindow=%s;WindowTextLength=%d;WindowText="%s"', [ // | |
Prefix, ParentHWnd, HWnd, BoolToStr(IsVisible), BoolToStr(IsOwned), BoolToStr(IsAppWindow), WindowTextLength, WindowText]); | |
end; | |
function DumpWindow(const Prefix: string; const HWnd: HWnd; const ParentHWnd: lParam): TWindowDump; | |
begin | |
Result := TWindowDump.Create(Prefix, HWnd, ParentHWnd); | |
Writeln(Result.ToString()); | |
end; | |
procedure DumpWindowStack(const Prefix: string; const HWnd: HWnd; const ParentHWnd: lParam); | |
var | |
OwnerHWnd: lParam; | |
begin | |
if ParentHWnd <> 0 then | |
begin | |
OwnerHWnd := GetWindow(ParentHWnd, GW_OWNER); | |
DumpWindow(Prefix, ParentHWnd, OwnerHWnd); | |
DumpWindowStack(' ' + Prefix, ParentHWnd, OwnerHWnd); | |
end; | |
end; | |
function EnumChildWindowsProc(HWnd: HWnd; ParentHWnd: lParam): Bool; stdcall; | |
var | |
IsVisible: Boolean; | |
ParentWindowText: string; | |
ParentWindowTextLength: Integer; | |
WindowDump: TWindowDump; | |
begin | |
Result := True; // carry on enumerating | |
IsVisible := IsWindowVisible(HWnd); | |
if IsVisible then | |
begin | |
WindowDump := DumpWindow(' ', HWnd, ParentHWnd); | |
ParentWindowTextLength := GetWindowTextLength(ParentHWnd); | |
if ParentWindowTextLength <> 0 then | |
begin | |
SetLength(ParentWindowText, ParentWindowTextLength); | |
GetWindowText(ParentHWnd, PChar(ParentWindowText), Length(ParentWindowText) + 1); | |
end; | |
if ParentWindowText = AbbyyFineReaderForScanSnap50 then | |
begin | |
if WindowDump.WindowText = '&Close' then | |
begin | |
Writeln(' > Child is Close button: clicking.'); | |
DumpWindowStack(' < ', HWnd, ParentHWnd); | |
SendMessage(HWnd, BM_CLICK, 0, 0); | |
end; | |
end; | |
if ParentWindowText = CreativeCloud then | |
begin | |
if WindowDump.WindowText = 'Sign in - Adobe ID' then | |
begin | |
Writeln(' > Child is Signin button: closing parent.'); | |
DumpWindowStack(' < ', HWnd, ParentHWnd); | |
SendMessage(ParentHWnd, WM_CLOSE, 0, 0); | |
end; | |
end; | |
end; | |
end; | |
function EnumWindowsProc(HWnd: HWnd; lParam: lParam): Bool; stdcall; | |
var | |
WindowDump: TWindowDump; | |
begin | |
Result := True; // carry on enumerating | |
WindowDump := TWindowDump.Create('', HWnd, lParam); | |
if WindowDump.IsVisible then | |
begin | |
Writeln(WindowDump.ToString()); | |
(* | |
ParentHWnd=$00000000;HWnd=$00030602;IsVisible=-1;IsOwned=0;IsAppWindow=-1;WindowTextLength=33;WindowText="ABBYY FineReader for ScanSnap 5.0" | |
> Recursive child windows for ABBYY | |
ParentHWnd=$00030602;HWnd=$000205E2;IsVisible=-1;IsOwned=0;IsAppWindow=0;WindowTextLength=0;WindowText="" | |
ParentHWnd=$00030602;HWnd=$000205E0;IsVisible=-1;IsOwned=0;IsAppWindow=0;WindowTextLength=0;WindowText="" | |
ParentHWnd=$00030602;HWnd=$000205EC;IsVisible=-1;IsOwned=0;IsAppWindow=0;WindowTextLength=0;WindowText="" | |
ParentHWnd=$00030602;HWnd=$000205EA;IsVisible=-1;IsOwned=0;IsAppWindow=0;WindowTextLength=74;WindowText="Register your copy of ABBYY FineReader and receive the following benefits:" | |
ParentHWnd=$00030602;HWnd=$000205E8;IsVisible=-1;IsOwned=0;IsAppWindow=0;WindowTextLength=25;WindowText="- Free technical support;" | |
ParentHWnd=$00030602;HWnd=$000205E6;IsVisible=-1;IsOwned=0;IsAppWindow=0;WindowTextLength=51;WindowText="- Information about new versions of ABBYY products." | |
ParentHWnd=$00030602;HWnd=$000205E4;IsVisible=-1;IsOwned=0;IsAppWindow=0;WindowTextLength=12;WindowText="Registration" | |
ParentHWnd=$00030602;HWnd=$000205FC;IsVisible=-1;IsOwned=0;IsAppWindow=0;WindowTextLength=0;WindowText="" | |
ParentHWnd=$00030602;HWnd=$000205FA;IsVisible=-1;IsOwned=0;IsAppWindow=0;WindowTextLength=6;WindowText="&Close" | |
> Child is Close button: clicking. | |
< ParentHWnd=$00000000;HWnd=$00030602;IsVisible=-1;IsOwned=0;IsAppWindow=-1;WindowTextLength=33;WindowText="ABBYY FineReader for ScanSnap 5.0" | |
ParentHWnd=$00030602;HWnd=$000205F6;IsVisible=-1;IsOwned=0;IsAppWindow=0;WindowTextLength=34;WindowText="Processing finished (warnings: 1)." | |
ParentHWnd=$00030602;HWnd=$000205F4;IsVisible=-1;IsOwned=0;IsAppWindow=0;WindowTextLength=31;WindowText="Converting to searchable PDF..." | |
ParentHWnd=$00030602;HWnd=$000205F0;IsVisible=-1;IsOwned=0;IsAppWindow=0;WindowTextLength=0;WindowText="" | |
ParentHWnd=$00030602;HWnd=$000205EE;IsVisible=-1;IsOwned=0;IsAppWindow=0;WindowTextLength=0;WindowText="" | |
ParentHWnd=$00030602;HWnd=$000205D2;IsVisible=-1;IsOwned=0;IsAppWindow=0;WindowTextLength=63;WindowText="Page 1. Make sure the correct recognition language is selected." | |
ParentHWnd=$00000000;HWnd=$00010248;IsVisible=-1;IsOwned=-1;IsAppWindow=0;WindowTextLength=14;WindowText="Creative Cloud" | |
> Recursive child windows for Creative Cloud | |
ParentHWnd=$00010248;HWnd=$0001024A;IsVisible=-1;IsOwned=0;IsAppWindow=0;WindowTextLength=28;WindowText="Main Container Client Dialog" | |
ParentHWnd=$00010248;HWnd=$0002034A;IsVisible=-1;IsOwned=0;IsAppWindow=0;WindowTextLength=3;WindowText="IMS" | |
ParentHWnd=$00010248;HWnd=$0001035A;IsVisible=-1;IsOwned=0;IsAppWindow=0;WindowTextLength=0;WindowText="" | |
ParentHWnd=$00010248;HWnd=$00020350;IsVisible=-1;IsOwned=0;IsAppWindow=0;WindowTextLength=18;WindowText="Sign in - Adobe ID" | |
> Child is Signin button: closing parent. | |
< ParentHWnd=$0003011A;HWnd=$00010248;IsVisible=-1;IsOwned=-1;IsAppWindow=0;WindowTextLength=14;WindowText="Creative Cloud" | |
< ParentHWnd=$00000000;HWnd=$0003011A;IsVisible=0;IsOwned=0;IsAppWindow=0;WindowTextLength=4;WindowText="Core" | |
*) | |
if WindowDump.IsVisible and WindowDump.IsAppWindow then | |
begin | |
if WindowDump.WindowText = AbbyyFineReaderForScanSnap50 then | |
begin | |
Writeln('> Recursive child windows for ABBYY'); | |
EnumChildWindows(HWnd, @EnumChildWindowsProc, HWnd); | |
end; | |
end; | |
if WindowDump.IsVisible and WindowDump.IsOwned then | |
if WindowDump.WindowText = CreativeCloud then | |
begin | |
Writeln('> Recursive child windows for Creative Cloud'); | |
EnumChildWindows(HWnd, @EnumChildWindowsProc, HWnd); | |
end; | |
end; | |
end; | |
begin | |
try | |
repeat | |
EnumWindows(@EnumWindowsProc, 0); | |
Writeln('.'); | |
Sleep(10 * 1000); | |
until False; | |
except | |
on E: Exception do | |
Writeln(E.ClassName, ': ', E.Message); | |
end; | |
end. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment