Open a .txt file when an 'if' condition is met, then paste clipboard to search file for word, indicate if word found Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
samt
Posts: 62
Joined: 09 Sep 2022, 13:29

Open a .txt file when an 'if' condition is met, then paste clipboard to search file for word, indicate if word found

Post by samt » 30 Jan 2023, 21:02

How to make a .txt to open automatically when an 'if' condition is met, and then to let me paste my clipboard into a search bar created by the script, to see if the txt file contains that 1 word from my clipboard. It would be a different word every time this script runs. If the word is found, then execute a msgbox saying the word has been found, if not, then return I guess. The ctrl+f search function in notepad is quite flimsy and doesn't do a good job.
I've tried the scripts from these threads but they don't seem to do what I'm trying to do

https://www.autohotkey.com/boards/viewtopic.php?f=6&t=576
https://www.autohotkey.com/boards/viewtopic.php?t=29213

I copied and tried the one below too but I don't really understand how to make it work properly. It looks the closest to my purpose but the word to search for would be defined at the moment by pasting it from my clipboard. I am on AHK V1.1...

Code: Select all

If !FileExist(file := A_ScriptDir "\testFile.txt") { ; I made sure the .txt file is in same folder as the script
 MsgBox, 48, Error, File not found. Aborting.`n`n%file%
 Return
} Else FileRead, ttext, %file%
If !WinExist(wTitle := "ahk_exe notepad.exe") {
 Run, notepad.exe
 WinWaitActive, %wTitle%,, 5
 If ErrorLevel {
  MsgBox, 48, Error, An error occurred while waiting for the window. Aborting.
  Return
 }
} Else WinActivate
Send % find(ttext, "website") "`t" find(ttext, "address") "{Enter}" 
Return

find(ttext, key) {
 RegExMatch(ttext, "i)" key ":\h*(\w+)", match)
 Return match1
}	

User avatar
mikeyww
Posts: 26937
Joined: 09 Sep 2014, 18:38

Re: Open a .txt file when an 'if' condition is met, then paste clipboard to search file for word, indicate if word found  Topic is solved

Post by mikeyww » 30 Jan 2023, 21:31

Code: Select all

; This script finds a word in a file
#Requires AutoHotkey v1.1.33
filePath := A_ScriptDir "\test.txt"
If !FileExist(filePath) {
 MsgBox, 48, Error, File not found.`n`n%filePath%
 Return
} Else FileRead txt, % filePath
Gui Font, s10
Gui Add, Edit, w500 vfind gFind
Gui Add, Text, wp   vmsg
Gui Add, Edit, wp   vtxt  r20 ReadOnly, % txt
If True {
 Gui Show,, Find a word
 GuiControl,, find, % Clipboard
}
Return

Find:
Gui Submit, NoHide
If RegExMatch(txt, "i)\b\Q" find "\E\b") {
 GuiControl,, msg, Word was found.
 Gui Color, A2FFA2
} Else {
 GuiControl,, msg, Word was not found.
 Gui Color, FF8E8E
}
Return

samt
Posts: 62
Joined: 09 Sep 2022, 13:29

Re: Open a .txt file when an 'if' condition is met, then paste clipboard to search file for word, indicate if word found

Post by samt » 30 Jan 2023, 22:46

mikeyww wrote:
30 Jan 2023, 21:31

Code: Select all

; This script finds a word in a file
#Requires AutoHotkey v1.1.33
filePath := A_ScriptDir "\test.txt"
If !FileExist(filePath) {
 MsgBox, 48, Error, File not found.`n`n%filePath%
 Return
} Else FileRead txt, % filePath
Gui Font, s10
Gui Add, Edit, w500 vfind gFind
Gui Add, Text, wp   vmsg
Gui Add, Edit, wp   vtxt  r20 ReadOnly, % txt
If True {
 Gui Show,, Find a word
 GuiControl,, find, % Clipboard
}
Return

Find:
Gui Submit, NoHide
If RegExMatch(txt, "i)\b\Q" find "\E\b") {
 GuiControl,, msg, Word was found.
 Gui Color, A2FFA2
} Else {
 GuiControl,, msg, Word was not found.
 Gui Color, FF8E8E
}
Return
This is perfect, thank you!! Did you modify or just make this right on the spot? You're so fast.
Very, very helpful, thanks so much.

User avatar
mikeyww
Posts: 26937
Joined: 09 Sep 2014, 18:38

Re: Open a .txt file when an 'if' condition is met, then paste clipboard to search file for word, indicate if word found

Post by mikeyww » 30 Jan 2023, 23:50

On the spot, modified from all of the other GUIs I've seen! :)

Enjoy!

Post Reply

Return to “Ask for Help (v1)”