attach hidden command prompt to script in autoexecute section Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
Gibbons
Posts: 93
Joined: 20 Sep 2016, 20:39

attach hidden command prompt to script in autoexecute section

04 Feb 2020, 12:46

I'm trying to open a command prompt and hide it so when I run DOS commands I don't get a 'blip' of the command prompt every time.

I had the code before, and it was 2 lines long, one line to open the command prompt, the second line hid the command prompt. That hidden command prompt would be used whenever the script needed a command prompt. I think DLL call or COM was involved.

Here is a test script (I know the loop is illogical, but it demonstrates the command window blip I'm trying to eliminate)

Code: Select all

#p::
{
	FileSelectFile, MyFile ; Select a pdf file
	loop, 5
	{
		clipboard :=  PdfToText(MyFile)
		sleep, 2000
	}
	return
}

PdfToText(PdfPath) {
    static XpdfPath := """" A_ScriptDir "\pdftotext.exe"""
    objShell := ComObjCreate("WScript.Shell")
 
    ;--------- Building CmdString (look in the .txt docs incuded with xpdf):
    ; From the xpdf docs in [ScriptDir]\xpdfbin-win-3.04\doc\pdftotext.txt:
    ;   SYNOPSIS
    ;       pdftotext [options] [PDF-file [text-file]]
    ;   ...
    ;       If text-file is '-', the text is sent to stdout.
    ; Options (Example option. Look in the xpdf docs for more):
    ;   -nopgbrk    Don't insert page breaks (form feed characters)  between  pages.
    ;---------
    CmdString := XpdfPath " -nopgbrk """ PdfPath """ -"
    objExec := objShell.Exec(CmdString)
    while, !objExec.StdOut.AtEndOfStream ; Wait for the program to finish
        strStdOut := objExec.StdOut.ReadAll()
    return strStdOut
}
User avatar
Xtra
Posts: 2750
Joined: 02 Oct 2015, 12:15

Re: attach hidden command prompt to script in autoexecute section  Topic is solved

04 Feb 2020, 13:00

Code: Select all

PdfToText(PdfPath) {
    static XpdfPath := """" A_ScriptDir "\pdftotext.exe"""
    dhw := A_DetectHiddenWindows
    DetectHiddenWindows, On
    Run, % ComSpec,, Hide, cPid
    WinWait, % "ahk_pid" . cPid
    DetectHiddenWindows, % dhw
    DllCall("AttachConsole", "uint", cPid)
    objShell := ComObjCreate("WScript.Shell")
    ;--------- Building CmdString (look in the .txt docs incuded with xpdf):
    ; From the xpdf docs in [ScriptDir]\xpdfbin-win-3.04\doc\pdftotext.txt:
    ;   SYNOPSIS
    ;       pdftotext [options] [PDF-file [text-file]]
    ;   ...
    ;       If text-file is '-', the text is sent to stdout.
    ; Options (Example option. Look in the xpdf docs for more):
    ;   -nopgbrk    Don't insert page breaks (form feed characters)  between  pages.
    ;---------
    CmdString := XpdfPath " -nopgbrk """ PdfPath """ -"
    objExec := objShell.Exec(CmdString)
    while, !objExec.StdOut.AtEndOfStream ; Wait for the program to finish
        strStdOut := objExec.StdOut.ReadAll()
    DllCall("FreeConsole")
    Process, Close, % cPid
    return strStdOut
}
HTH
Gibbons
Posts: 93
Joined: 20 Sep 2016, 20:39

Re: attach hidden command prompt to script in autoexecute section

05 Feb 2020, 10:50

Thanks!

I ended up braking up the lines of code you provided and put part into the autoexecute section, and the rest (slightly modified) into the exit section of my script. This made it so the command line only opens and closes one each time the script is run, instead of each time the command line is called. Increased efficiency about 4 fold.

Code: Select all

; Autoexecute Section
StartComSpec()
OnExit("ExitFunc")
return
; End Autoexecute

StartComSpec()
{
    dhw := A_DetectHiddenWindows
    DetectHiddenWindows, On
    Run, % ComSpec,, Hide, cPid
    WinWait, % "ahk_pid" . cPid
    DetectHiddenWindows, % dhw
    DllCall("AttachConsole", "uint", cPid)
    return
}

ExitFunc(ExitReason, ExitCode)
{
    DetectHiddenWindows, On
    DllCall("FreeConsole")
    Process, Close, cmd.exe
    return
}

#p::
{
   FileSelectFile, MyFile ; Select a pdf file
    loop, 250
    {
        clipboard :=  PdfToText(MyFile)
    }
    msgbox, % clipboard
    return
}

ExitFunc(ExitReason, ExitCode)
{
    DetectHiddenWindows, On
    DllCall("FreeConsole")
    Process, Close, cmd.exe
    return
}

PdfToText(PdfPath) {
    static XpdfPath := """" A_ScriptDir "\pdftotext.exe"""
    objShell := ComObjCreate("WScript.Shell")
    ;--------- Building CmdString (look in the .txt docs incuded with xpdf):
    ; From the xpdf docs in [ScriptDir]\xpdfbin-win-3.04\doc\pdftotext.txt:
    ;   SYNOPSIS
    ;       pdftotext [options] [PDF-file [text-file]]
    ;   ...
    ;       If text-file is '-', the text is sent to stdout.
    ; Options (Example option. Look in the xpdf docs for more):
    ;   -nopgbrk    Don't insert page breaks (form feed characters)  between  pages.
    ;---------
    CmdString := XpdfPath " -nopgbrk """ PdfPath """ -"
    objExec := objShell.Exec(CmdString)
    while, !objExec.StdOut.AtEndOfStream ; Wait for the program to finish
        strStdOut := objExec.StdOut.ReadAll()
    return strStdOut
}

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Google [Bot] and 382 guests