CMD Get Text Response

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
User avatar
TheDewd
Posts: 1513
Joined: 19 Dec 2013, 11:16
Location: USA

CMD Get Text Response

07 Mar 2024, 11:57

I'm trying to capture the text from command prompt (cmd.exe) after executing commands using pngquant.exe (PNG Image Compression).

I've tested various functions from the forums, but I can't seem to figure out something...

I can easily get the response from the help command pngquant.exe -h

However, I can't get the text after compressing an image: pngquant.exe --force --verbose --quality=45-85 image.png

The returned value is BLANK.

Windows Binary: https://pngquant.org/pngquant-windows.zip

Any advice? Why can I capture the help text, but not the compression status text?

If it's helpful, I think the text I'm trying to capture is not outputted all at once, but instead line by line over a period of time.

Code: Select all

C:\Users\Dewd\Downloads\Project\pngquant>pngquant --force --verbose --quality=45-85 image.png
image.png:
  conserving memory
  read 16040KB file
  used embedded ICC profile to transform image to sRGB colorspace
  made histogram...24833 colors found
  selecting colors...6%
  selecting colors...46%
  selecting colors...93%
  selecting colors...100%
  moving colormap towards local minimum
  eliminated opaque tRNS-chunk entries...0 entries transparent
  mapped image to new colors...MSE=3.003 (Q=89)
  writing 52-color image as image-fs8.png
  copied 1KB of additional PNG metadata
Quantized 1 image.

C:\Users\Dewd\Downloads\Project\pngquant>



Thanks!
RussF
Posts: 1290
Joined: 05 Aug 2021, 06:36

Re: CMD Get Text Response

07 Mar 2024, 12:39

I think the first thing I would try is to redirect output from the utility to a text file by adding >> path\filename.txt to the end of the command line and then you can read the entire text file in afterwards. The >> redirection will add to a file if it already exists. so if you want only the text from that specific operation to be in the file, you could use FileExist() and FileDelete prior to calling the utility to delete any existing file. If you want to keep all the log files, you could dynamically append a time string to each file name.

Russ
User avatar
flyingDman
Posts: 2832
Joined: 29 Sep 2013, 19:01

Re: CMD Get Text Response

07 Mar 2024, 18:23

>> and | clip did not work for me. (BTW thanks for the links to pngquant; works great and reduces the size of .png files by ~80%)
14.3 & 1.3.7
User avatar
TheDewd
Posts: 1513
Joined: 19 Dec 2013, 11:16
Location: USA

Re: CMD Get Text Response

07 Mar 2024, 18:42

(BTW thanks for the links to pngquant; works great and reduces the size of .png files by ~80%)
No problem! Been using it to reduce image sizes for my EmulationStation scraped images for my game collection.

There's a few GUI for it. I was going to try making my own GUI though.
garry
Posts: 3783
Joined: 22 Dec 2013, 12:50

Re: CMD Get Text Response

08 Mar 2024, 05:12

from user @teadrinker
you can write commands below ( example is in the script , variable 'cmdx' ) , get output in a textfile > f1=%a_desktop%\%a_now%_DOS_SCREEN_OUTPUT.txt
--
variable 'cmdx' :

Code: Select all

PR:=a_scriptdir . "\pngquant\pngquant.exe"
;cmdx:=pr . " -h"
pic:=a_scriptdir . "\test.png" 
cmdx:=pr . " --force --verbose --quality=45-85 " . pic
-
CMDRET script :

Code: Select all

;- CMD Get Text Response 
;- https://www.autohotkey.com/boards/viewtopic.php?f=76&t=127027

;- MODIFIED  = 20210201
;- CREATED   = 20201211  from user 'teadrinker'
;----------------------------------------------
;- cmdretxxx teadrinkerxxx codepagexxx
;- Hide command console when using RunWaitOne() example / from user teadrinker
;- https://www.autohotkey.com/boards/viewtopic.php?f=76&t=84266&sid=eff012cadb7a851b2c18f5f03b68408f
;- https://de.wikipedia.org/wiki/Zeichensatztabelle
#warn
#noenv
setworkingdir,%a_scriptdir%
Fileencoding, CP1200
name1:="DOS_TEST"
e:=""
;-
;cmdx:="dir"                            ;- first command for test
;cmdx:="ping autohotkey.com -n 4"
;url:="https://www.youtube.com/watch?v=iR0w0mWB3uA"
;cmdx=youtube-dl %url% -g -e

PR:=a_scriptdir . "\pngquant\pngquant.exe"
ifexist,%pr%
{
pic:=a_scriptdir . "\test.png" 
cmdx:=pr . " --force --verbose --quality=45-85 " . pic
}
else
cmdx:="ping autohotkey.com -n 4"


;----------------------------------------------
CPX:="cp" . DllCall("GetOEMCP", "UInt")        ;- get CODEPAGE e.g. cp850 here OK
global name1,cpx
;--------------------------
CONT:
Gui,1:default
Gui,1: -DPIScale 
Gui,1:Color,Black,Black                        ; GUI black   / EDIT is BLACK
wa:=A_screenwidth,ha:=A_screenHeight,xx:=100
;- maybe change fontsize
;============ GUISIZEx DPIx 4Kx 3840*2160 is 100% ============
if (wa=3840)
 Gui,1:Font,s12 cYellow,Lucida Console
;============ GUISIZEx DPIx FHD 1920*1080 is 100% ============
else if (wa=1920)
 Gui,1:Font,s10 cYellow,Lucida Console
else
 Gui,1:Font,s8 cYellow,Lucida Console
;=============================================================
x:=(wa*1)/xx,y:=(ha*1)/xx,w:=(wa*70)/xx,h:=(ha*60)/xx
Gui, Add, Edit, x%x%   y%y%  w%w% h%h% vED1                  ;- here the output
x:=(wa*1)/xx,y:=(ha*63)/xx,w:=(wa*65)/xx,h:=(ha*6)/xx
Gui, Add, Edit, x%x%   y%y% w%w%  h%h% vED2,%cmdx%           ;- add CMDx command above
x:=(wa*67)/xx,y:=(ha*63)/xx,w:=(wa*5)/xx,h:=(ha*2.2)/xx
Gui, add, Edit, x%x%  y%y% w%w%  h%h% vED3,
x:=(wa*67)/xx,y:=(ha*66)/xx,w:=(wa*5)/xx,h:=(ha*2.2)/xx
Gui,add,button, x%x%  y%y% w%w%  h%h% gPrintx, PRINT
x:=(wa*2)/xx,y:=(ha*2)/xx,w:=(wa*75)/xx,h:=(ha*75)/xx
Gui, Show,x%x% y%y% w%w% h%h%,%name1%
gosub,a0
GuiControl,1: Focus,ED2
return
;-------------------------
Guiclose:
Exitapp
esc::exitapp
;-------------------------
~$enter::
gosub,a0
return
;=========================================================
AddOutputInGui(output) {
   Control, EditPaste, %output%`r`n, Edit1,%name1%    ;- display output in 1st EDIT
}
;=========================================================
A0:
Gui,1:submit,nohide
ed2a:="cmd /c " . ed2
;CmdRet(ED2a, Func("AddOutputInGui"),CPX)
xxc:=cmdret(ED2a, Func("AddOutputInGui"),CPX)
e .="`r`n===============================`r`n" . ed2 . " =`r`n-------------------------------`r`n" . xxc . "`r`n===============================`r`n"
e := RegExReplace(e, "((^)|\R)\h*(?=\R|$)(?(2)\R)")              ;- remove empty lines
gosub,clear
gosub,printx
return
;-------------------------
CLEAR:
Gui,1:submit,nohide
Guicontrol,1:,ED2,
Guicontrol,1:,ED3,ENDED
GuiControl,1: Focus,ED2
return
;-------------------------
printx:
f1=%a_desktop%\%a_now%_DOS_SCREEN_OUTPUT.txt
Gui,1:submit,nohide
if e<>
 {
 fileappend,%e%,%f1%,utf-8
 run,%f1%
 e:=""
 }
return
;=========================================================
CmdRet(sCmd, callBackFuncObj := "", encoding := "CP0")
{
   static HANDLE_FLAG_INHERIT := 0x00000001, flags := HANDLE_FLAG_INHERIT
        , STARTF_USESTDHANDLES := 0x100, CREATE_NO_WINDOW := 0x08000000
   hPipeRead:=""
   hPipeWrite:=""
   sOutput:=""
   DllCall("CreatePipe", "PtrP", hPipeRead, "PtrP", hPipeWrite, "Ptr", 0, "UInt", 0)
   DllCall("SetHandleInformation", "Ptr", hPipeWrite, "UInt", flags, "UInt", HANDLE_FLAG_INHERIT)
   
   VarSetCapacity(STARTUPINFO , siSize :=    A_PtrSize*4 + 4*8 + A_PtrSize*5, 0)
   NumPut(siSize              , STARTUPINFO)
   NumPut(STARTF_USESTDHANDLES, STARTUPINFO, A_PtrSize*4 + 4*7)
   NumPut(hPipeWrite          , STARTUPINFO, A_PtrSize*4 + 4*8 + A_PtrSize*3)
   NumPut(hPipeWrite          , STARTUPINFO, A_PtrSize*4 + 4*8 + A_PtrSize*4)
   
   VarSetCapacity(PROCESS_INFORMATION, A_PtrSize*2 + 4*2, 0)
   if !DllCall("CreateProcess", "Ptr", 0, "Str", sCmd, "Ptr", 0, "Ptr", 0, "UInt", true, "UInt", CREATE_NO_WINDOW
                              , "Ptr", 0, "Ptr", 0, "Ptr", &STARTUPINFO, "Ptr", &PROCESS_INFORMATION)
   {
      DllCall("CloseHandle", "Ptr", hPipeRead)
      DllCall("CloseHandle", "Ptr", hPipeWrite)
      throw Exception("CreateProcess is failed")
   }
   DllCall("CloseHandle", "Ptr", hPipeWrite)
   VarSetCapacity(sTemp, 4096), nSize := 0
   while DllCall("ReadFile", "Ptr", hPipeRead, "Ptr", &sTemp, "UInt", 4096, "UIntP", nSize, "UInt", 0) {
      sOutput .= stdOut := StrGet(&sTemp, nSize, encoding)
      ;sOutput .= stdOut := StrGet(&sTemp, nSize)
      ;sOutput .= stdOut := StrGet(&sTemp, nSize, CPX)
      ( callBackFuncObj && callBackFuncObj.Call(stdOut) )
   }
   DllCall("CloseHandle", "Ptr", NumGet(PROCESS_INFORMATION))
   DllCall("CloseHandle", "Ptr", NumGet(PROCESS_INFORMATION, A_PtrSize))
   DllCall("CloseHandle", "Ptr", hPipeRead)
   Return sOutput
}
;================= END SCRIPT ========================================================================
RESULT :

Code: Select all

===============================
C:\Users\garry\Desktop\CMDRET\pngquant\pngquant.exe --force --verbose --quality=45-85 C:\Users\garry\Desktop\CMDRET\test.png =
-------------------------------
C:\Users\garry\Desktop\CMDRET\test.png:
  conserving memory
  read 8204KB file
  made histogram...78392 colors found
  selecting colors...11%
  selecting colors...77%
  selecting colors...100%
  moving colormap towards local minimum
  eliminated opaque tRNS-chunk entries...0 entries transparent
  mapped image to new colors...MSE=3.186 (Q=89)
  writing 69-color image as C:\Users\ursul\Desktop\CMDRET\test-fs8.png
  copied 1KB of additional PNG metadata
Quantized 1 image.
===============================

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: rubeusmalfoy and 82 guests