Skip to content

Instantly share code, notes, and snippets.

@jasonsparc
Last active May 28, 2017 18:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jasonsparc/c498a6b4fa0cedca4d801aad58439478 to your computer and use it in GitHub Desktop.
Save jasonsparc/c498a6b4fa0cedca4d801aad58439478 to your computer and use it in GitHub Desktop.
; File Open Utilities
OpenAndGetHwnd(sDocument, sParams:="", sValidHwndPredicate:="IsValidWinTitleText", sTimeout:=10) {
AssociatedExe := sDocument
SplitPath, sDocument, , , OutExt
if (not OutExt = "exe") {
AssociatedExe := FindExecutable(sDocument)
if (not AssociatedExe) {
IfNotExist sDocument
{
MsgBox 0, Error, %sDocument%`n`nThe specified file was not found.
} else {
MsgBox 0, Error, %sDocument%`n`nCould not find the associated executable for the specified document.
}
return 0
} else if (sParams) {
sParams = "%sDocument%" %sParams%
} else {
sParams = "%sDocument%"
}
}
return RunAndGetHwnd(AssociatedExe, sParams, sValidHwndPredicate, sTimeout)
}
RunAndGetHwnd(sExe, sParams:="", sValidHwndPredicate:="IsValidWinTitleText", sTimeout:=10) {
sTargetWin = ahk_exe %sExe%
OldIDs := GetValidWinIDs(sTargetWin, sValidHwndPredicate)
Run "%sExe%" %sParams%
Retries := sTimeout / 0.5
FailInterval := 500
Loop %Retries% {
WinWait %sTargetWin%,, sTimeout
if ErrorLevel ; Check for timeout
break
TestIDs := GetValidWinIDs(sTargetWin, sValidHwndPredicate)
NewIDs := GetNewEntries(OldIDs, TestIDs)
;MsgBox % GetWinTitleText("ahk_id " . FirstKey(NewIDs))
;MsgBox % EntriesToString(NewIDs)
if (NewIDs.Length()) {
return FirstKey(NewIDs)
}
Sleep FailInterval
}
return ""
}
; Additional Utility Functions
GetInternetShortcutUrl(sInternetShortcutFile) {
IniRead OutUrl, %sInternetShortcutFile%, InternetShortcut, Url, %A_Space%
Return OutUrl
}
FindExecutable(sDocument, sMaxPathLen:=260) {
VarSetCapacity(Ret, sMaxPathLen)
RetCode := DllCall("shell32.dll\FindExecutable", "Str", sDocument, "Str", "", "Str", Ret)
if (RetCode > 32) {
Ret = %Ret%
return Ret
}
ErrorLevel := RetCode
return ""
}
GetWinTitleText(sWinTitle) {
WinGetTitle Out, %sWinTitle%
return Out
}
IsValidWinTitleText(sWinTitle) {
WinGetTitle Out, %sWinTitle%
; Checks if invalid window.
; Add more checks below.
if (not Out or Out ~= "Opening - (Excel|Word)") {
return ""
}
return Out
}
GetValidWinIDs(sWinTitle, sPredicate:="IsValidWinTitleText") {
WinGet FoundIDs, List, %sWinTitle%
IDMap := {}
i := 0
Loop %FoundIDs%
{
FoundID := FoundIDs%A_Index%
if (%sPredicate%("ahk_id " . FoundID))
IDMap[FoundID] := ++i
}
return IDMap
}
; Utility Map Functions
GetNewEntries(sTargetMap, sTestMap) {
Ret := {}
i := 0
For k,v in sTestMap
if not sTargetMap[k]
Ret[k] := ++i
return Ret
}
FirstKey(sMap) {
For i,v in sMap {
return i
}
return ""
}
EntriesToString(sMap, sSep:="`n") {
Out := ""
For i,v in sMap
Out .= i . " := " . v . sSep
return Out
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment