Detect empty line in an open Notepad file Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
heldheldheld
Posts: 5
Joined: 27 Jan 2022, 14:44

Detect empty line in an open Notepad file

Post by heldheldheld » 27 Jan 2022, 17:45

I have a hotkey to open a specific notes.txt file that I always use for quick notes. To make it more convenient I have it set up so when it starts, automatically the 'Enter' key is send twice and then once ^Home. This way you can immediatly start typing on the top of the file for a fresh note, separated from earlier made notes by 1 break in between.

The problem is that sometimes there might already be 1 or 2 breaks at the top of the file. It gets messy if then automatically there is even more breaks added. I'd like to detect wether there is already a break, or 2 breaks and depending on that either send the right amount of 'Enter' presses or none at all. Anyone know how I can do this?

BoBo
Posts: 6564
Joined: 13 May 2014, 17:15

Re: Detect empty line in an open Notepad file

Post by BoBo » 27 Jan 2022, 17:54

Checking if the line length of the 1st and/or 2nd line = 0 ?

heldheldheld
Posts: 5
Joined: 27 Jan 2022, 14:44

Re: Detect empty line in an open Notepad file

Post by heldheldheld » 27 Jan 2022, 18:00

BoBo wrote:
27 Jan 2022, 17:54
Checking if the line length of the 1st and/or 2nd line = 0 ?
Well, yes that's basically what needs to happen. But I don't know how.

BoBo
Posts: 6564
Joined: 13 May 2014, 17:15

Re: Detect empty line in an open Notepad file  Topic is solved

Post by BoBo » 27 Jan 2022, 18:40

Code: Select all



; 1st line
; 2nd line
#SingleInstance, Force

i:=0
FileReadLine, ln1,% A_ScriptName, 1
FileReadLine, ln2,% A_ScriptName, 2
StrLen(ln1)=0 ? i++ : i
StrLen(ln2)=0 ? i++ : i
MsgBox % "Number of line breaks to add:`t" . 2-i
ExitApp
Will detect if the first two lines of this script are "empty". HTH

heldheldheld
Posts: 5
Joined: 27 Jan 2022, 14:44

Re: Detect empty line in an open Notepad file

Post by heldheldheld » 27 Jan 2022, 19:36

@BoBo Awesome, thnks!

BoBo
Posts: 6564
Joined: 13 May 2014, 17:15

Re: Detect empty line in an open Notepad file

Post by BoBo » 28 Jan 2022, 03:13

Code: Select all



; 1st line
; 2nd line
#SingleInstance, Force
SetBatchLines -1

i:=0
Loop 2
	{	FileReadLine, ln,% A_ScriptName,% A_Index
		StrLen(ln)=0 ? i++ : i
	}
Loop % 2-i
	Send {Enter}
Send {Ctrl Down}{Home}{Ctrl Up}
ExitApp
Should look similar to this.

Post Reply

Return to “Ask for Help (v1)”