Applescript WHERS子句能否用于过滤文本元素列表,如单词、字符和段落

Applescript WHERS子句能否用于过滤文本元素列表,如单词、字符和段落,applescript,Applescript,我有以下工作示例AppleScript代码段: set str to "This is a string" set outlist to {} repeat with wrd in words of str if wrd contains "is" then set end of outlist to wrd end repeat 我知道AppleScript中的WHERS子句通常可以用来替换这样的重复循环,从而显著提高性能。然而,对于文本元素列表,例如单词、字符和段落,我还没有找到

我有以下工作示例AppleScript代码段:

set str to "This is a string"

set outlist to {}
repeat with wrd in words of str
    if wrd contains "is" then set end of outlist to wrd
end repeat
我知道AppleScript中的WHERS子句通常可以用来替换这样的重复循环,从而显著提高性能。然而,对于文本元素列表,例如单词、字符和段落,我还没有找到一种方法来实现这一点

我试过:

set outlist to words of str whose text contains "is"
这在以下情况下失败:

error "Can’t get {\"This\", \"is\", \"a\", \"string\"} whose text contains \"is\"." number -1728
,可能是因为“text”不是text类的属性。查看,我看到“引用的形式”是text类的一个属性,所以我有一半希望它能起作用:

set outlist to words of str whose quoted form contains "is"
但这也失败了,因为:

error "Can’t get {\"This\", \"is\", \"a\", \"string\"} whose quoted form contains \"is\"." number -1728
有没有办法用AppleScript中的which子句替换这样的重复循环?

摘自的第534页(使用文本)

Apple脚本不考虑段落、单词和字符。 可通过使用对象的值来定位的可编写脚本的对象 使用筛选器引用的搜索中的属性或元素,或 条款

以下是另一种方法:

set str to "This is a string"
set outlist to paragraphs of (do shell script "grep -o '\\w*is\\w*' <<< " & quoted form of str)
将str设置为“这是一个字符串”

如@adayzdone所示,将outlist设置为(do)shell脚本“grep-o'\\w*is\\w*”的段落。看起来您的运气不好

但是您可以尝试像这样使用offset命令

    set wrd to "I am here"
        set outlist to {}

        set str to " This is a word"

  if ((offset of space & "is" & space in str) as integer) is greater than 0 then set end of outlist to wrd
请注意“is”周围的空格。这确保Offset正在查找整个单词。否则,Offset将在“This”中查找第一个匹配的“is”

更新

按照OP的要求使用它

set wrd to "I am here"
set outlist to {}

set str to " This is a word"
repeat with wrd in words of str

    if ((offset of "is" in wrd) as integer) is greater than 0 then set end of outlist to (wrd as string)
end repeat

-->{“This”,“is”}

这并没有给出我要查找的内容。我问题中的示例重复循环结构比包含“is”的所有str单词的列表要长,即{“This”,“is”}。不幸的是,偏移量一次只能给出一个偏移量。这只是向您显示偏移量。如果在同一重复循环中使用偏移量,则会搜索每个单词。但我确实忽略了您的“包含”认为您只需要“是”的要点“。在mo的ipad上。但将为未来读者更新答案更新以扩展如何使用偏移量来达到OP的目标hi@markhunte,最新的编辑几乎是我原始问题中的重复循环,除了使用“偏移量”而不是“包含”。问题的重点是找出是否有一种方法可以在不重复循环的情况下过滤输入列表。这可以通过对某些Applescript对象使用WHERS子句来完成,但显然不能通过文本元素来完成。您好@AtomicToothrush Yer。我似乎在阅读您的问题时遇到了问题。我会睁开眼睛,下次做得更好。;-)这正是我所担心的——只是不可能,至少在纯Applescript中有WHERS子句。shell脚本方法是一种不错的变通方法,尽管并不完全正确。它返回{“is”,“is”},而不是完整的单词,即{“This”,“is”}。但是,这可能可以通过调整grep表达式来解决。是的,只需要一些缺少的引号:
将outlist设置为(do shell脚本“grep-o'\\w*is\\w*”的段落