您可以使用AppleScript的split命令将字符串拆分为单词,并使用循环遍历列表中的每个字符串。然后,您可以使用包含语句来检查每个单词是否存在于一个单词列表中,并将匹配的单词添加到新的结果列表中。
以下是一个示例代码:
set wordList to {"apple", "banana", "orange", "grape"}
set stringList to {"I", "like", "to", "eat", "apple", "and", "banana"}
set resultList to {}
repeat with aString in stringList
set wordSplit to my split(aString, " ")
repeat with aWord in wordSplit
if aWord is in wordList then
set end of resultList to aWord
end if
end repeat
end repeat
on split(theString, theDelimiter)
-- Save delimiters to restore old settings
set oldDelimiters to AppleScript's text item delimiters
-- Set delimiters to delimiter passed in
set AppleScript's text item delimiters to theDelimiter
-- Create the array
set theArray to every text item of theString
-- Restore the old setting
set AppleScript's text item delimiters to oldDelimiters
-- Return the result
return theArray
end split
resultList
在这个例子中,wordList
是一个包含要保留的单词的列表,stringList
是一个包含要检查的字符串的列表。代码将遍历stringList
中的每个字符串,并使用split
函数将字符串拆分为单词。然后,它将检查每个单词是否在wordList
中,并将匹配的单词添加到resultList
中。最后,它将打印出resultList
的内容。
请注意,这个示例中的split
函数是自定义的,用于将字符串拆分为单词。它使用AppleScript的text item delimiters
来拆分字符串,并返回一个包含拆分后的单词的列表。