Is there a way to remove a specific item from a list in Applescript?
So something like this:
set theList to {1, 2, 3, 4, 5}
remove item 3 of theList
log theList
--Should log: (*1, 2, 4, 5*)
Unfortunately there is no higher level function like removeItemAtIndex in AppleScript.
Writing such a function is quite cumbersome because unlike the other programming/script languages AppleScript indices start at 1.
For example
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
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