删除 Applescript 中的列表项
Remove item of list in Applescript
有没有办法从 Applescript 的列表中删除特定项目?
所以像这样:
set theList to {1, 2, 3, 4, 5}
remove item 3 of theList
log theList
--Should log: (*1, 2, 4, 5*)
没有神奇的方法。您只需制作一个包含您想要的项目的列表。例如
set theList to (items 1 thru 2 of theList & items 4 thru -1 of theList)
不幸的是,AppleScript 中没有像 removeItemAtIndex 这样的高级函数。
编写这样的函数非常麻烦,因为与其他 programming/script 语言不同,AppleScript 索引从 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 的索引)会更容易一些
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 的列表中删除特定项目?
所以像这样:
set theList to {1, 2, 3, 4, 5}
remove item 3 of theList
log theList
--Should log: (*1, 2, 4, 5*)
没有神奇的方法。您只需制作一个包含您想要的项目的列表。例如
set theList to (items 1 thru 2 of theList & items 4 thru -1 of theList)
不幸的是,AppleScript 中没有像 removeItemAtIndex 这样的高级函数。
编写这样的函数非常麻烦,因为与其他 programming/script 语言不同,AppleScript 索引从 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 的索引)会更容易一些
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