Jump to content


Photo

Create new text file


  • Please log in to reply
2 replies to this topic

#1 sssweat

sssweat
  • Members
  • 1 posts

Posted 16 September 2009 - 04:23 PM

I've searched all the forum without finding any easy way to create new text files, so i did it myself^^

activation with altGr+n

By default text files are created, but it can easily be changed for .ahk for instance ^^

Notice:
The text file is created in the active window, but if the active window is not a Windows window, the file is created on the desktop

Only tested on Vista

And maybe my english translation could be better, if you have any suggestions, tell me;)

http://yfrog.com/02newtxtj

In English:
#SingleInstance Force 
#Persistent 
#NoEnv 
SendMode Input 
SetBatchLines -1 
DetectHiddenText On 
#NoTrayIcon


<^>!n::
default=
default2=TXT
{ ;searching for "path"
WinGetClass Class, A
If Class = Progman
	{
	Path = %A_Desktop%
	}
else if Class <> CabinetWClass
	{
	Path = %A_Desktop%
	}
else
	{
	ControlGetText Path, Edit1, ahk_class %Class%
	}
}

NewTry2:
{ ;creation du GUI
Gui, Add, Edit, vTxtName x5 y26 w162 h22 , %default%  ; edit du nom du fichier
Gui, Add, Edit, vExtName x181 y26 w49 h22 , %default2%
Gui, Add, Button, Default x34 y55 w54 h24 , OK
Gui, Add, Button, x146 y55 w54 h24 , Cancel
Gui, Add, Text, x5 y5 w224 h16 , Set the file name:
Gui, Add, Text, x173 y30 w11 h22 , .
Gui, Show, x416 y367 h87 w243, New text file
Return
}

ButtonOK:
Gui, Submit
Gui, Destroy

Test:
;verification de la validité du nom donné ( /\:?*"<>| sont interdits )
{	
if TxtName= 
	{
	MsgBox,, New text file, You didn't set any file name !
	default2 = %ExtName%
	Goto NewTry2
	}
else if ExtName= 
	{
	MsgBox,, New text file, you didn't set any extension !
	default = %TxtName%
	default2=
	{ ;creation du GUI specifique "pas d'extension" + goto Test
	Gui, Add, Edit, vExtName x181 y26 w49 h22 , %default2%
	Gui, Add, Edit, vTxtName x5 y26 w162 h22 , %default%  ; edit du nom du fichier
	Gui, Add, Button, Default x34 y55 w54 h24 , OK
	Gui, Add, Button, x146 y55 w54 h24 , Cancel
	Gui, Add, Text, x5 y5 w224 h16 , Set the file name:
	Gui, Add, Text, x173 y30 w11 h22 , .
	Gui, Show, x416 y367 h87 w243, New text file
	Return

	OK:
	Gui, Submit
	Gui, Destroy
	}
	Goto Test
	}
else if ( InStr(TxtName,"/") || InStr(TxtName,"\") || InStr(TxtName,":") || InStr(TxtName,"?") || InStr(TxtName,"*") InStr(TxtName,"<") || InStr(TxtName,">") || InStr(TxtName,"|") )
	{
	MsgBox,, New text file, Invalid folder name.`n`nAre forbidden: / \ : ? * " < > |
	default = %TxtName%
	default2 = %ExtName%
	Goto NewTry2
	}
else ifinstring TxtName, "
	{
	MsgBox,, New text file, Invalid folder name.`n`nAre forbidden: / \ : ? * " < > |
	default = %TxtName%
	default2 = %ExtName%
	Goto NewTry2
	}
else if ( InStr(ExtName,"/") || InStr(ExtName,"\") || InStr(ExtName,":") || InStr(ExtName,"?") || InStr(ExtName,"*") InStr(ExtName,"<") || InStr(ExtName,">") || InStr(ExtName,"|") )
	{
	MsgBox,, New text file, Invalid folder name.`n`nAre forbidden: / \ : ? * " < > |
	default = %TxtName%
	default2 = %ExtName%
	Goto NewTry2
	}
else ifinstring ExtName, "
	{
	MsgBox,, New text file, Invalid folder name.`n`nAre forbidden: / \ : ? * " < > |
	default = %TxtName%
	default2 = %ExtName%
	Goto NewTry2
	}
IfExist %Path%\%TxtName%.%ExtName%
	{ 
	MsgBox,, New text file, This file name already exists.
	default = %TxtName%
	default2 = %ExtName%
	Goto NewTry2 
	return
	}
}

StringLower, ExtName, ExtName
FileAppend,, %Path%\%TxtName%.%ExtName%
if ExtName=txt
	{
	run %Path%\%TxtName%.%ExtName%
	}
return

ButtonCancel:
GuiClose:
Gui,Destroy
ExitApp

but also in French ;)
#SingleInstance Force 
#Persistent 
#NoEnv 
SendMode Input 
SetBatchLines -1 
DetectHiddenText On 
#NoTrayIcon


<^>!n::
default=
default2=TXT
{ ;recherche de path
WinGetClass Class, A
If Class = Progman
	{
	Path = %A_Desktop%
	}
else if Class <> CabinetWClass
	{
	Path = %A_Desktop%
	}
else
	{
	ControlGetText Path, Edit1, ahk_class %Class%
	}
}

NewTry2:
{ ;creation du GUI
Gui, Add, Edit, vTxtName x5 y26 w162 h22 , %default%  ; edit du nom du fichier
Gui, Add, Edit, vExtName x181 y26 w49 h22 , %default2%
Gui, Add, Button, Default x34 y55 w54 h24 , OK
Gui, Add, Button, x146 y55 w54 h24 , Annuler
Gui, Add, Text, x5 y5 w224 h16 , Entrez le nom du fichier texte:
Gui, Add, Text, x173 y30 w11 h22 , .
Gui, Show, x416 y367 h87 w243, Nouveau fichier texte
Return
}

ButtonOK:
Gui, Submit
Gui, Destroy

Test:
;verification de la validité du nom donné ( /\:?*"<>| sont interdits )
{	
if TxtName= 
	{
	MsgBox,, Nouveau fichier texte, Vous n'avez pas entré de nom de fichier !
	default2 = %ExtName%
	Goto NewTry2
	}
else if ExtName= 
	{
	MsgBox,, Nouveau fichier texte, Vous n'avez pas entré d'extension !
	default = %TxtName%
	default2=
	{ ;creation du GUI specifique "pas d'extension" + goto Test
	Gui, Add, Edit, vExtName x181 y26 w49 h22 , %default2%
	Gui, Add, Edit, vTxtName x5 y26 w162 h22 , %default%  ; edit du nom du fichier
	Gui, Add, Button, Default x34 y55 w54 h24 , OK
	Gui, Add, Button, x146 y55 w54 h24 , Annuler
	Gui, Add, Text, x5 y5 w224 h16 , Entrez le nom du fichier texte:
	Gui, Add, Text, x173 y30 w11 h22 , .
	Gui, Show, x416 y367 h87 w243, Nouveau fichier texte
	Return

	OK:
	Gui, Submit
	Gui, Destroy
	}
	Goto Test
	}
else if ( InStr(TxtName,"/") || InStr(TxtName,"\") || InStr(TxtName,":") || InStr(TxtName,"?") || InStr(TxtName,"*") InStr(TxtName,"<") || InStr(TxtName,">") || InStr(TxtName,"|") )
	{
	MsgBox,, Nouveau fichier texte, Ce nom de fichier est invalide.`n`nSymboles interdits: / \ : ? * " < > |
	default = %TxtName%
	default2 = %ExtName%
	Goto NewTry2
	}
else ifinstring TxtName, "
	{
	MsgBox,, Nouveau fichier texte, Ce nom de fichier est invalide.`n`nSymboles interdits: / \ : ? * " < > |
	default = %TxtName%
	default2 = %ExtName%
	Goto NewTry2
	}
else if ( InStr(ExtName,"/") || InStr(ExtName,"\") || InStr(ExtName,":") || InStr(ExtName,"?") || InStr(ExtName,"*") InStr(ExtName,"<") || InStr(ExtName,">") || InStr(ExtName,"|") )
	{
	MsgBox,, Nouveau fichier texte, Ce nom de fichier est invalide.`n`nSymboles interdits: / \ : ? * " < > |
	default = %TxtName%
	default2 = %ExtName%
	Goto NewTry2
	}
else ifinstring ExtName, "
	{
	MsgBox,, Nouveau fichier texte, Ce nom de fichier est invalide.`n`nSymboles interdits: / \ : ? * " < > |
	default = %TxtName%
	default2 = %ExtName%
	Goto NewTry2
	}
IfExist %Path%\%TxtName%.%ExtName%
	{ 
	MsgBox,, Nouveau fichier texte, Ce nom de fichier existe déjà.
	default = %TxtName%
	default2 = %ExtName%
	Goto NewTry2 
	return
	}
}

StringLower, ExtName, ExtName
FileAppend,, %Path%\%TxtName%.%ExtName%
if ExtName=txt
	{
	run %Path%\%TxtName%.%ExtName%
	}
return

ButtonAnnuler:
GuiClose:
Gui,Destroy
ExitApp


edit: corrected the cancel button problem; it was due to translation ^^
but i won't have xp before 2weeks, sorry

#2 keybored

keybored
  • Members
  • 351 posts

Posted 18 September 2009 - 04:20 AM

sssweat,
Couple things noted on XP testing. There is no code for the cancel button. The Desktop variable is showing up instead of my location. It may have to do with "ExploreWClass" because of dual pane windows and other situations.

#3 Scoox

Scoox
  • Members
  • 117 posts

Posted 13 May 2012 - 02:55 AM

Thanks to the OP for getting me started with the idea of creating a new text file with Autohotkey. I tried the script but the bit that retrieves the current folder path didn't work. Obtaining the path from the address box is not the neatest way to do it, so I searched the forums for a better solution. Eventually I found this which enabled me to put together the following script.

Notes:
1) I use Classic Explorer and I have put a cutom command button on my toolbar, therefore the script can only be launched from within an explorer window. If you want to run this script anywhere via hotkey (e.g. open file dialogue, desktop, etc) then you will need to add the relevant window classes to the Explorer group (see code).
2) There is no GUI to this script.

#SingleInstance Force 
#NoEnv 
SetBatchLines -1 
#NoTrayIcon

;Explorer only
GroupAdd, Explorer, ahk_class CabinetWClass

LButton ^ t:: ;Create new text document. To launch via Classic Explorer button, comment out this line

If WinActive("ahk_group Explorer")
{
	DirPath := ActiveFolderPath() . "\"
	Disambiguator := ;in case file exists, append "(<number>)" to file namne
	FileName := "New text file"
	FileExt := ".txt"

	FilePath := DirPath . FileName . Disambiguator . FileExt
	
	While FileExist(FilePath) ;Find suitable file name (i.e. one that does not exist already)
	{
		Index += 1
		Disambiguator := " (" . Index . ")"
		FilePath := DirPath . FileName . Disambiguator . FileExt
	}

	FileAppend,, %FilePath%

	While !FileExist(FilePath)
		Sleep 200
	Sleep 1000 ;White till file icon is visible (sending F5 speeds things up but causes window to flicker)
	
	ControlFocus, SysListView321, A
	Send % FileName . Disambiguator . FileExt
	SendInput {F2}
}

Exit

;------------- Explorer Path Functions  ------------

;http://www.autohotkey.com/community/viewtopic.php?t=76118

; use #IfWinActive ahk_group ExplorerGroup
; to make sure the active window is an
; Explorer window before calling this function
;
ActiveFolderPath()
{
   return PathCreateFromURL( ExplorerPath(WinExist("A")) )
}

; slightly modified version of function by jethrow
; on AHK forums.
;
ExplorerPath(_hwnd)
{
   for Item in ComObjCreate("Shell.Application").Windows
      if (Item.hwnd = _hwnd)
         return, Item.LocationURL
}

; function by SKAN on AHK forums
;
PathCreateFromURL( URL )
{
 VarSetCapacity( fPath, Sz := 2084, 0 )
 DllCall( "shlwapi\PathCreateFromUrl" ( A_IsUnicode ? "W" : "A" )
         , Str,URL, Str,fPath, UIntP,Sz, UInt,0 )
 return fPath
}