Jump to content

Sky Slate Blueberry Blackcurrant Watermelon Strawberry Orange Banana Apple Emerald Chocolate

how to copy a file to the clipboard


  • Please log in to reply
58 replies to this topic
  • Guests
  • Last active:
  • Joined: --
@ lexikos

im using this:
#IfWinActive, ahk_class TTOTAL_CMD
$#a::
	Clipboard =
	PostMessage, 1075, 2029, , , ahk_class TTOTAL_CMD
	ClipWait, 1
Return

and want to include filename+extension with
$#+a::

Lexikos
  • Administrators
  • 9844 posts
  • AutoHotkey Foundation
  • Last active:
  • Joined: 17 Oct 2006

However, I'm reluctant to use GetDIBits for this, as the documentation says the hBitmap should be DDB with G(S)etDIBits.

I wasn't suggesting it be used for anything; just curious. What's more curious is that the DIB worked with GetDIBits...

PS. Maybe converting to DDB is a better choice, using BitBlt or alike, which is equally applicable for any value of bpp. Even in case of bpp>8, using CreateDIBitmap appears simpler.

That's a rather backwards-named function... Create(DDBfrom)DIBitmap :roll:. I would think GetObject is more suitable, since it retains the original format of the image. The editor (whatever you're going to paste the image into) isn't necessarily going to be the same format as the screen.

@guest: I have no idea what the PostMessage is supposed to do, nor what you're asking for.

Sean
  • Members
  • 2462 posts
  • Last active: Feb 07 2012 04:00 AM
  • Joined: 12 Feb 2007

What's more curious is that the DIB worked with GetDIBits...

As a matter of fact, there isn't anything GetDIBits has to do additionally in case of DIB. All is already there, so what it has to do is merely something similar to GetObject with DIBSECTION. So it may be only a matter of adding an additional check whether it's DIB or DDB.
Nevertheless, it may be preferable to not use it in public if documentation does not grant it officially.

That's a rather backwards-named function... Create(DDBfrom)DIBitmap :roll:.

I agree Window's GDI functions appear somewhat as a mess. Maybe of historical origin/constraint. BTW, I regard Bitmap in the name of GDI functions as DDB. Create(From)DIB(ToB)itmap.

  • Guests
  • Last active:
  • Joined: --
@ lexikos

it copies the path in text of the selected dir/folder. i want this hotkey to include filename.ext with shift added. i thought that this threads script was doing this but i was wrong.

Sean
  • Members
  • 2462 posts
  • Last active: Feb 07 2012 04:00 AM
  • Joined: 12 Feb 2007

it copies the path in text of the selected dir/folder. i want this hotkey to include filename.ext with shift added.

Maybe you want is:
PostMessage, 1075, 2018, , , ahk_class TTOTAL_CMD


  • Guests
  • Last active:
  • Joined: --
@Sean

thank you that is exactly what i needed.

totalmig
  • Members
  • 151 posts
  • Last active: May 20 2010 09:27 AM
  • Joined: 22 Jul 2008
Hi

I am trying to use the function you wrote lexikos. I am trying to do something very simple like:

::Test::
FiletoClipboard("C:/somefile.txt")

Tried using the things you wrote lexikos, put could not find out how to get it working. Would anyone care to elaborate? Tried just copying the entire function, but could not find the place to specify the filepath.

Hope some of you have the time to help me, thanks.

SoLong&Thx4AllTheFish
  • Members
  • 4999 posts
  • Last active:
  • Joined: 27 May 2007
try
C:[color=red]\[/color]somefile.txt


totalmig
  • Members
  • 151 posts
  • Last active: May 20 2010 09:27 AM
  • Joined: 22 Jul 2008
ops, just a typo here. Did not do that in my script. Getting error - call to nonexistent function? Do i need to somehow implement the function first?

SoLong&Thx4AllTheFish
  • Members
  • 4999 posts
  • Last active:
  • Joined: 27 May 2007
Yes, you need to include the entire function in your script and then call it the way you did. Your errors says it can not find the function so you didn't include it. Look into #include and Lib, read the documentation on functions.

totalmig
  • Members
  • 151 posts
  • Last active: May 20 2010 09:27 AM
  • Joined: 22 Jul 2008
Made it work, thanks alot :)

Currently i have around 2000 pictures in different folders that i would have to refer to use, and they are all jpeg. I need to send them over email so converting them all to bmp would take up alot more space, is there any way to make it work with jpegs?

Don't know if my questions are stupid, but thanks for taking the time :)

mitchi
  • Members
  • 9 posts
  • Last active: Jan 31 2009 06:21 AM
  • Joined: 14 Jun 2008
Hello, I'm using this code.

; Select the text yourself, then use this shortcut
^n::
clipboard =  ; Start off empty to allow ClipWait to detect when the text has arrived.
Send ^c
ClipWait  ; Wait for the clipboard to contain text.
FileToClipboard(clipboard)
return


;Select all the path in a typical execute dialog or open file dialog
^b::
clipboard = ;
Send {AppsKey}{a}
Send ^c
ClipWait
FileToClipboard(clipboard)
return

;path := "F:\boo.txt"
;FileToClipboard(path)

FileToClipboard(PathToCopy) 
{ 
    ; Expand to full paths: 
    Loop, Parse, PathToCopy, `n, `r 
        Loop, %A_LoopField%, 1 
            temp_list .= A_LoopFileLongPath "`n" 
    PathToCopy := SubStr(temp_list, 1, -1) 
    
    ; Allocate some movable memory to put on the clipboard. 
    ; This will hold a DROPFILES struct and a null-terminated list of 
    ; null-terminated strings. 
    ; 0x42 = GMEM_MOVEABLE(0x2) | GMEM_ZEROINIT(0x40) 
    hPath := DllCall("GlobalAlloc","uint",0x42,"uint",StrLen(PathToCopy)+22) 
    
    ; Lock the moveable memory, retrieving a pointer to it. 
    pPath := DllCall("GlobalLock","uint",hPath) 
    
    NumPut(20, pPath+0) ; DROPFILES.pFiles = offset of file list 
    
    pPath += 20 
    ; Copy the list of files into moveable memory. 
    Loop, Parse, PathToCopy, `n, `r 
    { 
        DllCall("lstrcpy","uint",pPath+0,"str",A_LoopField) 
        pPath += StrLen(A_LoopField)+1 
    } 
    
    ; Unlock the moveable memory. 
    DllCall("GlobalUnlock","uint",hPath) 
    
    DllCall("OpenClipboard","uint",0) 
    ; Empty the clipboard, otherwise SetClipboardData may fail. 
    DllCall("EmptyClipboard") 
    ; Place the data on the clipboard. CF_HDROP=0xF 
    DllCall("SetClipboardData","uint",0xF,"uint",hPath) 
    DllCall("CloseClipboard") 
}

Works well. But what if I want to CUT the file instead of copying it? How do it do it?

Cyclotron2k
  • Guests
  • Last active:
  • Joined: --
bump!

I'd love to know how to CUT a file to clipboard as well...

maraskan_user
  • Members
  • 52 posts
  • Last active: Dec 08 2014 11:18 PM
  • Joined: 20 Jun 2008

I'd love to know how to CUT a file to clipboard as well...


This can be done, if right after FileToClipboard, you do this:

DropEffectHex = 020000     ; "020000" is cut   "050000" is copy
Hex2Bin(DropEffect,DropEffectHex)
SetClipboardData("Preferred DropEffect",DropEffect,3,false)

You'll need PhiLho's SetClipboardData() and BinaryEncodingDecoding.ahk from http://www.autohotke...opic.php?t=8402.

Edit:
Alternatively, you can use this version of FileToClipboard, which is just a combination of the three functions needed into one.
Usage: FileToClipboard(Pathlist,Method)
Method can be copy or cut

   FileToClipboard(PathToCopy,Method="copy")
      {
      Loop,Parse,PathToCopy,`n,`r
         {
         Loop,%A_LoopField%,1
            {
            temp_list .= A_LoopFileLongPath "`n"
            }
         }            
      PathToCopy := SubStr(temp_list,1,-1)

      hPath := DllCall("GlobalAlloc","uint",0x42,"uint",StrLen(PathToCopy)+22)
      pPath := DllCall("GlobalLock","uint",hPath)
      NumPut(20,pPath+0)
      pPath += 20
      Loop,Parse,PathToCopy,`n,`r
         {
         DllCall("lstrcpy","uint",pPath+0,"str",A_LoopField)
         pPath += StrLen(A_LoopField)+1
         }
      DllCall("GlobalUnlock","uint",hPath)
      DllCall("OpenClipboard","uint",0)
      DllCall("EmptyClipboard")
      DllCall("SetClipboardData","uint",0xF,"uint",hPath)

      mem := DllCall("GlobalAlloc","UInt",2,"UInt",4)
      str := DllCall("GlobalLock","UInt",mem)
      if Method=copy
         DllCall("RtlFillMemory","UInt",str,"UInt",1,"UChar",0x05)
      else if Method=cut
         DllCall("RtlFillMemory","UInt",str,"UInt",1,"UChar",0x02)
      else
         {
         DllCall("CloseClipboard")
         return
         }
      DllCall("RtlZeroMemory","UInt",str + 1,"UInt",1)
      DllCall("RtlZeroMemory","UInt",str + 2,"UInt",1)
      DllCall("RtlZeroMemory","UInt",str + 3,"UInt",1)
      DllCall("GlobalUnlock","UInt",mem)

      cfFormat := DllCall("RegisterClipboardFormat","Str","Preferred DropEffect")
      DllCall("SetClipboardData","UInt",cfFormat,"UInt",mem)
      DllCall("CloseClipboard")
      return
      }


automaticman
  • Members
  • 658 posts
  • Last active: Nov 20 2012 06:10 PM
  • Joined: 27 Oct 2006
Does FileToClipboard(PathToCopy) also work with multiple files selected at once and all of them are copied into the clipboard. How would be an example if I would want to copy all .bla files from a given directory into the clipboard?

Can the method be also "paste"?