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

Get help with using AutoHotkey and its commands and hotkeys
Lee James
Posts: 7
Joined: 05 May 2019, 06:27

Script to create and open a new text file?

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?
User avatar
jeeswg
Posts: 6718
Joined: 19 Dec 2016, 01:58
Location: UK

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

05 May 2019, 19:02

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
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
Lee James
Posts: 7
Joined: 05 May 2019, 06:27

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

06 May 2019, 15:47

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?
User avatar
jeeswg
Posts: 6718
Joined: 19 Dec 2016, 01:58
Location: UK

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

07 May 2019, 00:24

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
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
Lee James
Posts: 7
Joined: 05 May 2019, 06:27

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

07 May 2019, 10:10

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! :)
User avatar
jeeswg
Posts: 6718
Joined: 19 Dec 2016, 01:58
Location: UK

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

07 May 2019, 23:22

- 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
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
Lee James
Posts: 7
Joined: 05 May 2019, 06:27

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

08 May 2019, 05:27

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

Return to “Ask For Help”

Who is online

Users browsing this forum: Google [Bot], TAC109 and 216 guests