How to recognize the difference between a blank Notepad and a Notepad with text Topic is solved

Get help with using AutoHotkey (v2 or newer) and its commands and hotkeys
User avatar
Sheepish
Posts: 2
Joined: 02 Jan 2023, 04:35

How to recognize the difference between a blank Notepad and a Notepad with text

02 Jan 2023, 05:27

Hello everyone,

I've been trying to make this script that only creates a new instance of Notepad when there are no blank instances (aka instances that have no text in the window) running, however, if there is a blank instance running it should switch to that instance.

I'm pretty new to ahk, so if there is a simpler way to do what I want I would also appreciate that answer as well.

Here is the code I have so far (I thought I would be able to utilize the WinExclude parameter of WinExist, but I couldn't find a way to denote the general presence of text, nor could I find a way to denote the absence of text within the WinText parameter):

Code: Select all

#n::
{
   if WinExist("Untitled - Notepad")
      WinActivate
   else
      Run "Notepad.exe"
}
Currently when I run the script, with no instances of notepad open, I get a new, blank, instance of notepad (good), then when I switch focus to another window and press #n again, it switches focus back to that same blank instance (also good). However, when I write in the blank instance, switch focus to another window, and then press #n, it still brings me back to the window that I wrote in, despite there being text.

Side Note: When I create a new instance of Notepad, the title is, "Untitled - Notepad" But when I type in that instance the title changes to, "*Untitled - Notepad" I thought that ahk would recognize this as a separate window when WinExist is called, but it seems to find no difference between them which made me confused. Also, I am running version 2.0.2

[Mod action: Moved to v2 section.]
User avatar
boiler
Posts: 17243
Joined: 21 Dec 2014, 02:44

Re: How to recognize the difference between a blank Notepad and a Notepad with text

02 Jan 2023, 05:55

Use WinGetList to get a list of all Notepad windows, then loop through them and use ControlGetText to see if the text in control Edit1 is empty.
Sheepish wrote:
02 Jan 2023, 05:27
Side Note: When I create a new instance of Notepad, the title is, "Untitled - Notepad" But when I type in that instance the title changes to, "*Untitled - Notepad" I thought that ahk would recognize this as a separate window when WinExist is called, but it seems to find no difference between them which made me confused.
Edit after seeing mikeyww's post: Should have checked the new behavior of v2 before posting this which is not correct in v2:
That can’t be true. You must have something else going on to make you think that is the case. You shouldn’t be relying on that title anyway. You should use something like ahk_exe notepad.exe for the WinTitle parameter.
Last edited by boiler on 02 Jan 2023, 06:12, edited 1 time in total.
Reason: Correction
User avatar
mikeyww
Posts: 27216
Joined: 09 Sep 2014, 18:38

Re: How to recognize the difference between a blank Notepad and a Notepad with text

02 Jan 2023, 06:02

Welcome to this AutoHotkey forum!

I was posting at the same time as boiler.

Code: Select all

#Requires AutoHotkey v2.0
#n:: {
 SetTitleMatchMode 1
 WinExist("Untitled - Notepad ahk_exe notepad.exe") ? WinActivate() : Run("notepad")
}
Explained: SetTitleMatchMode
If unspecified, TitleMatchMode defaults to 2 and fast.
User avatar
boiler
Posts: 17243
Joined: 21 Dec 2014, 02:44

Re: How to recognize the difference between a blank Notepad and a Notepad with text

02 Jan 2023, 06:16

If you want to distinguish only on whether the text area is blank, you still may want to check all the windows instead of relying on the title since it's possible to save an empty text file.
Tensai
Posts: 29
Joined: 12 Dec 2019, 14:15

Re: How to recognize the difference between a blank Notepad and a Notepad with text

02 Jan 2023, 06:17

I think what you're looking for is SetTitleMatchMode(3) to match the exact title.

Code: Select all

#Requires AutoHotkey v2
#SingleInstance Force

#n::
{
	SetTitleMatchMode(3)
	if WinExist("Untitled - Notepad")
		WinActivate
	else
		Run "Notepad.exe"
}

But to answer the title, you can use:

Code: Select all

Txt:=WinGetText("Notepad ahk_class Notepad ahk_exe notepad.exe")
MsgBox(Txt)
User avatar
mikeyww
Posts: 27216
Joined: 09 Sep 2014, 18:38

Re: How to recognize the difference between a blank Notepad and a Notepad with text  Topic is solved

02 Jan 2023, 06:18

To boiler's point:

Code: Select all

#Requires AutoHotkey v2.0
#n:: {
 For each, hWnd in WinGetList("ahk_exe notepad.exe")
  If ControlGetText('Edit1', wTitle := 'ahk_id' hWnd) = '' {
   WinActivate wTitle
   Return
  }
 Run 'notepad'
}
swagfag
Posts: 6222
Joined: 11 Jan 2017, 17:59

Re: How to recognize the difference between a blank Notepad and a Notepad with text

02 Jan 2023, 06:31

Code: Select all

#Requires AutoHotkey v2.0
#n::
{
	A_TitleMatchMode := 'RegEx'
	A_TitleMatchModeSpeed := 'Slow'
   if WinExist('i)ahk_exe notepad.exe', , , '[\s\S]')
      WinActivate
   else
      Run "Notepad.exe"
}
User avatar
Sheepish
Posts: 2
Joined: 02 Jan 2023, 04:35

Re: How to recognize the difference between a blank Notepad and a Notepad with text

02 Jan 2023, 22:37

@mikeyww I chose your latest solution using the For...in loop to cycle through the windows (though, thank you everyone for your responses as it helped me learn a lot more!), however, when I used 'Edit1' It kept raising the error:
Error: Target control not found.
Specifically: Edit1
I didn't really understand what Edit1 was (and I'm still not 100% sure), but I looked into it a bit more and after figuring out that Edit1 was a ClassNN I used the window spy to see which ClassNN was being used when I focused a Notepad window. Instead of Edit1 it was RichEditD2DPT1. Is this a windows 11 thing (I saw this mentioned in another thread, but wanted to clarify)? I was curious as to know if I should/shouldn't use RichEditD2DPT1 over Edit1 from now on, what are your thoughts?

p.s. The script works great, though like I mentioned above I needed to replace 'Edit1' with 'RichEditD2DPT1' to get it to work.

Code: Select all

#Requires AutoHotkey v2.0
#n:: {
 For each, hWnd in WinGetList("ahk_exe notepad.exe")
  If ControlGetText('RichEditD2DPT1', wTitle := 'ahk_id' hWnd) = '' {
   WinActivate wTitle
   Return
  }
 Run 'notepad'
}
User avatar
mikeyww
Posts: 27216
Joined: 09 Sep 2014, 18:38

Re: How to recognize the difference between a blank Notepad and a Notepad with text

02 Jan 2023, 23:04

Yes, it sounds like the right approach.

I like swagfag's as well. It's a neat idea, excludes windows that contain any window text.

This (below) works, too, in both Windows 10 and Windows 11.

Code: Select all

#Requires AutoHotkey v2.0
#n:: {
 A_TitleMatchMode := 'RegEx', A_TitleMatchModeSpeed := 'Slow'
 WinExist('i)ahk_exe notepad.exe',,, 's).') ? WinActivate() : Run('notepad')
}

Return to “Ask for Help (v2)”

Who is online

Users browsing this forum: songdg and 15 guests