Page 4 of 13

Re: RunCMD()

Posted: 21 Jun 2020, 11:25
by guest3456
hi SKAN

why do you avoid the PeekNamedPipe call that many of the newer iterations of StdOutToVar have used?

https://www.autohotkey.com/boards/viewtopic.php?f=6&t=791

Re: RunCMD()

Posted: 21 Jun 2020, 11:36
by burque505
@garry, this works for wget.

Code: Select all

#SingleInstance, Force
#Include RunCmd.ahk
;varout := StdOutStream("cmd.exe /c %comspec% /c C:\msys64\usr\bin\wget.exe https://wordpress.org/latest.zip") ; prior attempt using stdout2var.ahk
varout := RunCmd("C:\msys64\usr\bin\wget.exe https://wordpress.org/latest.zip")
msgbox %varout%
Thanks again, @SKAN!

Re: RunCMD()

Posted: 21 Jun 2020, 12:00
by SKAN
guest3456 wrote:
21 Jun 2020, 11:25
why do you avoid the PeekNamedPipe call that many of the newer iterations of StdOutToVar have used?
No, I'm not avoiding.
1) Sean would've used it had he found it to be better. (He was thorough with API functions)
2) I'm yet to find a CLI utility that hangs.. I need a problem to compare and find if PeekNamedPipe() is better.

:)

Re: RunCMD()

Posted: 21 Jun 2020, 12:18
by guest3456
fair enough :thumbsup:

Re: RunCMD()

Posted: 21 Jun 2020, 12:43
by garry
@burque505
@SKAN
thank you very much , works fine ( example with wget.exe )

Code: Select all

;- https://www.autohotkey.com/boards/viewtopic.php?f=6&t=74647&start=60

;- example downloads a mp3-file to folder C:\users\%a_username%\Downloads ( normal downloadfolder ) 
;---------------------
#SingleInstance, Force
#Include RunCmd.ahk
;---------------------
f1:="http://74.136.132.35/MUSIC/RHAPSODY/J/Johnny%20Horton/American%20Originals/02%20-%20North%20To%20Alaska.mp3"
SplitPath,f1, name, dir, ext, name_no_ext, drive
name:= URLdecode(name)                            ;- remove %20 etc in filename
wget1:="D:\M_GARRY\PROGRAMME_SA\WGET\wget.exe"    ;- <<< here is wget.exe
 loop,%wget1%                                     ;- create shortpath
   SP1:=A_loopFileShortPath
folderx2:="C:\Users\" . a_username . "\Downloads" ;- <<< Download_folder        
ifnotexist,%folderx2%
 filecreatedir,%folderx2%
f2  := folderx2 . "\" . name                      ;- File-fullpath
transform,s,chr,34
;--------------
VAR:=sp1 . " --output-document=" . s . f2 . s . " " . f1  ;- wget downloads file to defined folder 
varout:=Runcmd(var)
msgbox %varout%                                   ;- see variable ( wget-finished )
;run,%folderx2%  ;- open folder
;run,%f2%        ;- run file
return
;--------------
URLdecode(str) {
   Loop
      If RegExMatch(str, "i)(?<=%)[\da-f]{1,2}", hex)
         StringReplace, str, str, `%%hex%, % Chr("0x" . hex), All
      Else Break
   Return, str
}
return
;============================================================

Re: RunCMD()

Posted: 21 Jun 2020, 16:39
by burque505
@garry, your code works great.
Regards,
burque505

Re: RunCMD()

Posted: 21 Jun 2020, 20:04
by xml123
burque505 wrote: I've been trying to figure out where I found this code, cli_stdio.ahk.
Thank you! That solves my problem, but not perfectly. Still a little problem about encoding. Anyway, thanks for your script.

Re: RunCMD()

Posted: 22 Jun 2020, 07:22
by burque505
@xml123, I'm glad it was at least of some help.
Have you tried (pick the correct code page)

Code: Select all

cli_stdout("","65001")
or

Code: Select all

cli_stdin("","65001")
for the encoding problem?
Regards,
burque505

Re: RunCMD()

Posted: 22 Jun 2020, 09:46
by xml123
@burque505
It work! Now the scripts works perfectly. Thanks for your generous help.

Re: RunCMD()

Posted: 22 Jun 2020, 10:05
by burque505
@xml123, I'm glad it worked for you. If you are able to share any of your code, I'm sure I could learn from it. I've never coded anything using cli_stdio.ahk myself, I just found it lurking in my file system. :D
Regards,
burque505

Re: RunCMD()

Posted: 22 Jun 2020, 10:23
by xml123
@burque505
Just a simple piece of code. Of course I can show it if you don't mind this.
I'm using sublimetext as my text editor. There is a cli version "subl.exe", which can be used like this: "subl [arguments] - Edit stdin". I want to create a shortcut that copy what I'm selecting directly into sublimetext instead of open the editor then copy and paste. Here is my code.

Code: Select all

#include lib/cli_stdio.ahk
!s::
	Send, ^c
	ClipWait
	cli_createprocess("C:\Program Files\SublimeText\subl.exe -")
	cli_stdin(Clipboard,"UTF-8")
	cli_close()
return

Re: RunCMD()

Posted: 22 Jun 2020, 10:38
by burque505
@xml123, thanks, I'll try it. I have Sublime Text available, I think.

EDIT: Thanks, works great! For my system I used this. I had to add a sleep command to wait for the program to load before pasting.

Code: Select all

#include cli_stdio.ahk
!u::
	Send, ^c
	ClipWait
	cli_createprocess("C:\Program Files\Sublime Text 3\subl.exe mytext.txt")
	cli_stdin(Clipboard,"UTF-8")
	sleep 2000
	Send, ^v
	cli_close()
return
Regards,
burque505

Re: RunCMD()

Posted: 22 Jun 2020, 20:01
by xml123
@burque505
Maybe you misunderstood me. The parameter - is necessary. In your situation the command should be C:\Program Files\Sublime Text 3\subl.exe mytext.exe -. In this case, you needn't sleep or send ctrl+v.

Append:
You are right. The versions of sublime text we use are different. I'm using the beta version 4074. Here is my help.

Code: Select all

Sublime Text build 4074

Usage: subl [arguments] [files]         Edit the given files
   or: subl [arguments] [directories]   Open the given directories
   or: subl [arguments] -- [files]      Edit files that may start with '-'
   or: subl [arguments] -               Edit stdin
   
 ...
It seems that build 3211 doesn't support editing stdin. Therefore the command cli_stdin(Clipboard,"UTF-8") is useless.
Cause it is really off-topic, I shouldn't add more posts about sublime.

Re: RunCMD()

Posted: 23 Jun 2020, 07:43
by burque505
@xml123, thanks, I did misunderstand (I also edited my post above to reflect 'mytext.txt', not 'mytext.exe', which was a typo).

However, I just tried it, and it doesn't work with my installation. Rather, it opens two text files ('mytext.txt' and '-') and, without a sleep command and ctrl+v it pastes nothing.

Perhaps the command line options in our respective versions are different?

Here's what they are in mine:

Code: Select all

Sublime Text build 3211

Usage: subl [arguments] [files]         Edit the given files
   or: subl [arguments] [directories]   Open the given directories

Arguments:
  --project <project>: Load the given project
  --command <command>: Run the given command
  -n or --new-window:  Open a new window
  -a or --add:         Add folders to the current window
  -w or --wait:        Wait for the files to be closed before returning
  -b or --background:  Don't activate the application
  -s or --stay:        Keep the application activated after closing the file
  -h or --help:        Show help (this message) and exit
  -v or --version:     Show version and exit

Filenames may be given a :line or :line:column suffix to open at a specific
location.
PS: This is getting pretty far off-topic from the OP. ;)

Regards,
burque505

Re: RunCMD()

Posted: 02 Jul 2020, 10:35
by Sam_
This function is awesome! Thank you for sharing it.
SKAN wrote:
23 Apr 2020, 16:55
This is not a streaming version. I didn't write one since no one asked for it.
Will you pretty please also provide a streaming version?

Re: RunCMD()

Posted: 02 Jul 2020, 17:45
by SKAN
Sam_ wrote:This function is awesome! Thank you for sharing it.
Thank you. :)
Sam_ wrote:Will you pretty please also provide a streaming version?
Quickly written.. not well tested. I will try to post a proper demo later. (streaming to an edit control)
Obsolete version

RunCMD() v0.93

Posted: 08 Jul 2020, 18:40
by SKAN
Code updated:
New non-blocking version with streaming support. Check out the demo in title post.
Image

Re: RunCMD() v0.93 : Capture stdout to variable. Non-blocking version. Pre-process/omit individual lines.

Posted: 09 Jul 2020, 07:58
by burque505
Nice, @SKAN, thank you!! :bravo:
Regards,
burque505

Re: RunCMD() v0.93 : Capture stdout to variable. Non-blocking version. Pre-process/omit individual lines.

Posted: 09 Jul 2020, 10:56
by elModo7
I had used a few methods to run cmds and retrieve their output, your seems the most complete and versatile, also the real time stream ret seems something I will be using for a few projects.
Thank you for your hard work @SKAN it really is well appreciated! :clap:

Re: RunCMD() v0.93 : Capture stdout to variable. Non-blocking version. Pre-process/omit individual lines.

Posted: 09 Jul 2020, 13:15
by SKAN
burque505 wrote:
09 Jul 2020, 07:58
Nice, @SKAN, thank you!! :bravo:
 
:) :thumbup:
Do let me know if documentation needs to be better.