Page 1 of 1

Script to create and open a new text file?

Posted: 05 May 2019, 18:32
by Lee James
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?

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

Posted: 05 May 2019, 19:02
by jeeswg
Something like this:

Code: Select all

q:: ;create a blank text file, open it with Notepad2
vPathNotepad2 := "C:\Program Files\Notepad2\Notepad2.exe"

;use the same filename each time:
;vPath := A_Desktop "\z my txt.txt"

;choose a filename based on the current time:
vPath := A_Desktop "\z " A_Now ".txt"

;do nothing if file already exists
if FileExist(vPath)
{
	MsgBox, % "error: file already exists:`r`n" vPath
	return
}

;create a blank text file (ANSI/UTF-8/UTF-16)
;FileAppend,, % "*" vPath
FileAppend,, % "*" vPath, UTF-8
;FileAppend,, % "*" vPath, UTF-16

Run, "%vPathNotepad2%" "%vPath%"
return
Here's a script to simply open Notepad2:

Code: Select all

w:: ;open Notepad2
vPathNotepad2 := "C:\Program Files\Notepad2\Notepad2.exe"
Run, "%vPathNotepad2%"
return

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

Posted: 06 May 2019, 15:47
by Lee James
jeeswg, thanks so much, that first script is fantastic! :)

The only slight problem is I want it to create the text file in the current Explorer window I'm in, rather than a fixed location. Is this possible?

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

Posted: 07 May 2019, 00:24
by jeeswg
This will create a text file inside the active Explorer window.

Code: Select all

#IfWinActive, ahk_class CabinetWClass
q:: ;explorer - create new text file and open it with Notepad2
#IfWinActive, ahk_class ExploreWClass
q:: ;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

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

Posted: 07 May 2019, 10:10
by Lee James
Jeeswg,

IT WORKED! :) :) :) Thank you so much!

I never thought the Script would need to be so big, I was thinking it would just be a simple line of code. So I really appreciate the trouble you've taken to write this! :)

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

Posted: 07 May 2019, 23:22
by jeeswg
- That's great.
- Btw one thing I like to do is create a file in a folder, and then select it, there's an example here:
jeeswg's Explorer tutorial - AutoHotkey Community
https://autohotkey.com/boards/viewtopic.php?f=7&t=31755

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

Posted: 08 May 2019, 05:27
by Lee James
jeeswg wrote:
07 May 2019, 23:22
- Btw one thing I like to do is create a file in a folder, and then select it
I don't think I would have any need for that, but thanks anyway :)