拆分字符串并获取 AppleScript 中的第一个元素

let*_*ome 3 applescript

我想要一个执行以下操作的 AppleScript:

  • 给定一个字符串 ( Kdiff_full_call) 作为参数
  • 拆分一个字符串并得到
  • 第一个元素,它是一个 zip 文件的路径
  • 将文件解压到Downloads目录中

我怎样才能在 AppleScript 中做到这一点?

Kdiff_full_call 具有以下模式: string1-string2 string3 string4

到目前为止,我有这个脚本:

on open location kdiff_full_call
    set array to my theSplit(kdiff_full_call, "-")
    -- set string1 ... ??
do shell script "/usr/bin/unzip -d " & quoted form of string1
end open location

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)

Pat*_*ita 5

你很接近。在 AS 中,数组是一个项目列表。所以你可以像这样访问它:

set string1 to array's item 1
-- or
set string1 to item 1 of array
Run Code Online (Sandbox Code Playgroud)

或者更好,如果模式永远不会改变并且你总是有相同数量的字符串

set {string1,string2,string3,string4} to array
Run Code Online (Sandbox Code Playgroud)

我建议根据它们的内容重命名你的 vars 以便更好地理解。而不是 string1 我会采取 filePath (或其他)