Reading from text file and display

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
Jack
Posts: 8
Joined: 16 Jun 2017, 06:04

Reading from text file and display

23 Jun 2017, 01:29

Hi,
I've learned basic of hotkey and has created a simple script to read a text file and display the selected text.
Here's the code :

Code: Select all

#Include C:\tf.ahk

#F1::
Clipboard:= ""
Send ^c
  
StringLower Clipboard, Clipboard
MsgBox % TF_Find("C:\abc.txt", "","", clipboard, 0, 1)
Return
Here's sample data in abc.txt
a
a cheat
b cheat
band
down

I've two prblems :
1 - if nothing is selected it displays entire file contents in msgbox
2 - if I select 'a', it displays all words containing 'a'.

What I'm missing ?
Jack
Posts: 8
Joined: 16 Jun 2017, 06:04

Re: Reading from text file and display

23 Jun 2017, 01:52

Ok, first problem is solved by adding following code before msgbox
if (Clipboard != "")
MsgBox % TF_Find("C:\abc.txt", "","", clipboard, 0, 1)

Anyone can please help me with the second problem ( the exact search )
Guest

Re: Reading from text file and display

23 Jun 2017, 01:52

1 - you can check if the clipboard is empty, if so do nothing (return)
2 - TF_Find uses RegExMatch so if you only want to find 'a' not 'cheat' if you've selected 'a' you can use "\b" for word boundary. See
https://autohotkey.com/docs/misc/RegEx-QuickRef.htm
https://autohotkey.com/docs/commands/RegExMatch.htm

So this should work:

Code: Select all

#Include C:\tf.ahk

#F1::
Clipboard:=""
Send ^c
If (Trim(Clipboard," `r`n`t") = "") ; clipboard is empty or has only spaces, new lines, tabs
	Return
StringLower Clipboard, Clipboard
MsgBox % TF_Find("C:\abc.txt", "","", "\b" clipboard "\b", 0, 1)
Return
Jack
Posts: 8
Joined: 16 Jun 2017, 06:04

Re: Reading from text file and display

23 Jun 2017, 03:07

Hi,
Thank you for the solutions.

First one worked.
\b is not working, may be due to space in words. See sample data I posted in my original post.
Guest

Re: Reading from text file and display

23 Jun 2017, 04:51

It works for me but then again I TRIM the clipboard to remove spaces, try my version as it works as I think it should. 'a' only finds 'a' and 'a cheat' not 'band' - if you only want to find 'a' and not 'a cheat' you need to modify the query

MsgBox % TF_Find("C:\abc.txt", "","", "^" clipboard "$", 0, 1)

^ = start of line, $ end of line
Jack
Posts: 8
Joined: 16 Jun 2017, 06:04

Re: Reading from text file and display

23 Jun 2017, 06:54

Yes, that works.
Thank you for the solutions.

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: doanmvu, mikeyww and 288 guests