0

次の作業例の 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 の who 句を使用して、このような繰り返しループを置き換えてパフォーマンスを大幅に向上させることができることを私は知っています。ただし、単語、文字、段落などのテキスト要素リストの場合、これを機能させる方法を見つけることができませんでした。

私が試してみました:

set outlist to words of str whose text contains "is"

これは次の場合に失敗します。

error "Can’t get {\"This\", \"is\", \"a\", \"string\"} whose text contains \"is\"." number -1728

、おそらく「テキスト」はテキストクラスのプロパティではないためです。テキスト クラスのAppleScript リファレンスを見ると、「引用されたフォーム」がテキスト クラスのプロパティであることがわかります。

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 の who 句に置き換える方法はありますか?

4

2 に答える 2

1

@adayzdoneが示したように。あなたはそれで運が悪いようです。

しかし、このようにオフセット コマンドを使用してみることができます。

    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 が単語全体を検出していることを確認できます。それ以外の場合、オフセットは「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

-->{"これ", "は"}

于 2013-09-14T07:05:26.677 に答える
1

AppleScript 1-2-3の 534 ページ (テキストの操作) から

AppleScript は、段落、単語、および文字を、フィルタ参照またはその句を使用した検索でプロパティまたは要素の値を使用して検索できるスクリプト可能なオブジェクトとは見なしません。

別のアプローチを次に示します。

set str to "This is a string"
set outlist to paragraphs of (do shell script "grep -o '\\w*is\\w*' <<< " & quoted form of str)
于 2013-09-13T22:22:50.753 に答える