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
nitrix
  • Guests
  • Last active:
  • Joined: --
Hi,

is it possible to copy a file (or a folder) to the clipboard for later pasting

i don't want to copy the content of the file but the "handle" so that i can copy the file later within a file explorer

exemple :

#i::
copyfile(file.pdf)
return

1) i press Windows+i
2) then i go into any folder using windows explorer and then i paste the file using ctrl+v

final goal : use an autohotkey script with FARR (find and run robot)

copyfile.ahk c:\somefile.pdf

Thanks for you help

engunneer
  • Moderators
  • 9162 posts
  • Last active: Sep 12 2014 10:36 PM
  • Joined: 30 Aug 2005
when you copy the file to the clipboard it is only copying the (full) path. you may be able to paste the path into explorer to move the file.

try copying a file and then doing msgbox, %clipboard% to see. (or ClipboardAll)

nitrix
  • Guests
  • Last active:
  • Joined: --
i think it shows the path, but the thing is you cannot use it to move or paste the file into windows explorer.

what i'd like to do is to [insert correct words] the file so that one can paste it into explorer

i did see a small utility to do that (was coded in C++ if i remember well)but it did not work well on my computer...

i guess it's feasible with a dll call, but i'm just to much of a newbie to find out ;)

Razlin
  • Members
  • 454 posts
  • Last active: Feb 05 2014 06:21 PM
  • Joined: 05 Nov 2007
If I understand correctly you want to

#i:: 
SEND ^C 
return

that will copy the file if you have it selected.

then

#j:: 
SEND ^V
return
that would paste "or create" the new file


Which is the same as actually doing ctrl c and ctrl v so you dont save anything
-=raz=-

Lexikos
  • Administrators
  • 9844 posts
  • AutoHotkey Foundation
  • Last active:
  • Joined: 17 Oct 2006
Razlin,
looks like nitrix wants to copy a specific file (path) to the clipboard, not the selected file in Explorer.

nitrix,
Copying the path to the clipboard in text form won't work. You need to use SetClipboardData(). Try this:
(Edit: See further down for the updated version.)
FileToClipboard(PathToCopy)
{
    ; Expand to full path:
    Loop, %PathToCopy%, 1
        PathToCopy := A_LoopFileLongPath
    
    ; Allocate some movable memory to put on the clipboard.
    ; This will hold a DROPFILES struct, the string, and an (extra) null terminator
    ; 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
    
    ; Copy the string into moveable memory.
    DllCall("lstrcpy","uint",pPath+20,"str",PathToCopy)
    
    ; 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")
}


nitrix
  • Guests
  • Last active:
  • Joined: --
thank you so much lexikos !!

this is exactly what i wanted and it works like a charm

you made my day !! :D

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

do you think it is possible to modifiy this function to be able to copy multiple files/folders ?

i managed to make it work for one file or one folder but not for multiple files/folders

thanks in advance

Nitrix

Lexikos
  • Administrators
  • 9844 posts
  • AutoHotkey Foundation
  • Last active:
  • Joined: 17 Oct 2006
Each path needs to be terminated by a null character, with another null character at the end to terminate the list. Since AutoHotkey uses null characters to mark "end of string", none of AutoHotkey's string functions will work for this. We need to either:[*:2ps8ekaz]Copy the whole string to the allocated memory, then replace (using a Loop, NumGet and NumPut) each delimiter character with a null character; or
[*:2ps8ekaz]Use a parsing loop to copy each sub-string into the allocated memory, leaving a null character between each.The following uses the latter method:
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")
}
The input should be a new-line (`n or `r`n) delimited list of paths.

Edit: Now expands relative paths for a list of paths (before it would only expand one.)

nitrix
  • Guests
  • Last active:
  • Joined: --
thanks again lexikos !!
this works great :D

it makes a great addition to keyboard launcher like FARR or Launchy

i may post your function on FARR forum if that's ok with you (with appropriate credits of course)

Cheers, Nitrix

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

if that's ok with you

Sure.

I noticed a small oversight: the function will expand one relative path to a full path, but not a list of relative paths. I've updated the script in my previous post.

Actually, as a side-effect of using a file loop to expand relative paths, the function now also accepts a list of filters. For instance,
FileToClipboard("*.txt`n*.doc")
8)

  • Guests
  • Last active:
  • Joined: --
hmm i need this but to what do i bind the hotkey? how?

Rhys
  • Members
  • 761 posts
  • Last active: Aug 09 2013 04:53 PM
  • Joined: 17 Apr 2007
Lexikos, that's a great function. I have a question - Why do images that are copied to the clipboard with it behave differently than images captured via {printscreen}?
For example, if I paste an image that I've injected into the clipboard with your funtion into my Outlook message, it automatically attaches the file. When I {printscreen} and paste, I get an image in the body of the email.
Word, however will paste an image into the document if I use your function - However, I think MS Word does manipulate the data somehow. After I paste into Word, if I paste into Outlook again, I get a tiny little icon of a picture :p

Lexikos
  • Administrators
  • 9844 posts
  • AutoHotkey Foundation
  • Last active:
  • Joined: 17 Oct 2006
FileToClipboard() copies the paths of files. It does not copy any file data, image or otherwise. PrintScreen puts image data on the clipboard.

@Guest: The same way you'd bind anything to a hotkey.
hotkey::FileToClipboard("somefilepath")


nitrix
  • Guests
  • Last active:
  • Joined: --

It does not copy any file data, image or otherwise. PrintScreen puts image data on the clipboard


that would be pretty nice however !

CopyAsCapture(someimage)

pretty similar to a FileRead... for text files...

for a graphic designer, using a keyboard launcher, that would be pretty amazing to be able to copy images into the clipboard for pasting into photoshop or other softwares...

and since lexikos is one amazing code guru :D maybe he has some ideas !

cheers, nitrix

Lexikos
  • Administrators
  • 9844 posts
  • AutoHotkey Foundation
  • Last active:
  • Joined: 17 Oct 2006
Even easier than FileToClipboard!
; Copies image data from file to the clipboard.
ImageToClipboard(Filename)
{
    hbm := DllCall("LoadImage","uint",0,"str",Filename,"uint",0,"int",0,"int",0,"uint",0x10)
    if !hbm
        return
    DllCall("OpenClipboard","uint",0)
    DllCall("EmptyClipboard")
    ; Place the data on the clipboard. CF_BITMAP=0x2
    if ! DllCall("SetClipboardData","uint",0x2,"uint",hbm)
        DllCall("DeleteObject","uint",hbm)
    DllCall("CloseClipboard")
}
I'm not sure what formats LoadImage() supports (probably only .bmp). If it doesn't work with a particular format, search the forums for GDI+ image loading. (Google might work better, since the query has a "+".)