I'm trying to make a macro that will take a word(s) in the clipboard, search a predesignated TXT file, find that word(s) in the list, and copy the word(s) that's directly one line below it. If there's a space in the next line then or the word isn't in the list, then do nothing. The word or words must match exactly to what's in the line but ignores capitalization.
Here's an example of how this works. I come across this sentence:
The affect caused him to be drowsy for days.
I copy "affect," trigger the macro, it looks through a TXT file until it finds "affect," copies what's on the next line down which is "effect," and copies "effect" into the clipboard. I can then program the macro to paste "effect" in the place of "affect."
The TXT file would simply show:
affect
effect
which
that
whilst
whlie
...and so on.
I tried having ChatGPT 4 write an AppleScript, but, unsurprising, it didn't work. Here's what it gave me:
set theFilePath to "/path/to/your/textfile.txt" -- set the path to your txt file here
-- get the word to find from the clipboard
set theWordToFind to the clipboard
-- read the file
set theFileReference to open for access file theFilePath
set theFileContent to read theFileReference
close access theFileReference
-- split the file content into lines
set theLines to paragraphs of theFileContent
-- iterate over the lines to find the word and get the following word
set foundWord to false
set nextWord to ""
repeat with i from 1 to count theLines
set theLine to item i of theLines
set theWords to words of theLine
if foundWord then
if theLine is not equal to "" then
set nextWord to first item of theWords
exit repeat
end if
else if theWords contains theWordToFind then
set foundWord to true
end if
end repeat
-- check if the word was found and if the following word exists
if foundWord and nextWord is not equal to "" then
set the clipboard to nextWord
display dialog "The word following '" & theWordToFind & "' is '" & nextWord & "'. It has been copied to the clipboard."
else
display dialog "The word '" & theWordToFind & "' was not found in the file, or there was no word following it."
end if
Any help on this would be much appreciated.