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: 12
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: 6902
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: 12
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: 6902
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: 12
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: 6902
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: 12
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 :)
Lee James
Posts: 12
Joined: 05 May 2019, 06:27

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

29 Nov 2020, 12:32

The solution given above worked perfectly (thank you again). But now I would like a modified version of it, if anyone could help?

Instead of the text file being opened, I'd just like it to be created and then renamed, so I can run the script then start typing what I want the document to be called.

Can anyone do this?
User avatar
mikeyww
Posts: 2344
Joined: 09 Sep 2014, 18:38

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

29 Nov 2020, 13:02

Code: Select all

vDotExt := ".txt"

#If WinActive("ahk_class CabinetWClass") || WinActive("ahk_class ExploreWClass")
q:: ;explorer - create new text file and open it with Notepad2
WinGet, hWnd, ID, A
For oWin in ComObjCreate("Shell.Application").Windows {
 If (oWin.HWND != hWnd)
  Continue
 vDir := RTrim(oWin.Document.Folder.Self.Path, "\")
 If !InStr(FileExist(vDir), "D") {
  oWin := ""
  Return
 }
 Loop {
  InputBox, vNameNoExt, File name, Enter file name without extension.,, 300, 125
  If (ErrorLevel || vNameNoExt = "")
   Return
  If FileExist(vPath := vDir "\" vNameNoExt vDotExt)
   MsgBox, 48, File exists, The file already exists.`n`n%vPath%
  Else Break
 }
 FileAppend,, % "*" vPath, UTF-8
 MsgBox, 36, Success, Done!`n`nOpen the file
 IfMsgBox, Yes
  Run, %vPath%
 Break
}
oWin := ""
Return
#If
Lee James
Posts: 12
Joined: 05 May 2019, 06:27

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

29 Nov 2020, 13:39

Thank you, Mikie for the quick reply. That's great, and very handy, and I'm going to keep this useful script. But is it possible to change it so there are no dialog boxes, and it just does a standard Windows rename (F2)? I just want things as minimal as possible, and that way, if I don't want to open the file, I don't have to click anything.
User avatar
mikeyww
Posts: 2344
Joined: 09 Sep 2014, 18:38

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

29 Nov 2020, 14:04

Sure. Just remove the MsgBox if you don't need it.
Lee James
Posts: 12
Joined: 05 May 2019, 06:27

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

29 Nov 2020, 16:45

I'm really sorry, I don't know how to remove that part. I don't know where it starts and ends! (Sorry.)
User avatar
mikeyww
Posts: 2344
Joined: 09 Sep 2014, 18:38

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

29 Nov 2020, 18:02

The following version removes the extra prompt. I think it fulfills the description,
Instead of the text file being opened, I'd just like it to be created and then renamed, so I can run the script then start typing what I want the document to be called.
.

Code: Select all

vDotExt := ".txt"

#If WinActive("ahk_class CabinetWClass") || WinActive("ahk_class ExploreWClass")
q:: ;explorer - create new text file and open it with Notepad2
WinGet, hWnd, ID, A
For oWin in ComObjCreate("Shell.Application").Windows {
 If (oWin.HWND != hWnd)
  Continue
 vDir := RTrim(oWin.Document.Folder.Self.Path, "\")
 If !InStr(FileExist(vDir), "D") {
  oWin := ""
  Return
 }
 Loop {
  InputBox, vNameNoExt, File name, Enter file name without extension.,, 300, 125
  If (ErrorLevel || vNameNoExt = "")
   Return
  If FileExist(vPath := vDir "\" vNameNoExt vDotExt)
   MsgBox, 48, File exists, The file already exists.`n`n%vPath%
  Else Break
 }
 FileAppend,, % "*" vPath, UTF-8
 Send {F5}
 Break
}
oWin := ""
Return
#If
Lee James
Posts: 12
Joined: 05 May 2019, 06:27

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

29 Nov 2020, 19:20

I am extremely grateful for your help. Would it be possible to just give just the code for creating the text file, and making sure it's selected (so I can hit enter to open it)?
User avatar
mikeyww
Posts: 2344
Joined: 09 Sep 2014, 18:38

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

29 Nov 2020, 19:47

There's probably a better way to do this, but here is an update.

Code: Select all

vDotExt := ".txt"

#If WinActive("ahk_class CabinetWClass") || WinActive("ahk_class ExploreWClass")
q:: ;explorer - create new text file and open it with Notepad2
WinGet, hWnd, ID, A
For oWin in ComObjCreate("Shell.Application").Windows {
 If (oWin.HWND != hWnd)
  Continue
 If !InStr(FileExist(vDir := RTrim(oWin.Document.Folder.Self.Path, "\")), "D") { ; Non-directory
  oWin := ""
  Return
 }
 Loop ; Increments the filename suffix until file name is unique
  If !FileExist(vPath := vDir "\NewTextDocument" (A_Index = 1 ? "" : " (" A_Index ")") vDotExt)
   Break
 FileAppend,, % "*" vPath, UTF-8
 SplashTextOn, 300, 125, Working, `nPlease wait....
 WinClose
 Run, %A_WinDir%\explorer.exe /select`,%vPath% ; Select the new file in Explorer
 Sleep, 1500
 SplashTextOff
 WinWaitActive, ahk_exe explorer.exe,, 1
 If !ErrorLevel ; Enables renaming; if not needed, delete this line and the next line
  Send {F2}
 Break
}
oWin := ""
Return
#If
Lee James
Posts: 12
Joined: 05 May 2019, 06:27

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

30 Nov 2020, 18:30

Thank you. Unfortunately, this is a little glitchy in several respects, so I think your original script is the best one.
User avatar
mikeyww
Posts: 2344
Joined: 09 Sep 2014, 18:38

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

30 Nov 2020, 18:57

OK. I figured as much. Best of luck.

Return to “Ask For Help”

Who is online

Users browsing this forum: Google [Bot], linearsoulchild, mikeyww, WeedTrek and 24 guests