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

Get help with using AutoHotkey (v2 or newer) and its commands and hotkeys
User avatar
michaelbeijer
Posts: 83
Joined: 04 Oct 2014, 08:01
Location: Hastings, UK.
Contact:

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

20 Jan 2024, 16:17

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
Michael Beijer
Dutch-English technical translator
Hastings, United Kingdom
Email: michael[at]beijer.uk
Website: Beijer.uk
Terminology: Beijerterm.com
AHK-learning project: Beijer.bot
Proz profile: proz.com/profile/652138
LinkedIn: linkedin.com/in/michael-beijer
burque505
Posts: 1736
Joined: 22 Jan 2017, 19:37

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

20 Jan 2024, 16:26

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.
User avatar
michaelbeijer
Posts: 83
Joined: 04 Oct 2014, 08:01
Location: Hastings, UK.
Contact:

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

20 Jan 2024, 16:45

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).
Michael Beijer
Dutch-English technical translator
Hastings, United Kingdom
Email: michael[at]beijer.uk
Website: Beijer.uk
Terminology: Beijerterm.com
AHK-learning project: Beijer.bot
Proz profile: proz.com/profile/652138
LinkedIn: linkedin.com/in/michael-beijer
burque505
Posts: 1736
Joined: 22 Jan 2017, 19:37

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

20 Jan 2024, 17:08

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'.)
User avatar
michaelbeijer
Posts: 83
Joined: 04 Oct 2014, 08:01
Location: Hastings, UK.
Contact:

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

20 Jan 2024, 17:52

Thanks/bedankt!

inderdaad:

ahk_class ThunderRT6FormDC
ahk_exe XYplorer.exe
ahk_pid 14800
ahk_id 460124
Michael Beijer
Dutch-English technical translator
Hastings, United Kingdom
Email: michael[at]beijer.uk
Website: Beijer.uk
Terminology: Beijerterm.com
AHK-learning project: Beijer.bot
Proz profile: proz.com/profile/652138
LinkedIn: linkedin.com/in/michael-beijer
User avatar
michaelbeijer
Posts: 83
Joined: 04 Oct 2014, 08:01
Location: Hastings, UK.
Contact:

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

21 Jan 2024, 12:46

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
	}
Michael Beijer
Dutch-English technical translator
Hastings, United Kingdom
Email: michael[at]beijer.uk
Website: Beijer.uk
Terminology: Beijerterm.com
AHK-learning project: Beijer.bot
Proz profile: proz.com/profile/652138
LinkedIn: linkedin.com/in/michael-beijer
WKen
Posts: 183
Joined: 21 Feb 2023, 00:01

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

21 Jan 2024, 13:29

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
	)
}
User avatar
michaelbeijer
Posts: 83
Joined: 04 Oct 2014, 08:01
Location: Hastings, UK.
Contact:

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

21 Jan 2024, 15:27

Thanks so much @WKen! It works perfectly.
Michael Beijer
Dutch-English technical translator
Hastings, United Kingdom
Email: michael[at]beijer.uk
Website: Beijer.uk
Terminology: Beijerterm.com
AHK-learning project: Beijer.bot
Proz profile: proz.com/profile/652138
LinkedIn: linkedin.com/in/michael-beijer
User avatar
flyingDman
Posts: 2817
Joined: 29 Sep 2013, 19:01

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

21 Jan 2024, 17:40

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? ;)
14.3 & 1.3.7
User avatar
michaelbeijer
Posts: 83
Joined: 04 Oct 2014, 08:01
Location: Hastings, UK.
Contact:

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

21 Jan 2024, 17:54

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)
Michael Beijer
Dutch-English technical translator
Hastings, United Kingdom
Email: michael[at]beijer.uk
Website: Beijer.uk
Terminology: Beijerterm.com
AHK-learning project: Beijer.bot
Proz profile: proz.com/profile/652138
LinkedIn: linkedin.com/in/michael-beijer
WKen
Posts: 183
Joined: 21 Feb 2023, 00:01

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

22 Jan 2024, 08:42

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
	)
}

Return to “Ask for Help (v2)”

Who is online

Users browsing this forum: Descolada and 28 guests