将一个字符串拆分为多个变量 AppleScript

ALX*_*ALX 5 string variables applescript

我如何将用户在 AppleScript 中输入的句子转换为每个单词的单独变量。例如,Lorem ipsum dolor sit amet会被空间分割成不同的变量,例如。var1 = lorem, var2 = ipsum等等。以下是我到目前为止的想法,但我显然一无所获。

set TestString to "1-2-3-5-8-13-21"
set myArray to my theSplit(TestString, "-")
on theSplit(theString, theDelimiter)
    -- save delimiters to restore old settings
    set oldDelimiters to AppleScript's text item delimiters
    -- set delimiters to delimiter to be used
    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 theSplit
Run Code Online (Sandbox Code Playgroud)

pbe*_*ell 5

只需使用“词”关键字。这是脚本的一个小例子:

set teststring to "1-2-3-5-8-13-21"
set Wordlist to words of teststring -- convert string to list of words

repeat with aword in Wordlist --loop through each word
    log aword -- do what ever you need with aword
end repeat
Run Code Online (Sandbox Code Playgroud)