2017-02-09 60 views
1

如何將AppleScript中用戶輸入的句子變爲每個單詞的單獨變量。例如,Lorem ipsum dolor sit amet會被空格分成不同的變量,例如。 var1 = lorem, var2 = ipsum等。以下是我到目前爲止所提出的,但我顯然無法獲得。將字符串拆分爲多個變量AppleScript

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 

回答

2

只要使用'單詞'關鍵詞。這裏是一個腳本的小例子:

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

repeat with aword in Wordlist --loop through each word 
    log award -- do what ever you need with a word 
end repeat 
相關問題