Script to create and open a new text file? Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
User avatar
mikeyww
Posts: 27366
Joined: 09 Sep 2014, 18:38

Re: Script to create and open a new text file?

13 Jan 2021, 12:03

Here is one way.

Code: Select all

Run, %A_WinDir%\explorer.exe /select`,"%vPath%"
iPhilip
Posts: 835
Joined: 02 Oct 2013, 12:21

Re: Script to create and open a new text file?

13 Jan 2021, 22:16

Lee James wrote:
13 Jan 2021, 11:50
Thank you! Is there any way to have the script select the file? (So that I can either hit Enter to open, or hit F2 to rename.)
You might find the function below does what you want:

Code: Select all

; Select a file in a Windows Explorer window that begins with the specified name.
; Accounts for "Hide extensions for known file types" setting in Folder Options.
; References: https://docs.microsoft.com/en-us/windows/win32/shell/shell
;             https://docs.microsoft.com/en-us/windows/win32/shell/shellfolderview-selectitem
;             https://autohotkey.com/board/topic/89713-jumping-to-selected-files-in-windows-explorer/?p=568216
;             https://docs.microsoft.com/en-us/previous-versions/windows/internet-explorer/ie-developer/platform-apis/aa752043(v=vs.85)

ExplorerSelect(hwnd, Name) {
   for window in ComObjCreate("Shell.Application").Windows
      if (window.hwnd = hwnd)
         for item in window.Document.Folder.Items
            if (InStr(item.Name, Name) = 1 || InStr(Name, item.Name) = 1)
               return window.Document.SelectItem(item, 1|4|8|16)
}
Cheers!
Windows 10 Pro (64 bit) - AutoHotkey v2.0+ (Unicode 64-bit)
iPhilip
Posts: 835
Joined: 02 Oct 2013, 12:21

Re: Script to create and open a new text file?

26 Jan 2021, 12:03

Lee James wrote:
05 May 2019, 18:32
I'd like to be able to hit a key in Windows Explorer and have it create a new Notepad2 text file (C:\Program Files\Notepad2\Notepad2.exe) then open that file so I can start typing.

Can anyone help?
Hi Lee,

You might want to take a look at this post, specifically the example provided. I hope it helps.

- iPhilip
Windows 10 Pro (64 bit) - AutoHotkey v2.0+ (Unicode 64-bit)
Lee James
Posts: 17
Joined: 05 May 2019, 06:27

Re: Script to create and open a new text file?

28 Jan 2021, 12:36

I am extremely grateful to you both for your help and would not want to waste your time. However, I'm afraid this is all too confusing for my simple brain! I just use AHK for very basic key assignments. I'm not a programmer, can't dig into code, combine scripts, and have have no idea what stuff like 'calling a class' means! Sorry :oops: I'm sorry, please consider me a total dummy.

The original script did almost exactly what I wanted, I would just like it to select the new file, instead of opening it. Could this be very easily changed?

Original script:

Code: Select all

#IfWinActive, ahk_class CabinetWClass
#t:: ;explorer - create new text file and open it with Notepad2
#IfWinActive, ahk_class ExploreWClass
#t:: ;explorer - create new text file and open it with Notepad2
vPathNotepad2 := "C:\Program Files\Notepad2\Notepad2.exe"
vNameNoExt := "New Text Document"
vDotExt := ".txt"
vPath := ""
WinGet, hWnd, ID, A
for oWin in ComObjCreate("Shell.Application").Windows
{
	if (oWin.HWND = hWnd)
	{
		vDir := RTrim(oWin.Document.Folder.Self.Path, "\")
		;if !DirExist(vDir)
		if !InStr(FileExist(vDir), "D")
		{
			oWin := ""
			return
		}

		Loop
		{
			vSfx := (A_Index=1) ? "" : " (" A_Index ")"
			vName := vNameNoExt vSfx vDotExt
			vPath := vDir "\" vName
			if !FileExist(vPath)
				break
		}

		;create a blank text file (ANSI/UTF-8/UTF-16)
		;FileAppend,, % "*" vPath
		FileAppend,, % "*" vPath, UTF-8
		;FileAppend,, % "*" vPath, UTF-16
		break
	}
}
oWin := ""
if FileExist(vPath)
	Run, "%vPathNotepad2%" "%vPath%"
return
iPhilip
Posts: 835
Joined: 02 Oct 2013, 12:21

Re: Script to create and open a new text file?

28 Jan 2021, 13:17

Lee James wrote:
28 Jan 2021, 12:36
I am extremely grateful to you both for your help and would not want to waste your time. However, I'm afraid this is all too confusing for my simple brain! I just use AHK for very basic key assignments. I'm not a programmer, can't dig into code, combine scripts, and have have no idea what stuff like 'calling a class' means! Sorry :oops: I'm sorry, please consider me a total dummy.

The original script did almost exactly what I wanted, I would just like it to select the new file, instead of opening it. Could this be very easily changed?

Original script:

Code: Select all

#IfWinActive, ahk_class CabinetWClass
#t:: ;explorer - create new text file and open it with Notepad2
#IfWinActive, ahk_class ExploreWClass
#t:: ;explorer - create new text file and open it with Notepad2
vPathNotepad2 := "C:\Program Files\Notepad2\Notepad2.exe"
vNameNoExt := "New Text Document"
vDotExt := ".txt"
vPath := ""
WinGet, hWnd, ID, A
for oWin in ComObjCreate("Shell.Application").Windows
{
	if (oWin.HWND = hWnd)
	{
		vDir := RTrim(oWin.Document.Folder.Self.Path, "\")
		;if !DirExist(vDir)
		if !InStr(FileExist(vDir), "D")
		{
			oWin := ""
			return
		}

		Loop
		{
			vSfx := (A_Index=1) ? "" : " (" A_Index ")"
			vName := vNameNoExt vSfx vDotExt
			vPath := vDir "\" vName
			if !FileExist(vPath)
				break
		}

		;create a blank text file (ANSI/UTF-8/UTF-16)
		;FileAppend,, % "*" vPath
		FileAppend,, % "*" vPath, UTF-8
		;FileAppend,, % "*" vPath, UTF-16
		break
	}
}
oWin := ""
if FileExist(vPath)
	Run, "%vPathNotepad2%" "%vPath%"
return
Yes. Like this:

Code: Select all

#IfWinActive, ahk_class CabinetWClass
#t::
vNameNoExt := "New Text Document"
vDotExt := ".txt"
vPath := ""
WinGet, hWnd, ID, A
for oWin in ComObjCreate("Shell.Application").Windows {
   if (oWin.HWND = hWnd) {
      vDir := oWin.Document.Folder.Self.Path
      Loop {
         vSfx := (A_Index=1) ? "" : " (" A_Index ")"
         vName := vNameNoExt vSfx vDotExt
         vPath := vDir "\" vName
         if !FileExist(vPath)
            break
      }
      FileAppend,, % vPath, UTF-8-RAW
      break
   }
}
oWin.Refresh()
for Item in oWin.Document.Folder.Items
   if (Item.Path = vPath)
      oWin.Document.SelectItem(Item, 3|4|8)
oWin := ""
return
Last edited by iPhilip on 29 Jan 2021, 19:21, edited 2 times in total.
Windows 10 Pro (64 bit) - AutoHotkey v2.0+ (Unicode 64-bit)
User avatar
mikeyww
Posts: 27366
Joined: 09 Sep 2014, 18:38

Re: Script to create and open a new text file?

28 Jan 2021, 13:28

I used the following after FileAppend, and removed the Run at the end.

Code: Select all

		For item in oWin.Document.SelectedItems
 			oWin.Document.SelectItem(item.Path, False)
		oWin.Document.SelectItem(vPath, True)
iPhilip
Posts: 835
Joined: 02 Oct 2013, 12:21

Re: Script to create and open a new text file?

29 Jan 2021, 19:23

I updated my post above with tested code. It should work now.
Windows 10 Pro (64 bit) - AutoHotkey v2.0+ (Unicode 64-bit)
lyscop
Posts: 14
Joined: 18 Dec 2021, 20:26

Re: Script to create and open a new text file?

19 Dec 2021, 07:04

mikeyww wrote:
29 Nov 2020, 19:47
There's probably a better way to do this, but here is an update.
The codes above work well, but can it execute on the desktop?
User avatar
mikeyww
Posts: 27366
Joined: 09 Sep 2014, 18:38

Re: Script to create and open a new text file?

19 Dec 2021, 07:15

#IfWinActive controls where the hotkey works. Without it, the hotkey works everywhere. It's OK to sneak a look at the documentation! You actually can dig into the code a bit. https://www.autohotkey.com/docs/commands/_IfWinActive.htm
lyscop
Posts: 14
Joined: 18 Dec 2021, 20:26

Re: Script to create and open a new text file?

20 Dec 2021, 08:04

mikeyww wrote:
19 Dec 2021, 07:15
#IfWinActive controls where the hotkey works. Without it, the hotkey works everywhere. It's OK to sneak a look at the documentation! You actually can dig into the code a bit. https://www.autohotkey.com/docs/commands/_IfWinActive.htm
Thank you for your kind advice on the desktop or the explorer.

Another question about how to select the new text file on the desktop instead of the following code?

Code: Select all

For item in oWin.Document.SelectedItems
    oWin.Document.SelectItem(item.Path, False)
oWin.Document.SelectItem(vPath, True)
User avatar
mikeyww
Posts: 27366
Joined: 09 Sep 2014, 18:38

Re: Script to create and open a new text file?

20 Dec 2021, 09:26

Here are some ideas. viewtopic.php?p=423506#p423506

An adaptation:

Code: Select all

If hWnd(dir := A_ScriptDir)
 SoundBeep, 1500
Else Run, % "explorer.exe /select,""" newestFile(dir) """"

newestFile(dir) {
 Loop, Files, %dir%\*.*
  If (A_LoopFileTimeModified > newestTime)
   newestTime := A_LoopFileTimeModified, sub := A_LoopFilePath
 Return sub
}

hWnd(dir) {
 ; https://www.autohotkey.com/boards/viewtopic.php?p=422958#p422958
 For Window in ComObjCreate("Shell.Application").Windows
  Continue
 Until (Window.document.Folder.Self.Path = dir && hWnd := Window.hwnd)
 Return hWnd
}
lyscop
Posts: 14
Joined: 18 Dec 2021, 20:26

Re: Script to create and open a new text file?

20 Dec 2021, 09:36

mikeyww wrote:
20 Dec 2021, 09:26
Here are some ideas. viewtopic.php?p=423506#p423506
It seems there is the only way that to use Run to select the new text file.

Code: Select all

Run, % "explorer.exe /select,""" path """"
But it will open an explorer and the text file is selected, instead of selecting the text file on the desktop interference.
User avatar
mikeyww
Posts: 27366
Joined: 09 Sep 2014, 18:38

Re: Script to create and open a new text file?

20 Dec 2021, 10:13

Code: Select all

F3::
oWindows := ComObjCreate("Shell.Application").Windows, VarSetCapacity(hWnd, 4, 0)
                                                   ; VT_BYREF := 0x4000, VT_I4 := 0x3
oWin := oWindows.FindWindowSW(0, "", SWC_DESKTOP := 8, ComObject(0x4003, &hWnd), SWFO_NEEDDISPATCH := 1)
Loop, Files, %A_Desktop%\*.*
 oWin.Document.SelectItem(A_LoopFilePath, False)
oWin.Document.SelectItem(newestFile(A_Desktop), True)
SoundBeep, 1000
Return

newestFile(dir) {
 Loop, Files, %dir%\*.*
  If (A_LoopFileTimeModified > newestTime)
   newestTime := A_LoopFileTimeModified, sub := A_LoopFilePath
 Return sub
}
lyscop
Posts: 14
Joined: 18 Dec 2021, 20:26

Re: Script to create and open a new text file?

20 Dec 2021, 10:45

mikeyww wrote:
20 Dec 2021, 10:13

Code: Select all

F3::
oWindows := ComObjCreate("Shell.Application").Windows, VarSetCapacity(hWnd, 4, 0)
                                                   ; VT_BYREF := 0x4000, VT_I4 := 0x3
oWin := oWindows.FindWindowSW(0, "", SWC_DESKTOP := 8, ComObject(0x4003, &hWnd), SWFO_NEEDDISPATCH := 1)
Loop, Files, %A_Desktop%\*.*
 oWin.Document.SelectItem(A_LoopFilePath, False)
oWin.Document.SelectItem(newestFile(A_Desktop), True)
SoundBeep, 1000
Return

newestFile(dir) {
 Loop, Files, %dir%\*.*
  If (A_LoopFileTimeModified > newestTime)
   newestTime := A_LoopFileTimeModified, sub := A_LoopFilePath
 Return sub
}
Works perfect,Thanks.
SeeYouAtTop
Posts: 8
Joined: 22 Dec 2020, 13:27

Re: Script to create and open a new text file?

24 Jul 2022, 06:31

mikeyww wrote:
20 Dec 2021, 10:13
That's what I wanted. Thank you so much,
Love from India!


[Mod edit: Moved misplaced quote tags from around reply text.]

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Bing [Bot], Google [Bot], peter_ahk and 402 guests