Jump to content

Pipe to clipboard


jefhal
 Share

Recommended Posts

Perhaps there is an easier way to get command output into the clipboard, but I couldn't find it. Instead I found this utility to pipe output from a dos command to the clipboard. It's called clipboard.exe and I found it at: Pipe to clipboard utility

I use it a lot with @comspec to pump output directly into a variable, e.g.:

run(@comspec & ' /c  ' & "ipconfg /all |clipboard")
$var = clipget()
msgbox(1,"Clipboard contents= ",$var)

This avoids the need for a file to be created and saves me a few file handling steps. I don't know anything about the website, but given the extent of the programs he offers, my gut feeling is that the code is legit. I like it and hope some of you find it useful...

...by the way, it's pronounced: "JIF"... Bob Berry --- inventor of the GIF format
Link to comment
Share on other sites

If you use the AutoIt beta, then the example below would be another option.

It does not require external programs or temp files to function.

Instead, it use the inbuilt StdoutRead() beta function.

#include <Constants.au3>
Global $cmdOUT = ''

$PID = Run("ipconfig /all", "", @SW_HIDE, $STDOUT_CHILD)

While NOT @ERROR
      $cmdOUT &= StdoutRead($PID)
Wend

MsgBox(1, "IPConfig Output:", $cmdOUT)

-Kendall

Edit: Changed example to w0uter's suggestion.

Edit: Spelling.

Edit: Code box cleanup.

Edited by lte5000
Link to comment
Share on other sites

why not use

Global $cmdOUT = ''

While NOT @ERROR
      $cmdOUT &= StdoutRead($PID)
Wend

<{POST_SNAPBACK}>

Yep, that would be better.

Kendall

Edit: I didn't realize there was: &=

..'til now. :whistle:

Edit: Spelling.

Edited by lte5000
Link to comment
Share on other sites

Thanks to w0uter and lte5000. Now I can eliminate one more external program to do my work...

Edited by jefhal
...by the way, it's pronounced: "JIF"... Bob Berry --- inventor of the GIF format
Link to comment
Share on other sites

The top script outputs any information from a DOS session to the clipboard. The remaining ones use functions of the beta to directly read any output into an AutoIt variable, ridding the need for temporary files or damage to current clipboard information.

Link to comment
Share on other sites

Hi !

I have developped a script,with autoit! who do the same work than the clipboard.exe utility (see 1st message).

But it's run OK only if the script is passed by Aut2exe (if he is compiled like fclipboard.exe)

Here, the source-code :

;fclipboard.au3 --> fclipboard.exe
$data = ConsoleRead()
ClipPut($data)

And, a sample of use :

@echo off
:: sendclip.bat

echo AZERTYUIOP|fclipboard
echo Envoyé au clipboard.

*sorry for my bad english *

:whistle:

Edited by Michel Claveau
Link to comment
Share on other sites

  • 3 weeks later...

Here the release, number 2

perhaps, I can to sell this mega-great developpment ?

Awesome!!!!! Such an elegant solution....

However, this is very weird, when I run ipconfig /all into your code: ipconfig /all | fclipboard.exe

the exact same thing happens as happens with stdout_child. The output gets chopped off. I lose the last 15 or so lines of the output. Does this happen because time runs out? I do not understand...

...by the way, it's pronounced: "JIF"... Bob Berry --- inventor of the GIF format
Link to comment
Share on other sites

written straight in the browser, so could be buggy.

I should have such bugs! Brilliant!! This solves it !!! Thanks to all of you.

w0uter, so that I understand Why this works, when will @error be true? Is it TRUE when there is no more output from the process?

Am I mistaken, or are there two expressions: &= and =&? &= means concat, =& means by reference? Where can I learn more about by reference, as the examples in the forum and the help file are still confusing me... Thanks!

...by the way, it's pronounced: "JIF"... Bob Berry --- inventor of the GIF format
Link to comment
Share on other sites

w0uter, Michel, et al:

Thanks again for the ideas and code. Here's my latest incarnation of clipit. I found that @error was not going to "true", so I added a manual "terminate" and a "timed" one. If I let the timer run for 10 seconds, I could get 529 lines from: dir c:\ /s |clipit

In 50 seconds I got 47000 lines of output from the above test (Pentium 4, 2.8Ghz, WinXP)

In five seconds I could be sure to get everything that ipconfig puts out.

For now, this avoids the "truncation" that happens sometimes with stdout_child.

; This code must be compiled (clipit.exe) and run from the dos window with the "pipe" symbol "|" : e.g.:
; c:>ipconfig /all | clipit.exe
; This will put the output of ipconfig /all into the clipboard. You can then paste it where you like...
; Based on ideas from many authors in thread "Scripts and Scraps" "Pipe to clipboard"

#Include <Date.au3>
$starttime = TimerInit()
; HotKeySet("{ESC}", "Terminate"); unremark for manually stopping the clipit process
$s_line = ''

While Not @ERROR
    $s_line &= ConsoleRead()
; ToolTip($s_line); unremark for debugging
    If TimerDiff($starttime) > 10000 Then ExitLoop; 10000 = 10 seconds. choose value that gives your process enough time
Wend

ClipPut($s_line)

Func Terminate()
Exit 0
EndFunc

Question: Since this tool is now buried deep in this thread, should it be posted again in a new thread at the top so that it's easier to find? I'm sure a number of people could use it, not just within AutoIT...

Edited by jefhal
...by the way, it's pronounced: "JIF"... Bob Berry --- inventor of the GIF format
Link to comment
Share on other sites

you forgot that @error is for the preveus command only.

You are correct, Sir! Thanks again. I just tested the version below at work, and it's fabulous!

HotKeySet("{ESC}", "Terminate"); unremark for manually stopping the clipit process
$s_line = ''
While Not @ERROR
$s_line &= ConsoleRead()
Wend
ClipPut($s_line)

Func Terminate()
Exit 0
EndFunc
...by the way, it's pronounced: "JIF"... Bob Berry --- inventor of the GIF format
Link to comment
Share on other sites

Hi,

nice idea. ;)

I will try it next time.

I usually use this

#include <file.au3>
Dim $array
Run(@ComSpec & " /c " & 'ipconfig /all >> c:\ipconf.log', "", @SW_HIDE)
ProcessWaitClose("cmd.exe")
_FileReadToArray("c:\ipconf.log",$array)
FileDelete("c:\ipconf.log")
;---------Example for use--------------
For $x = 1 to $array[0]
    Msgbox(0,'Line ' & $x, $array[$x])
Next
Link to comment
Share on other sites

I usually use this

Run(@ComSpec & " /c " & 'ipconfig /all >> c:\ipconf.log', "", @SW_HIDE)
Hi Matrix112-

I used that same method at first. Then I switched to the stdout_child method as it was much more elegant. However, I kept losing the end of the output. Then I found a shareware program called "clipboard", which worked great except I was relying on code whose source I could not see. Finally, several of us evolved the solution in this thread. Perhaps some day I will either figure out how to use stdout_child to get all of the output or (if I am using it properly) it will be changed to support the complexity of psexec.exe...

By the way, why are you capturing ipconfig output?

Edited by jefhal
...by the way, it's pronounced: "JIF"... Bob Berry --- inventor of the GIF format
Link to comment
Share on other sites

Hey, jefhal;

Again, it's dangerous to test the value of @error for a conditional loop like the previous example, because any function call that you make could change @error's value. It's easy (at least for me) to forget this and put a MsgBox() in the loop for debug purposes and then waste time debugging why my script broke...

The construct that has been traditionally used for XxxYyyRead() has been:

While 1
 ; Don't seperate the next two lines
  $foo &= ConsoleRead()
  If @error = -1 Then ExitLoop
WEnd

Also, could you post/PM/otherwise direct me to your code that used StdoutRead() that was truncating the output of the child process? Internally StdoutRead() and ConsoleRead() use identical code, so they both should have gotten the same output from the same program; if StdoutRead() is broken in some way I need to know about it...

Yes yes yes, there it was. Youth must go, ah yes. But youth is only being in a way like it might be an animal. No, it is not just being an animal so much as being like one of these malenky toys you viddy being sold in the streets, like little chellovecks made out of tin and with a spring inside and then a winding handle on the outside and you wind it up grrr grrr grrr and off it itties, like walking, O my brothers. But it itties in a straight line and bangs straight into things bang bang and it cannot help what it is doing. Being young is like being like one of these malenky machines.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...