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?
Script to create and open a new text file? Topic is solved
Re: Script to create and open a new text file?
Something like this:
Here's a script to simply open Notepad2:
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
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
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
Re: Script to create and open a new text file?
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?
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
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
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
Re: Script to create and open a new text file?
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!
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?
- 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
- 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
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
Re: Script to create and open a new text file?
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?
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?
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?
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?
Sure. Just remove the MsgBox if you don't need it.
Re: Script to create and open a new text file?
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?
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?
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?
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?
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?
OK. I figured as much. Best of luck.
Re: Script to create and open a new text file?
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?
Your Run command opens the file, so you can delete it and the "If" statement preceding it.
Who is online
Users browsing this forum: peter_ahk and 175 guests