本文介绍了删除 Applescript 中的列表项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法从 Applescript 的列表中删除特定项目?

Is there a way to remove a specific item from a list in Applescript?

就像这样:

set theList to {1, 2, 3, 4, 5}
remove item 3 of theList
log theList

--Should log: (*1, 2, 4, 5*)

推荐答案

遗憾的是,AppleScript 中没有像 removeItemAtIndex 这样的高级函数.

Unfortunately there is no higher level function like removeItemAtIndex in AppleScript.

编写这样的函数非常麻烦,因为与其他编程/脚本语言不同,AppleScript 索引从 1 开始.

Writing such a function is quite cumbersome because unlike the other programming/script languages AppleScript indices start at 1.

例如

on removeItem from theList at theIndex
    if theIndex > (count theList) or theIndex is 0 then return theList
    if theIndex = 1 then
        return items 2 thru -1 of theList
    else if theIndex is (count theList) then
        return items 1 thru -2 of theList
    else
        tell theList to return items 1 thru (theIndex - 1) & items (theIndex + 1) thru -1
    end if
end removeItem

在 Foundation Framework 的帮助下容易一些(保持基于 1 的索引)

It's a bit easier with the help of the Foundation Framework (keeping the 1-based indices)

use AppleScript version "2.5"
use framework "Foundation"

on removeItem from theList at theIndex
    if theIndex > (count theList) or theIndex is 0 then return theList
    set mutableArray to current application's NSMutableArray's arrayWithArray:theList
    mutableArray's removeObjectAtIndex:(theIndex - 1)
    return mutableArray as list
end removeItem

这篇关于删除 Applescript 中的列表项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-20 10:22