Page 1 of 2

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

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

Posted: 29 Nov 2020, 12:32
by Lee James
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?

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

Posted: 29 Nov 2020, 13:02
by mikeyww

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

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

Posted: 29 Nov 2020, 13:39
by Lee James
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.

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

Posted: 29 Nov 2020, 14:04
by mikeyww
Sure. Just remove the MsgBox if you don't need it.

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

Posted: 29 Nov 2020, 16:45
by Lee James
I'm really sorry, I don't know how to remove that part. I don't know where it starts and ends! (Sorry.)

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

Posted: 29 Nov 2020, 18:02
by mikeyww
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

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

Posted: 29 Nov 2020, 19:20
by Lee James
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)?

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

Posted: 29 Nov 2020, 19:47
by mikeyww
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

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

Posted: 30 Nov 2020, 18:30
by Lee James
Thank you. Unfortunately, this is a little glitchy in several respects, so I think your original script is the best one.

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

Posted: 30 Nov 2020, 18:57
by mikeyww
OK. I figured as much. Best of luck.

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

Posted: 09 Jan 2021, 10:42
by Lee James
Might anyone be kind enough to edit down this script so that it doesn't open the text file, only creates it?

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: 09 Jan 2021, 13:05
by mikeyww
Your Run command opens the file, so you can delete it and the "If" statement preceding it.

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

Posted: 13 Jan 2021, 11:50
by Lee James
mikeyww wrote:
09 Jan 2021, 13:05
Your Run command opens the file, so you can delete it and the "If" statement preceding it.
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.)