AHK Script to Copy & Paste File to Current TC Window

English support forum

Moderators: white, Hacker, petermad, Stefan2

Post Reply
BeRoM
Junior Member
Junior Member
Posts: 15
Joined: 2019-10-25, 14:53 UTC
Location: Texas

AHK Script to Copy & Paste File to Current TC Window

Post by *BeRoM »

Dual-posted on AHK Forum as well:

Hi all!

I am looking for a script to copy and paste a specific file to my Total Commander window at the press of a HotKey. My current script worked beautifully for several years until yesterday, and I cannot figure out why it does not work anymore. When I try to run it, it crashes without an error message.

Here is the current non-working script:

Code: Select all

#IfWinActive, Total Commander ;  this script adds the template file to your current Total Commander Window with Ctrl+Shift+4
^+4::FileToClipboard("\\homeserver\users\ID001\template.dbf")
FileToClipboard(PathToCopy,Method="copy")
   {
   FileCount:=0
   PathLength:=0

   ; Count files and total string length
   Loop,Parse,PathToCopy,`n,`r
      {
      FileCount++
      PathLength+=StrLen(A_LoopField)
      }

   pid:=DllCall("GetCurrentProcessId","uint")
   hwnd:=WinExist("ahk_pid " . pid)
   ; 0x42 = GMEM_MOVEABLE(0x2) | GMEM_ZEROINIT(0x40)
   hPath := DllCall("GlobalAlloc","uint",0x42,"uint",20 + (PathLength + FileCount + 1) * 2,"UPtr")
   pPath := DllCall("GlobalLock","UPtr",hPath)
   NumPut(20,pPath+0),pPath += 16 ; DROPFILES.pFiles = offset of file list
   NumPut(1,pPath+0),pPath += 4 ; fWide = 0 -->ANSI,fWide = 1 -->Unicode
   Offset:=0
   Loop,Parse,PathToCopy,`n,`r ; Rows are delimited by linefeeds (`r`n).
      offset += StrPut(A_LoopField,pPath+offset,StrLen(A_LoopField)+1,"UTF-16") * 2

   DllCall("GlobalUnlock","UPtr",hPath)
   DllCall("OpenClipboard","UPtr",hwnd)
   DllCall("EmptyClipboard")
   DllCall("SetClipboardData","uint",0xF,"UPtr",hPath) ; 0xF = CF_HDROP

   ; Write Preferred DropEffect structure to clipboard to switch between copy/cut operations
   ; 0x42 = GMEM_MOVEABLE(0x2) | GMEM_ZEROINIT(0x40)
   mem := DllCall("GlobalAlloc","uint",0x42,"uint",4,"UPtr")
   str := DllCall("GlobalLock","UPtr",mem)

   if (Method="copy")
      DllCall("RtlFillMemory","UPtr",str,"uint",1,"UChar",0x05)
   else if (Method="cut")
      DllCall("RtlFillMemory","UPtr",str,"uint",1,"UChar",0x02)
   else
      {
      DllCall("CloseClipboard")
      return
      }

   DllCall("GlobalUnlock","UPtr",mem)

   cfFormat := DllCall("RegisterClipboardFormat","Str","Preferred DropEffect")
   DllCall("SetClipboardData","uint",cfFormat,"UPtr",mem)
   DllCall("CloseClipboard")
setwindelay, 2
Send ^v
   return
   }
return
My goal is to simply press Ctrl+Shift+4 and copy the same template file ("\\homeserver\users\ID001\template.dbf") to my current Total Commander window.

The default template.dbf file always stays in it's home location: \\homeserver\users\ID001\template.dbf

All I need this script to do is instantly copy/paste the default template.dbf file into my current TC location.

I believe my current script stopped working due to a Windows update, because it not longer works in Win 10, but still works perfectly in Windows 7.

I am open to any ideas.

Thank you,

BeRoM
User avatar
Stefan2
Power Member
Power Member
Posts: 4157
Joined: 2007-09-13, 22:20 UTC
Location: Europa

Re: AHK Script to Copy & Paste File to Current TC Window

Post by *Stefan2 »

a) do you have access to "\\homeserver\users\ID001\" on your win10 device?
b) add a few "msgbox step_1" commands between the code lines for to see where it stops.





 
BeRoM
Junior Member
Junior Member
Posts: 15
Joined: 2019-10-25, 14:53 UTC
Location: Texas

Re: AHK Script to Copy & Paste File to Current TC Window

Post by *BeRoM »

Stefan2 wrote: 2022-08-18, 15:12 UTC a) do you have access to "\\homeserver\users\ID001\" on your win10 device?
b) add a few "msgbox step_1" commands between the code lines for to see where it stops.
Yes, I can access "\\homeserver\users\ID001\" without any issues.

Great idea! I placed several msgbox commands, and the code breaks after I click OK on Step 6:

Code: Select all

msgbox step_6
   NumPut(20,pPath+0),pPath += 16 ; DROPFILES.pFiles = offset of file list
msgbox step_7
   NumPut(1,pPath+0),pPath += 4 ; fWide = 0 -->ANSI,fWide = 1 -->Unicode
msgbox step_8
   Offset:=0
   Loop,Parse,PathToCopy,`n,`r ; Rows are delimited by linefeeds (`r`n).
msgbox step_9
      offset += StrPut(A_LoopField,pPath+offset,StrLen(A_LoopField)+1,"UTF-16") * 2
msgbox step_10
msgbox step_11
   DllCall("GlobalUnlock","UPtr",hPath)
   DllCall("OpenClipboard","UPtr",hwnd)
   DllCall("EmptyClipboard")
   DllCall("SetClipboardData","uint",0xF,"UPtr",hPath) ; 0xF = CF_HDROP
msgbox step_12
   ; Write Preferred DropEffect structure to clipboard to switch between copy/cut operations
   ; 0x42 = GMEM_MOVEABLE(0x2) | GMEM_ZEROINIT(0x40)
   mem := DllCall("GlobalAlloc","uint",0x42,"uint",4,"UPtr")
   str := DllCall("GlobalLock","UPtr",mem)
msgbox step_13
   if (Method="copy")
      DllCall("RtlFillMemory","UPtr",str,"uint",1,"UChar",0x05)
   else if (Method="cut")
      DllCall("RtlFillMemory","UPtr",str,"uint",1,"UChar",0x02)
   else
      {
      DllCall("CloseClipboard")
      return
      }
msgbox step_14
   DllCall("GlobalUnlock","UPtr",mem)

   cfFormat := DllCall("RegisterClipboardFormat","Str","Preferred DropEffect")
   DllCall("SetClipboardData","uint",cfFormat,"UPtr",mem)
   DllCall("CloseClipboard")
msgbox step_15
setwindelay, 2
Send ^v
   return
   }

return
Stiltzkin
Member
Member
Posts: 182
Joined: 2004-09-10, 17:15 UTC

Re: AHK Script to Copy & Paste File to Current TC Window

Post by *Stiltzkin »

this seems overly complicated for creating a template.ext file.



why not something like this?:

Code: Select all

[em_template_1]
cmd=%commander_path%\Tools\AskParam64.exe
param=/c"Template" /p(e)"input1:Filename:" cmd /c copy "\\server\templates\word.odt" "%P%%%%input1%%%%.odt"
menu=New *.odt
iconic=1
button=WCMICON2.DLL,28
path=.
[em_template_1] can be set to any hotkey in totalcmd
BeRoM
Junior Member
Junior Member
Posts: 15
Joined: 2019-10-25, 14:53 UTC
Location: Texas

Re: AHK Script to Copy & Paste File to Current TC Window

Post by *BeRoM »

Stiltzkin wrote: 2022-08-18, 16:59 UTC this seems overly complicated for creating a template.ext file.



why not something like this?:

Code: Select all

[em_template_1]
cmd=%commander_path%\Tools\AskParam64.exe
param=/c"Template" /p(e)"input1:Filename:" cmd /c copy "\\server\templates\word.odt" "%P%%%%input1%%%%.odt"
menu=New *.odt
iconic=1
button=WCMICON2.DLL,28
path=.
[em_template_1] can be set to any hotkey in totalcmd
That's exactly what I did... I had no idea you could do this, so I've replaced the AHK hotkeys using this method, and it seems to be working well. I have multiple files tied to hotkeys like this, so there is a slight delay compared to the AHK hotkeys, but it's better than not having them at all! Thanks for the help!
Post Reply