Page 1 of 1

Create new empty.docx (or copy empty.docx saved somewhere on my computer) in(to) current folder with shortcut?

Posted: 20 Jan 2024, 16:17
by michaelbeijer
Hi guys,

Does anyone know how to achieve this:

1. I click in a folder, either in Windows Explorer or in XYplorer
2. press a keyboard shortcut

2a. a new .docx file is created in the current folder
or
2b. a .docx somewhere on my computer is copied into the current folder

Any pointers would be greatly appreciated!

Michael

Re: Create new empty.docx (or copy empty.docx saved somewhere on my computer) in(to) current folder with shortcut?

Posted: 20 Jan 2024, 16:26
by burque505
Hi, @michaelbeijer, I have something for v1 that I got from the forum, still looking for the link. In the meantime, this may give you some ideas.
The script I was looking for is at this page.

Re: Create new empty.docx (or copy empty.docx saved somewhere on my computer) in(to) current folder with shortcut?

Posted: 20 Jan 2024, 16:45
by michaelbeijer
Thanks @burque505! I'll have a look!

I also found this: viewtopic.php?f=76&t=101873&p=555936#p555936 … which sort of works, albeit not reliably, and also not in XYplorer (although I think I can fix that by getting the window class for XYplorer with Window Spy; not sure how to make it work in multiple file explorers at once though).

Re: Create new empty.docx (or copy empty.docx saved somewhere on my computer) in(to) current folder with shortcut?

Posted: 20 Jan 2024, 17:08
by burque505
You might try this to add XYPlorer :

Code: Select all

If WinActive("ahk_class CabinetWClass") OR WinActive("ahk_class Progman") OR WinActive("ahk_class WorkerW") OR WinActive("ahk_exe XYplorer.exe) {
		; Do stuff
}
I don't have XYPlorer installed anymore, so I can't check the 'ahk_class'.
(Volgens mij moet dat 'ahk_class ThunderRT6FormDC' zijn voor XYPlorer, maar dat zal niet veel helpen. Te veel apps hebben dat als 'ahk_class'.)

Re: Create new empty.docx (or copy empty.docx saved somewhere on my computer) in(to) current folder with shortcut?

Posted: 20 Jan 2024, 17:52
by michaelbeijer
Thanks/bedankt!

inderdaad:

ahk_class ThunderRT6FormDC
ahk_exe XYplorer.exe
ahk_pid 14800
ahk_id 460124

Re: Create new empty.docx (or copy empty.docx saved somewhere on my computer) in(to) current folder with shortcut?

Posted: 21 Jan 2024, 12:46
by michaelbeijer

Code: Select all

^+n::
flnm := "comments.docx"
if !(path := getpath()) or (fileexist(path "\" flnm))
	return
oWord := ComObjCreate("word.application")
oWord.Documents.Add
oWord.Visible := True
oword.activeDocument.SaveAs(path "\" flnm)
return

getpath() {
	WinGetClass class, % "ahk_id " hwnd := WinExist("A")
	if (class ~= "CabinetWClass") OR (class ~= "ThunderRT6FormDC") OR (ahk_exe ~= "XYplorer.exe")
		for window in ComObjCreate("Shell.Application").Windows
			if (window.hwnd==hwnd)
				return window.Document.Folder.Self.Path
	}
OK, so I am trying to get this to work in XYplorer as well. It works pretty reliably in Windows explorer.

this was the original I found online:

Code: Select all

^+n::
flnm := "comments.docx"
if !(path := getpath()) or (fileexist(path "\" flnm))
	return
oWord := ComObjCreate("word.application")
oWord.Documents.Add
oWord.Visible := True
oword.activeDocument.SaveAs(path "\" flnm)
return

getpath() {
	WinGetClass class, % "ahk_id " hwnd := WinExist("A")
	if (class ~= "(Cabinet|Explore)WClass")
		for window in ComObjCreate("Shell.Application").Windows
			if (window.hwnd==hwnd)
				return window.Document.Folder.Self.Path
	}

Re: Create new empty.docx (or copy empty.docx saved somewhere on my computer) in(to) current folder with shortcut?  Topic is solved

Posted: 21 Jan 2024, 13:29
by WKen

Code: Select all

#Requires AutoHotkey v2

; Explorer ------------------------------
^+n::
{
	flnm := "comments.docx"
	if !(path := getpath()) or (fileexist(path "\" flnm))
		return
	oWord := ComObject("word.application")
	oWord.Documents.Add
	oWord.Visible := True
	oword.activeDocument.SaveAs(path "\" flnm)
	return
}

getpath() {
	class := WinGetClass("ahk_id " hwnd := WinExist("A"))
	if (class ~= "(Cabinet|Explore)WClass")
		for window in ComObject("Shell.Application").Windows
			if (window.hwnd==hwnd)
				return window.Document.Folder.Self.Path
}


; XYplorer ------------------------------
; Get the hwnd of the script
G_OwnHWND := A_ScriptHwnd

; Get messages back from XYplorer
OnMessage(0x4a, Receive_WM_COPYDATA)
dataReceived := "", programPath := ""

#HotIf winActive("ahk_class ThunderRT6FormDC")

^+n::
{
	flnm := "comments.docx"
	if !(path := XY_Get(false, false)) or (fileexist(path "\" flnm))
		return
	oWord := ComObject("word.application")
	oWord.Documents.Add
	oWord.Visible := True
	oword.activeDocument.SaveAs(path "\" flnm)
	return
}

#HotIf



XY_Get(bAll:=false, bSelection:=false)
{
    xyQueryScript := '::if (!' bAll ' && !' bSelection ') {$return = "<curpath>"`;} elseif (' bAll ') {$return = listpane(, , , "<crlf>")`;} elseif (' bSelection ') {$return = get("SelectedItemsPathNames", "<crlf>")`;} copydata ' G_OwnHWND ', "$return", 2`;'

    if xyHwnd := GetXYHWND()
       Send_WM_COPYDATA(xyHwnd, xyQueryScript)

    return dataReceived
}


GetXYHWND() {
	static xyClass := 'ahk_class ThunderRT6FormDC'

    if WinExist(xyClass) {
        for xyid in WinGetList(xyClass)
            for ctrl in WinGetControls(xyid)
                if A_Index = 10
                   return xyid
    }
}


Send_WM_COPYDATA(xyHwnd, message) {
   if !(xyHwnd)
       return

   size := StrLen(message)

   COPYDATA := Buffer(A_PtrSize * 3)
   NumPut("Ptr", 4194305, COPYDATA, 0)
   NumPut("UInt", size * 2, COPYDATA, A_PtrSize)
   NumPut("Ptr", StrPtr(message), COPYDATA, A_PtrSize * 2)

   return DllCall("User32.dll\SendMessageW", "Ptr", xyHwnd, "UInt", 74, "Ptr", 0, "Ptr", COPYDATA, "Ptr")
}


Receive_WM_COPYDATA(wParam, lParam, *) {
	global dataReceived := StrGet(
		NumGet(lParam + 2 * A_PtrSize, 'Ptr'),   ; COPYDATASTRUCT.lpData, ptr to a str presumably
		NumGet(lParam + A_PtrSize, 'UInt') / 2   ; COPYDATASTRUCT.cbData, count bytes of lpData, /2 to get count chars in unicode str
	)
}

Re: Create new empty.docx (or copy empty.docx saved somewhere on my computer) in(to) current folder with shortcut?

Posted: 21 Jan 2024, 15:27
by michaelbeijer
Thanks so much @WKen! It works perfectly.

Re: Create new empty.docx (or copy empty.docx saved somewhere on my computer) in(to) current folder with shortcut?

Posted: 21 Jan 2024, 17:40
by flyingDman
re: the windows version you found here viewtopic.php?f=76&t=101873&p=555936#p452877 . You say " which sort of works, albeit not reliably", then later, quoting the same code (except for the references to ThunderRT6FormDC and XYplorer.exe), you say "It works pretty reliably in Windows Explorer". Then in response to Wken's post which includes the Windows version I posted, you say "It works perfectly". So which one it it? ;)

Re: Create new empty.docx (or copy empty.docx saved somewhere on my computer) in(to) current folder with shortcut?

Posted: 21 Jan 2024, 17:54
by michaelbeijer
Ha ha, I think I was confused because if the file (comments.docx) is already in the folder, it won't create a second one. This led me to believe the script wasn't always working. To confuse matters more, I was also trying to run it from inside my multi-script launching script*, which didn't work. (*this one: AHK Startup, by Fanatic Guru @ viewtopic.php?style=19&f=6&t=788&start=100)

Re: Create new empty.docx (or copy empty.docx saved somewhere on my computer) in(to) current folder with shortcut?

Posted: 22 Jan 2024, 08:42
by WKen
Try:

Code: Select all

#Requires AutoHotkey v2

; Create new document -------------------
new_docx(path) {
    flnm := "comments"
	filePath := path "\" flnm ".docx"
	if fileExist(filePath) {
		loop
		{
			filePath := path "\" flnm "-0" A_Index ".docx"
			if !fileExist(filePath)
				break
		}
	}
	oWord := ComObject("word.application")
	oWord.Documents.Add
	oWord.Visible := True
	oword.activeDocument.SaveAs(filePath)
	return
}

; Explorer ------------------------------
^+n::
{
	if !path := getpath()
		return
	new_docx(path)
}

getpath() {
	class := WinGetClass("ahk_id " hwnd := WinExist("A"))
	if (class ~= "(Cabinet|Explore)WClass")
		for window in ComObject("Shell.Application").Windows
			if (window.hwnd==hwnd)
				return window.Document.Folder.Self.Path
}


; XYplorer ------------------------------
; Get the hwnd of the script
G_OwnHWND := A_ScriptHwnd

; Get messages back from XYplorer
OnMessage(0x4a, Receive_WM_COPYDATA)
dataReceived := ""

#HotIf winActive("ahk_class ThunderRT6FormDC")

^+n::
{
	flnm := "comments.docx"
	if !path := XY_Get(false, false)
		return
	new_docx(path)
}

#HotIf



XY_Get(bAll:=false, bSelection:=false)
{
    xyQueryScript := '::if (!' bAll ' && !' bSelection ') {$return = "<curpath>"`;} elseif (' bAll ') {$return = listpane(, , , "<crlf>")`;} elseif (' bSelection ') {$return = get("SelectedItemsPathNames", "<crlf>")`;} copydata ' G_OwnHWND ', "$return", 2`;'

    if xyHwnd := GetXYHWND()
       Send_WM_COPYDATA(xyHwnd, xyQueryScript)

    return dataReceived
}


GetXYHWND() {
	static xyClass := 'ahk_class ThunderRT6FormDC'

    if WinExist(xyClass) {
        for xyid in WinGetList(xyClass)
            for ctrl in WinGetControls(xyid)
                if A_Index = 10
                   return xyid
    }
}


Send_WM_COPYDATA(xyHwnd, message) {
   if !(xyHwnd)
       return

   size := StrLen(message)

   COPYDATA := Buffer(A_PtrSize * 3)
   NumPut("Ptr", 4194305, COPYDATA, 0)
   NumPut("UInt", size * 2, COPYDATA, A_PtrSize)
   NumPut("Ptr", StrPtr(message), COPYDATA, A_PtrSize * 2)

   return DllCall("User32.dll\SendMessageW", "Ptr", xyHwnd, "UInt", 74, "Ptr", 0, "Ptr", COPYDATA, "Ptr")
}


Receive_WM_COPYDATA(wParam, lParam, *) {
	global dataReceived := StrGet(
		NumGet(lParam + 2 * A_PtrSize, 'Ptr'),   ; COPYDATASTRUCT.lpData, ptr to a str presumably
		NumGet(lParam + A_PtrSize, 'UInt') / 2   ; COPYDATASTRUCT.cbData, count bytes of lpData, /2 to get count chars in unicode str
	)
}