How can I excerpt a whole sentence from paragrapgh contaning a keyword

Hi!

I want to ask how can I excerpt a whole sentence from a paragraph which contains keyword?

Example: keyword= Australian
I want this sentence = ““It was an oversight but not a deliberate snub,” he told The Australian newspaper.”

Paragraph = “Matt Doran - from Channel 7 - flew from Sydney to London on 4 November to meet Adele for her only Australian interview about her new album, 30.
But after admitting during it that he had not listened to the album, Sony withheld the interview footage.
Doran apologised and said he had missed an email with a preview copy of the songs.
“It was an oversight but not a deliberate snub,” he told The Australian newspaper. “This is the most important email I have ever missed.”
He denied reports he had been suspended from Channel 7, after being absent from on-air duties.”

My aim is excerpt any sentence which contain my keyword. Thanks

First, I rewrote your text as single paragraph, because your text is not single paragraph. Assuming you want process the paragraph and not the text.

Also, your text in the example has 2 sentences containing the keyword “Australian”. So, you will get 2 sentences in the result. To got only second sentence, make the keyword more specific. For example do it “Australian newspaper”.


set myKeyword to "Australian"

set aParagraph to "Matt Doran - from Channel 7 - flew from Sydney to London on 4 November to meet Adele for her only Australian interview about her new album, 30. But after admitting during it that he had not listened to the album, Sony withheld the interview footage. Doran apologised and said he had missed an email with a preview copy of the songs. \"It was an oversight but not a deliberate snub\", he told The Australian newspaper.\" This is the most important email I have ever missed.\""

set ATID to AppleScript's text item delimiters
set AppleScript's text item delimiters to {"."}
set theSentences to text items of aParagraph
set AppleScript's text item delimiters to ATID

set filteredSentences to {}
repeat with aSentence in theSentences
	if aSentence contains myKeyword then
		set end of filteredSentences to contents of aSentence & "."
	end if
end repeat

if (count filteredSentences) = 1 then
	return filteredSentences as text
else
	display dialog "The result contains multiple matches.
So,  provide more specific keyword."
	return filteredSentences
end if

Hej KniazidisR

Thank you very much for your help. I am new to AppleScript. Your script working but I cant append the result to an example.txt. How can I solve this problem. Thank you very much.

Here is example. :


do shell script "echo " & filteredSentences as text & linefeed & " >> /users/123/Desktop/testFile.txt"

Replace return filteredSentences as text code line with this example. You should indicate full Posix path of your text file as well. Instead of mine.

Hi KniazidisR.

Thank you very much. It worked.:slight_smile: